In a explicit CXX project, CMake *silently* ignores .c files in the configuration step
This is probably an intended and correct behaviour, I'd just like to point out that it happens silently which can create an linker error later in the build process.
In a project that looks like this:
project(foo CXX)
add_library(foo foo.cpp bar.c)
add_exectuable(fooRunner runner.cpp)
target_link_libraries(fooRunner PRIVATE foo)
Here CMake will promptly ignore the bar.c file and when linking the fooRunner it will result in a linker error.
I know a solution is to use set_source_file_properties(bar.c PROPERTIES LANGUAGE CXX)
which is fine in itself.
But I think either a functional or user-warning change needs to happen.
As I've specified my project to explicitly be CXX and then explicitly link a .c file into a cxx library, I think either
- User-warning change: CMake should warn me that it skipped the .c file as there is no C compiler set and that I need to force it with the
set_source_file_properties(..
command if I want it to compile and link it as CXX - Functional change: Actually compile it as CXX as I've only marked it as such.
I'm leaning towards the former solution as the latter is perhaps breaking backwards compatibility and is maybe not even the mind of best practice. I realize this might be a niche problem, but it occured for a user that needed my help. And since it happens silently without error or warning, while being unlikely it's what the user expects, it might warrant a change? Removing gotchas is always a plus in my book.