cMake to compile C source files with g++ compiler

Viewed 347

I have been asked to compile C Source codes with g++ compiler in cMake environment.

I tried one of the solution from Stack Overflow:

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

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

but I get below errors while running 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:
1 Answers

For individual files you can set the language yourself independent of the file extension by setting the 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)

This results in CMake using the C++ compiler for the .c files regardless of the compilers used for C / C++.


It may be possible to achieve the same effect for the whole project by listing only C++ as language, but I haven't tested this option:

# top level project
project(MyProject LANGUAGES CXX)

...
Related