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
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: //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