[CMake] Pure template libraries

Eric Noulard eric.noulard at gmail.com
Wed Nov 12 07:58:08 EST 2008


2008/11/12 Cristóvão Sousa <crisjss at gmail.com>:
> Hi,
>
> I'm having some trouble about creating the correct CMakeLists.txt files to
> deal with pure template libraries (header files only).
>
> A simple sample:
>
> I have 3 directories with a file in each one:
> - Libbase
>        '- base.hpp
> - Libderiv
>         '- deriv.hpp
> - Exec
>     '- main.cpp
>
> File main.cpp includes file deriv.hpp;
> File deriv.hpp includes file base.hpp:
> main.cpp --> deriv.hpp --> base.hpp
>
>
> Right now I have the following cmake files:
>
> root CMakeLists.txt :
>        add_subdirectory(Libbase)
>        add_subdirectory(Libderiv)
>        add_subdirectory(Exec)
>
> Libbase/CMakeLists.txt :
> #empty
>
> Libderiv/CMakeLists.txt :
> #empty
>
> Exec/CMakeLists.txt :
> set(exec_SRCS main.cpp)
> add_executable(exec ${exec_SRCS})
> include_directories(${PROJECT_SOURCE_DIR}/base)
> include_directories(${PROJECT_SOURCE_DIR}/deriv)
>
>
> This works fine, but is a little bit annoying having to place
> "include_directories(${PROJECT_SOURCE_DIR}/base)"
> in the Exec/CMakeLists.txt file. (because in the real project
> I use a lot of pure template libraries which include a lot of
> other libraries, and often I don't remember them all.)
>
> 1- Is it possible to create a pure headers library with "add_library"?
>   I tried passing only the header fine name but it complains about not
>   knowing how to compile it.
>
> 2- And how can we make a include_directory be transitive, i.e.
>   having a library XZY which "include_directory( ABC_DIR )",
>   automatically do the "include_directory( ABC_DIR )" on any
>   "target_link_libraries( EXECTARG XYZ)"?

If your header are somehow "public" since they must be included by other part
of the porject you may either:

1) add them explicitely to your exe sources files:

   set(exec_SRCS main.cpp ../ Libbase/base.hpp ../Libderiv/deriv.hpp)

2) Or may be defining "template lib" like (note the PARENT_SCOPE in SET)
   Libbase/CMakeLists.txt :
   SET(TLIB_BASE ${CMAKE_CURRENT_SOURCE_DIR}/base.hpp PARENT_SCOPE)

   Libderiv/CMakeLists.txt :
   SET(TLIB_DERIV ${CMAKE_CURRENT_SOURCE_DIR}/deriv.hpp PARENT_SCOPE)

   Exec/CMakeLists.txt :
   set(exec_SRCS main.cpp ${TLIB_BASE} ${TLIB_DERIV})
   add_executable(exec ${exec_SRCS})

3) export them somewhere in your build tree and include_directory  the
choosen place:

   Libbase/CMakeLists.txt :
   CONFIGURE_FILE(base.hpp build_include)

   Libderiv/CMakeLists.txt :
   CONFIGURE_FILE(deriv.hpp build_include)

   Exec/CMakeLists.txt :
   include_directories(build_include)
   set(exec_SRCS main.cpp)
   add_executable(exec ${exec_SRCS})


I did not test my proposal, but I think they should work.

-- 
Erk


More information about the CMake mailing list