CMake 可以使用 g++ 编译 C 文件吗? [英] Can CMake use g++ to compile C files?

查看:60
本文介绍了CMake 可以使用 g++ 编译 C 文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾在一个项目中使用 g++ 编译以 .c 结尾的文件中的 C 代码.原因是我听说 g++ 有更好的警告信息.

I have worked on a project where I was using g++ to compile C code in files that end in .c. The reason is that I'm told that g++ has better warning messages.

我正在将此项目的构建过程切换为使用 CMake.我发现最初 CMake 想使用 gcc 来编译 C 文件.由于在使用时声明变量之类的事情,这失败了.所以我尝试使用g++通过设置

I am switching the build process for this project to use CMake. I found that initially CMake wanted to use gcc to compile C files. This failed because of things like declaring variables at use time. So I tried to use g++ to compile C files by using the setting

set(CMAKE_C_COMPILER_INIT g++)

在 CMakeLists.txt 文件中.但这会导致错误消息:

in the CMakeLists.txt file. But this results in the error message:

#error "The CMAKE_C_COMPILER is set to a C++ compiler"

我一直将我的 .c 文件重命名为 .cpp 来解决这个问题,因为这对我来说似乎是最简单的方法,也许也是最好的方法.但是我想知道是否可以强制 CMake 使用 g++ 来编译 C 文件.

I have been renaming my .c files to .cpp to fix this problem as that seems to be the easiest way for me to make things work, and perhaps the best way too. But I was wondering if it is possible to force CMake to use g++ to compile C files.

推荐答案

您不应为此目的覆盖编译器.如果你真的需要将你的 C 文件编译为 C++,那么你应该教 cmake 你的文件属于 C++ 语言:

You should not override the compiler for this purpose. If you really need to compile your C files as C++ then you should teach cmake that your files belong to C++ language:

set_source_files_properties(filename.c PROPERTIES LANGUAGE CXX )

这篇关于CMake 可以使用 g++ 编译 C 文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆