24

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.

Gabriel Southern
  • 9,602
  • 12
  • 56
  • 95
  • 2
    "The reason is that I'm told that g++ has better warning messages." - And could you confirm that? I'd say it's very questionable, because (as far as I know) both are just drivers invoking the same compiler with different arguments (language, include paths, standard library linked in, etc.) Apart from that, g++ isn't a C compiler so you end up writing very poor C++. –  Oct 07 '11 at 17:36
  • If you're writing C, then compile with a C compiler. C and C++ aren't the same thing. – Kusalananda Oct 07 '11 at 18:00
  • 1
    You said '_This failed because of things like declaring variables at use time_'; that sounds like you are using GCC in C89 mode, instead of C99 mode. Try adding `-std=c99` to the C compiler - somehow. – Jonathan Leffler Oct 08 '11 at 04:27
  • thanks for the comments I'll be sure to think about these suggestions. – Gabriel Southern Oct 10 '11 at 18:36

4 Answers4

50

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 )
Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
  • it sounds like what I wanted to do is not the best idea and I should rename the files to .cpp. But thanks for a correct answer it's useful to know how to do what I wanted in the event that it is really necessary. – Gabriel Southern Oct 10 '11 at 18:37
  • any idea why applying this to a single file in my project would make all the files in the project be compiled as C++ ? I have a bunch of .c files and I want only one of the to be compiled as C++. – Scorpio Jul 24 '14 at 12:25
  • 1
    @Scorpio, I actually want that symptom, but in a defined way... Why doesn't CMake have a way to compile all .c files as C++? – kchoi Aug 08 '16 at 17:38
  • This does not fix subsequent link problems for C++ projects when driving the link process through the compiler driver. CMake does not know to use C++ libraries during link, and it breaks on Solaris and some older Linux. – jww Nov 28 '18 at 21:26
  • 3
    You can do the following to treat all C files and C++. `file(GLOB_RECURSE CFILES "${CMAKE_SOURCE_DIR}/*.c") SET_SOURCE_FILES_PROPERTIES(${CFILES} PROPERTIES LANGUAGE CXX )` – jwmay Apr 08 '19 at 11:30
  • 2
    Clang prints a **deprecated** message: `clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]` – Salva Corts Aug 12 '20 at 10:18
3

To have cmake treat all C files as C++ files use:

file(GLOB_RECURSE CFILES "${CMAKE_SOURCE_DIR}/*.c")
SET_SOURCE_FILES_PROPERTIES(${CFILES} PROPERTIES LANGUAGE CXX )
Richard
  • 56,349
  • 34
  • 180
  • 251
1

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

project(derproject LANGUAGES CXX)
Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
0

set_source_files_properties

The CMake setting of (my) choice here would be the set_source_files_properties command. https://cmake.org/cmake/help/latest/command/set_source_files_properties.html

set(qpid_dispatch_SOURCES
  alloc.c
  alloc_pool.c
  aprintf.c
  amqp.c
  atomic.c
# [...]
)
set_source_files_properties(${qpid_dispatch_SOURCES} PROPERTIES LANGUAGE CXX)
add_library(qpid-dispatch OBJECT ${qpid_dispatch_SOURCES})

As described in the linked docs, CMake 3.18 changed the scoped effect of set_source_files_properties. See the DIRECTORY and TARGET_DIRECTORY options. Therefore, to apply source file property recursively to all files in your project, your CMakeLists.txt should look something like this

cmake_minimum_required(VERSION 3.20)
project(qpid-dispatch LANGUAGES C CXX)

# [...]

add_subdirectory(src)
add_subdirectory(tests)
add_subdirectory(router)

# [...]

file(GLOB_RECURSE CFILES "*.c")
set_source_files_properties(${CFILES}
        DIRECTORY src tests router
        PROPERTIES LANGUAGE CXX)
user7610
  • 25,267
  • 15
  • 124
  • 150