Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can CMake use g++ to compile C files?

Tags:

gcc

g++

cmake

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.

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++)

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

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

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.

like image 592
Gabriel Southern Avatar asked Oct 07 '11 17:10

Gabriel Southern


2 Answers

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 )
like image 182
Andrey Kamaev Avatar answered Sep 17 '22 23:09

Andrey Kamaev


If you need to switch the whole project, set it in the project directive:

project(derproject LANGUAGES CXX)
like image 33
Victor Sergienko Avatar answered Sep 18 '22 23:09

Victor Sergienko