Reputation: 13
I have a directory containing implementations of some class. Users should be able to add their own implementation which is later dynamically loaded. For this purpose every implementation contains a maker() function. Therefore every implementation must be linked into a single shared library. The directory where all implementations are located is ./pixgeo/src/. For now the CMake file looks like this:
ADD_LIBRARY( FEI4Single SHARED ./pixgeo/src/FEI4Single.cc)
ADD_LIBRARY( FEI4Double SHARED ./pixgeo/src/FEI4Double.cc)
ADD_LIBRARY( FEI4FourChip SHARED ./pixgeo/src/FEI4FourChip.cc)
ADD_LIBRARY( Mimosa26 SHARED ./pixgeo/src/Mimosa26.cc)
INSTALL_SHARED_LIBRARY( FEI4Single DESTINATION lib )
INSTALL_SHARED_LIBRARY( FEI4Double DESTINATION lib )
INSTALL_SHARED_LIBRARY( FEI4FourChip DESTINATION lib )
INSTALL_SHARED_LIBRARY( Mimosa26 DESTINATION lib )
Is there a way to do the following:
For all %{X} in the directory, do the following:
ADD_LIBRARY( %{X} SHARED ./pixgeo/src/%{X}.cc)
INSTALL_SHARED_LIBRARY( %{X} DESTINATION lib )
Upvotes: 1
Views: 1284
Reputation: 14947
A combination of FILE(GLOB ...)
and FOREACH()
:
FILE(GLOB IMPL_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/pixgeo/src/*.cc)
FOREACH(impl_src ${IMPL_SRCS})
# Extract FEI4Single from FEI4Single.cc
GET_FILENAME_COMPONENT(basename ${impl_src} NAME_WE)
ADD_LIBRARY( ${basename} SHARED ${impl_src} )
INSTALL_SHARED_LIBRARY( ${basename} DESTINATION lib )
ENDFOREACH()
Note the use of ${CMAKE_CURRENT_SOURCE_DIR}
. It's not critical to this discussion, but you shouldn't use relative paths to source files, as it can interfere with out-of-source builds.
Upvotes: 2