[CMake] Pseudo library

Michael Hertling mhertling at online.de
Tue Aug 23 00:48:24 EDT 2011


On 08/20/2011 04:33 AM, ☂Josh Chia (谢任中) wrote:
> I have a C++ .h file with template classes that require the Python and
> Boost-Python libraries for successful linking. There is no associated .cpp
> file.
> 
> I consider this .h file a library that other parts of my project can use.
> The CMakefile.txt for those parts should not need to know the implementation
> detail that the Python and Boost-Python libraries are needed. Is there a way
> to create a pseudo-library, say, 'foo', that those other parts can link,
> e.g. with target_link_libraries, so that they just need to link the pseudo
> library 'foo' in order to effectively depend on and link to the Python and
> Boost-Python libraries?
> 
> Note that my pseudo-library is pseudo because it doesn't have any .cpp file.
> I can't actually compile that into a real library, and add_library won't
> allow me to specify zero source  files.

If your pseudo library is restricted to your project, you might do
what's outlined in the following exemplary CMakeLists.txt files:

# CMakeLists:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(PSEUDO CXX)
SET(CMAKE_VERBOSE_MAKEFILE ON)
ADD_SUBDIRECTORY(pseudo)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.cxx "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.cxx)
TARGET_LINK_LIBRARIES(main pseudo)

# pseudo/CMakeLists.txt:
FIND_PACKAGE(Boost REQUIRED python)
FIND_PACKAGE(PythonLibs REQUIRED)
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pseudo.hxx
"template<class T> class pseudo{}\n")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pseudo.cxx
"static void pseudo(void){}\n")
ADD_LIBRARY(pseudo STATIC pseudo.cxx pseudo.hxx)
TARGET_LINK_LIBRARIES(pseudo ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

The header-of-interest is pseudo.hxx; here, generated on the fly for
convenience. The actual pseudo library is made up of pseudo.cxx that
contains a simple function not visible to the outside world, note the
"static" keyword, so linking against this static library doesn't mean
any harm. The pseudo library is in turn linked against the Python and
Boost-Python libraries via TARGET_LINK_LIBRARIES(). Finally, the main
target is linked against the pseudo library which pulls in the Python
and Boost-Python libraries as can be seen in the output of Make. If I
understand correctly, this should do what you intend.

If your pseudo library is also to be used by other projects, you
should write a configuration file, usually <Pseudo>Config.cmake
or <pseudo>-config.cmake, providing the well-known variables:

<PSEUDO>_INCLUDE_DIRS: Include directories of your header and
the ones of further headers which your header might include.
<PSEUDO>_LIBRARIES: Full paths of libraries - or the names
of imported targets - your header requires to link against,
i.e. Boost-Python and Python in the above-noted case.
<PSEUDO>_DEFINITIONS: definitions that your header
requires to be set for proper compilation.

In this way, a project may issue FIND_PACKAGE(<PSEUDO>) and

ADD_DEFINITIONS(${<PSEUDO>_DEFINITIONS})
INCLUDE_DIRECTORIES(${<PSEUDO>_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(... ${<PSEUDO>_LIBRARIES})

to use your header and ensure that its prerequisites are
recognized; therefor, you don't need an actual library.

'hope that helps.

Regards,

Michael

PS: Some toolchains accept an empty source file for a static library or
    even an empty source file list, i.e. ADD_LIBRARY(pseudo STATIC "").


More information about the CMake mailing list