I am trying to organize a C++ project which starts to have a lot of files. I would like to create two executables which share some source file using Cmake. I have found an i
You have to quote the variables.
add_executable(test_mss "${Common_sources}" "${Mss_sources}")
Otherwise for an empty variable, CMake replaces the variable by nothing and the number of arguments seems to be wrong.
Similar problem: https://stackoverflow.com/a/39733128/2799037
The error you get is because source list, passed to add_executable
, is actually empty.
Correct way for collect sources in Common/
subdirectory is:
file(GLOB Common_sources "Common/*.cpp")
In command file(GLOB) RELATIVE option doesn't specify search directory. Instead, it just tells CMake to generate relative paths instead of absolute:
If RELATIVE flag is specified, the results will be returned as relative paths to the given path.
Assuming
file(GLOB Common_sources "Common/*.cpp")
# gets: /<path-to-source>/Common/my_source.cpp
then (also note to absolute path in RELATIVE option)
file(GLOB Common_sources RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/Common" "Common/*.cpp")
# gets: my_source.cpp
and (when files are not under RELATIVE directory)
file(GLOB Common_sources RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/Mps" "Common/*.cpp")
# gets: ../Common/my_source.cpp