My CMake project builds an executable consisting of C and C++ source files:
add_executable(myAP
main.cpp
DpdkSock.c
<snip>
)
I want to specify different compile options for the .cpp and .c files. (For example, compiling the .cpp file requires the -std=c++11 flag, but gcc does not recognise that flag and so it should not be used for the .c file).
How can I achieve this?
(I am using add_compile_options() to specify the options).
Thanks for your answers. I am still struggling somewhat. The suggestions worked for -std=c++11, but I also want to enable ‘-pedantic’ for C++ files but not for C. I tried this:
# Enable pedantic warnings for C++ files but not C files
target_compile_options(ThebeAP PRIVATE
$<$<CXX_COMPILER_ID:GNU>:
-pedantic>)
But it seems gcc.exe is still also using -pedantic.
You are not using the right generator expression. $<CXX_COMPILER_ID:GNU> asks about compiler id, not the compiler used for files. So, as soon as the C++ compiler is GNU, the genex is true.
You have to define options based on compiler used for compiling files: $<$<COMPILE_LANG_AND_ID:CXX,GNU>:-pedantic>.