[CMake] How to set path to library header files?
Chris Johnson
cxjohnson at gmail.com
Tue Dec 2 12:01:05 EST 2014
Background: I'm converting an existing project from a custom build process
which uses BSD Make to CMake. The source for the project is contained in
about 600 directories, and has about a dozen libraries and maybe a hundred
executables. The old build system (make) has to continue working until the
new CMake-based system is fully operational and tested. For the most part,
this has been straight forward and easy, since make is building in source
using in-source Makefiles. I've simply added a CMakeLists.txt file to each
directory, and the actual CMake build occurs out-of-source. It's only the
edge cases (of course!) which have given me a headache. I've solved some
of them. This message is about one I have not yet been able to solve.
The basic problem is this: my executables cannot find the header files for
some of the libraries, because they are in a subdirectory when installed,
but are not in a subdirectory while in source.
Here's a simplified example (SSCCE) which reproduces this problem.
The file structure:
.
|-- bin/
|-- include/
| `-- mylib/
|-- lib/
`-- src/
|-- CMakeLists.txt
|-- mylib/
| |-- CMakeLists.txt
| |-- myfunc.cpp
| `-- myfunc.h
`-- prog/
|-- CMakeLists.txt
`-- prog.cpp
The top-level bin, include and lib directories are the install locations,
exactly parallel to standard Unix locations. Note that file myfunc.h
installs into ./include/mylib. Note also that prog.cpp includes this
header via #include "myfunc.h".
Here are the current CMakeLists.txt files.
Top level (../src):
cmake_minimum_required(VERSION 2.8.4)
project(src)
add_subdirectory(mylib)
add_subdirectory(prog)
mylib:
cmake_minimum_required(VERSION 2.8.4)
project(mylib)
set(CPP_SOURCE myfunc.cpp)
set(HEADERS myfunc.h)
add_library(mylib ${CPP_SOURCE} )
target_include_directories(
mylib PUBLIC
# Headers used from source/build location:
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
# Headers used from the installed location:
"$<INSTALL_INTERFACE:include>"
)
install(TARGETS mylib DESTINATION lib)
install(FILES ${HEADERS} DESTINATION include/mylib)
prog:
cmake_minimum_required(VERSION 2.8.4)
project(prog)
set(SOURCE_FILES prog.cpp)
set(LIBS mylib)
add_executable(prog ${SOURCE_FILES})
target_link_libraries(prog ${LIBS})
install(TARGETS prog DESTINATION bin)
When I build, I get this error:
src/.build$ make install
-- Configuring done
-- Generating done
-- Build files have been written to: /sandbox/src/.build
Scanning dependencies of target mylib
[ 50%] Building CXX object mylib/CMakeFiles/mylib.dir/myfunc.cpp.o
Linking CXX static library libmylib.a
[ 50%] Built target mylib
Scanning dependencies of target prog
[100%] Building CXX object prog/CMakeFiles/prog.dir/prog.cpp.o
/sandbox/src/prog/prog.cpp:1:10: fatal error: 'mylib/myfunc.h'
file not found
#include "mylib/myfunc.h"
^
1 error generated.
make[2]: *** [prog/CMakeFiles/prog.dir/prog.cpp.o] Error 1
make[1]: *** [prog/CMakeFiles/prog.dir/all] Error 2
make: *** [all] Error 2
How can I create CMake rules that will allow me to work around this?
As I mentioned above, the existing make-based build has to continue
working, so I cannot change the #include statement to remove the
subdirectory path.
..chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20141202/1c606def/attachment.html>
More information about the CMake
mailing list