CMake exports targets with absolute path

Viewed 411

I am developing a static library and I am using CMake. This library needs Boost. So I did the following:

set(LIBRARY_NAME "MyLib")
set(LIBRARY_VERSION 1.0.0)
project(${LIBRARY_NAME} VERSION ${LIBRARY_VERSION})

set(Boost_USE_STATIC_LIBS   ON)
set(Boost_USE_MULTITHREADED ON) 
find_package(Boost COMPONENTS system filesystem regex thread date_time log log_setup REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

add_library(${LIBRARY_NAME} STATIC xx.cpp)

target_link_libraries(${LIBRARY_NAME} PUBLIC ${Boost_LIBRARIES})

Everything is fine till now.

Now, I need to make this library install-able. So, I follow this tutorial https://cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html

Everything looks fine on my machine. However, when I moved the installed files (The files that are generated by calling make-install) into another machine and tried to use it using find_package(MyLib), problem raised saying that it could find boost in the in a place supposed to be on my original machine. I dig into the file MyLibTargets.cmake and I saw absolute paths of Boost library!

Why this is happining? how can I prevent this?

1 Answers

If you took the cmake build directory and used it from another machine, this won't work. cmake is not intended to support different platforms from the same working directory.

You need to use out of source builds for this purpose. But once you did an in place build with cmake the directory is burned and out of source builds no longer work for this source tree. You need to delete all cmake temporary files and folders to get the source tree working again in this case.

Related