Can CMake use g ++ to compile C files?

I was working on a project in which I used g ++ to compile C code in files that end in .c. The reason is because they tell me that g ++ has the best warning messages.

I will move on to the build process for this project to use CMake. I found that CMake originally wanted to use gcc to compile C files. This failed due to things like declaring variables during use. So I tried using g ++ to compile C files using the parameter

set(CMAKE_C_COMPILER_INIT g++) 

in the CMakeLists.txt file. But this leads to an error message:

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

I renamed my .c files to .cpp to fix this problem, since it seems to me that this is the easiest way to get everything working, and maybe the best way. But I was wondering if CMake can be used to use g ++ to compile C files.

+8
gcc g ++ cmake
source share
1 answer

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 are in C ++:

 set_source_files_properties(filename.c PROPERTIES LANGUAGE CXX ) 
+21
source share

All Articles