1

I want to change the LANGUAGE property of all .c files to be CXX so that they're compiled with g++. However, when I use the result of a GLOB_RECURSE in set_source_files_properties, it seems to have no effect.

I have the following in my CMakeLists.txt and its output is not making sense to me.

get_source_file_property(property_before ${CMAKE_SOURCE_DIR}/examples/one/file.c LANGUAGE)
message(STATUS ${property_before})
# OUTPUT: NOTFOUND

file(GLOB_RECURSE c_sources "*.c")
foreach(c_file ${c_sources})
    message(STATUS ${c_file})
    # examples/one/file.c is included in the list of files
endforeach(c_file)
set_source_files_properties(c_sources PROPERTIES LANGUAGE CXX)

get_source_file_property(property_after ${CMAKE_SOURCE_DIR}/examples/one/file.c LANGUAGE)
message(STATUS ${property_after})
# OUTPUT: NOTFOUND

set_source_files_properties(${CMAKE_SOURCE_DIR}/examples/one/file.c PROPERTIES LANGUAGE CXX)
get_source_file_property(property_after_manual ${CMAKE_SOURCE_DIR}/examples/one/file.c LANGUAGE)
message(STATUS ${property_after_manual})
# OUTPUT: CXX

Source tree is something like:

.
├── CMakeLists.txt
├── examples # that use my library
│   ├── one
│   │   ├── file.c
│   │   └── CMakeLists.txt
│   ├── …
│   └── CMakeLists.txt
├── include # my library headers
│   ├── header1.h
│   ├── header2.h
│   └── …
├── src
│   ├── src1.cpp
│   ├── src2.cpp
│   └── …
└── test #tests
    └── …

Pretty sure I'm doing something dumb and it's an easy fix, but I've wasted over an hour and I didn't find the issue. I really don't want to add manually all the .c files :-)

João Neto
  • 1,732
  • 17
  • 28
  • 1
    The answer to your original problem: ["Tell CMake to use C++ compiler for C files coming from CMake?"](https://stackoverflow.com/questions/37931068/tell-cmake-to-use-c-compiler-for-c-files-coming-from-cmake) – Florian Apr 17 '18 at 11:22
  • 1
    The file paths used in `get_source_file_property()` have to be the same as used in your `add_library()`/`add_executable()` commands. And "Source file properties are visible only to targets added in the same directory". – Florian Apr 17 '18 at 11:24
  • I've tried that solution and doesn't work for me. I forgot to add: the C files are scattered throughout the project, in different directories and are used to build different executables. – João Neto Apr 17 '18 at 11:27
  • "Source file properties are visible only to targets added in the same directory" -- ah! This may be the issue. The targets are only being declared in the sub-directories, while I'd want to compile all C files with g++ – João Neto Apr 17 '18 at 11:38
  • Sorry! Your solution in https://stackoverflow.com/questions/37931068/tell-cmake-to-use-c-compiler-for-c-files-coming-from-cmake indeed works. However, it looks very weird to me why it works. I'll follow-up on the other thread :-) thanks! – João Neto Apr 17 '18 at 11:46
  • 1
    Happy I could help. Please be aware that the other Q&A started with having only the `CXX` language activated by explicitly listing the enabled languages in the `project()` command. Probably also something you may want to try. – Florian Apr 17 '18 at 12:00
  • If I remove the `C` language I get `make[2]: *** No rule to make target 'examples/one/CMakeFiles/file.dir/build'. Stop.`. – João Neto Apr 17 '18 at 14:15
  • That's the part in the linked question where you have to add `list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS c)` so `c` is a valid file extension for the `cxx` compiler. – Florian May 06 '18 at 19:01

1 Answers1

2

You need to expand the c_sources variable like so: set_source_files_properties(${c_sources} PROPERTIES LANGUAGE CXX), like you already do it correctly in the foreach above.

bk138
  • 3,033
  • 1
  • 34
  • 28