Вопрос

EDIT ===================

The apr-1 is the apache portable runtime, which I downloaded and compiled myself to create the shared library (so its not made by cmake). I need to link against this library after I run CPack on the target system.

My development project directory:

read_config/bin
           /libs
           /include
           /src
           /build

For my CMakeLists.txt, everything installs ok, but now I have a linking problem, as it cannot find the new location of the library.

I have removed the LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/libs) as I don't need this now, as I am using the FIND_LIBRARY.

And added the following:

# Find the library in the libs folder
FIND_LIBRARY(APR_LIBS NAMES "apr-1" PATHS ${PROJECT_SOURCE_DIR}/libs)
# Print out the path to see if its correct
MESSAGE(${APR_LIBS})
# Link the library with the executable
TARGET_LINK_LIBRARIES(cfg ${APR_LIBS})

# Install the libs folder
INSTALL(DIRECTORY ${PROJECT_SOURCE_DIR}/libs/
    DESTINATION libs)

# Install the executable in the bin folder
INSTALL(TARGETS cfg 
    RUNTIME DESTINATION bin)

Everything installs ok, but when I check the executable to see what libraries it links against it cannot find the apr-1 as it looks in the wrong place.

libapr-1.so.0 => not found

This is because I have extracted the installation package to a new location which is different to where it was compiled from. From the compiled directory I get this, which runs ok:

libapr-1.so.0 => /home/devel/projects/read_config/libs/libapr-1.so.0 (0xf7746000)

I after I install I have this directory structure on the target installation machine, so the executable will link with the new libs/apr-1 library:

target directory/
      /bin
      /libs/apr-1

Many thanks for any further suggestions,

========================================

gcc (GCC) 4.7.2 cmake version 2.8.9 Fedora 17

Hello,

I am getting in the following error:

CMake Error at src/CMakeLists.txt:14 (INSTALL):
  install TARGETS given target "apr-1" which does not exist in this
  directory.

I have the following directory:

read_config/
       /build - out of source build
       /CMakeLists.txt
       /include - apr include files
       /libs - apr libraries
       /src - project source files *.h *.c CMakeLists.txt

In my root CMakeLists.txt I have the following:

# Name of project and compiler to use
PROJECT(read_cfg C)

# Set compiler flags
IF(CMAKE_COMPILER_IS_GNUCC)
    MESSAGE(STATUS "COMPILER IS GNUCC")
    SET(CMAKE_C_FLAGS "-Wall -Wextra -m32 -Wunreachable-code -ggdb -O0 -D_DEBUG -D_LARGEFILE64_SOURCE")
ENDIF(CMAKE_COMPILER_IS_GNUCC)

# Where the include files can be found
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/read_config/include)

# Where the libraries can be found
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/read_config/libs)

ADD_SUBDIRECTORY(src)

In my src directory I have the following CMakeLists.txt

# Create the executable and link the libraries into it
ADD_EXECUTABLE(cfg test_config.c)

# Place executable in bin directory
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)

# Add libraries
IF(CMAKE_COMPILER_IS_GNUCC)
  MESSAGE(STATUS "Linking cfg library")
  TARGET_LINK_LIBRARIES(cfg apr-1)
ENDIF(CMAKE_COMPILER_IS_GNUCC)

INSTALL(TARGETS cfg 
    RUNTIME 
    DESTINATION bin)
INSTALL(TARGETS apr-1 
    ARCHIVE 
    DESTINATION lib)

SET(CPACK_PACKAGE_NAME "rd_cfg")
SET(CPACK_PACKAGE_VENDOR "sun.com")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "rd_cfg - CPack read_config installation")
SET(CPACK_PACKAGE_VERSION "1.0.0")
SET(CPACK_PACKAGE_VERSION_MAJOR "1")
SET(CPACK_PACKAGE_VERSION_MINOR "0")
SET(CPACK_PACKAGE_VERSION_PATCH "0")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "Read_config_install_dir")

INCLUDE(CPack)

The problem is this line here:

INSTALL(TARGETS apr-1 
    ARCHIVE 
    DESTINATION lib)

I have tried doing the following

INSTALL(TARGETS ${CMAKE_SOURCE_DIR}/libs/apr-1 
    ARCHIVE 
    DESTINATION lib)

But it still fails to find the library.

And also using the path like this:

INSTALL(TARGETS src/libs 
    ARCHIVE 
    DESTINATION lib)

I would like to be able to just put the directory there like this so I don't have to specifiy each library, but just the directory like this:

INSTALL(TARGETS ${CMAKE_SOURCE_DIR}/libs 
    ARCHIVE 
    DESTINATION lib)

So it can find all the libraries in that directory. I have about 5 of them.

Many thanks for any suggestions,

Это было полезно?

Решение

The error seems to be a result of calling install in a different CMakeLists.txt to that in which apr-1 is defined via add_library.

I guess you're doing add_library(apr-1 ...) in your root CMakeLists.txt, which means that's where you need to call install(TARGETS apr-1 ARCHIVE DESTINATION lib).

Note that the error message "...does not exist in this directory." is not referring to actual directories in the filesystem sense, but rather the notional directories which come about via add_subdirectory commands.

Edit

In light of your comments, it seems that the issue is that apr-1 is not an actual CMake target, but rather just a library.

In this case, the install command cannot be the TARGETS version, it would have to be install(FILES...) or install(DIRECTORY...). This would probably mean you'd have to locate the library using e.g. find_library in order to get a full path.

Further Edit

This further problem is as a result of not setting the correct RPATH flags. You need to add the following lines to your src/CMakeLists.txt before the ADD_EXECUTABLE call:

SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/libs")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/libs" isSystemDir)
IF("${isSystemDir}" STREQUAL "-1")
   SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/libs")
ENDIF("${isSystemDir}" STREQUAL "-1")

For a full explanation on these CMake variables, see the CMake Wiki on RPATH handling

Другие советы

try adding this to your CMakeLists.txt

set(CMAKE_INSTALL_RPATH $LIBRARY_OUTPUT_PATH)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top