cMake 使用 g++ 编译器编译 C 源文件

cMake to compile C source files with g++ compiler

我被要求在 cMake 环境中使用 g++ 编译器编译 C 源代码。

我尝试了 Stack Overflow 中的一种解决方案:

Set CC,CXX before running cmake like in below steps.

export CC=/usr/bin/g++
export CXX=/usr/bin/g++

但我在 运行 cmake

时遇到以下错误
"cmake -DPLATFORM=x64 ../"
-- The C compiler identification is unknown
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/g++
-- Check for working C compiler: /usr/bin/g++ -- broken
CMake Error at /usr/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):
  The C compiler

    "/usr/bin/g++"

  is not able to compile a simple test program.

  It fails with the following output:

对于单个文件,您可以通过设置 LANGUAGE source file property:

独立于文件扩展名自行设置语言
# compile foo.c and bar.c with the C++ compiler
set_source_files_properties(foo.c bar.c PROPERTIES LANGUAGE CXX)

这导致 CMake 对 .c 文件使用 C++ 编译器,而不考虑用于 C/C++ 的编译器。


通过仅将 C++ 列为语言可能会为整个项目实现相同的效果,但我尚未测试此选项:

# top level project
project(MyProject LANGUAGES CXX)

...