My suggestion is to tackle this in two phases:
- Build a library from the
.cpp
and.h
files, usingadd_library
- Iterate through all your
.cxx
files and create an executable from each, usingadd_executable
andforeach
Build the library
This could be something as simple as
file( GLOB LIB_SOURCES lib/*.cpp )
file( GLOB LIB_HEADERS lib/*.h )
add_library( YourLib ${LIB_SOURCES} ${LIB_HEADERS} )
Build all the executables
Simply loop over all the .cpp files and create separate executables.
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
# file( GLOB APP_SOURCES RELATIVE app/*.cxx )
file( GLOB APP_SOURCES app/*.cxx )
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} YourLib )
endforeach( testsourcefile ${APP_SOURCES} )
Some warnings:
file( GLOB )
is usually not recommended, because CMake will not automatically rebuild if a new file is added. I used it here, because I do not know your sourcefiles.- In some situations, source-files may be found with a full pathname. If necessary, use the RELATIVE flag for
file(GLOB ...)
. - Manually setting the source-files requires a change to CMakeLists.txt, which triggers a rebuild. See this question for the (dis-)advantages of globbing.
- I generated the testname using a
string( REPLACE ... )
. I could have used get_filename_component with theNAME_WE
flag.
Concerning “general” CMake info, I advise you to read some of the broad “CMake Overview” questions already asked here on stackoverflow. E.g.: