将位于子文件夹中的外部项目库与cmake链接

时间:2016-05-23 14:20:12

标签: c++ cmake

我有这个目录结构:

CMakeLists.txt

ext/                                  # contains all the external projects
    xz/
        CMakeList.txt
    gtest/
        CMakeLists.txt
src/                                  # contains the Cpp code
    Class1.cpp
    Class2.cpp
    main.cpp
includes/                             # contains the include files
    Class1.h
    Class2.h

其中ext/CMakeList.txt文件包含:

cmake_minimum_required(VERSION 3.1)
project(libxz_builder C CXX)
include(ExternalProject)

ExternalProject_Add(project_xz
        INSTALL_DIR ${COMMON_LOCAL}
        URL http://tukaani.org/xz/xz-5.2.1.tar.gz
        SOURCE_DIR ${COMMON_LIB}/xz-5.2.1
        CONFIGURE_COMMAND ${COMMON_LIB}/xz-5.2.1/configure --prefix=${COMMON_LOCAL}
        BUILD_COMMAND make
        BUILD_IN_SOURCE 1
        )
ExternalProject_Get_Property(project_xz install_dir)
add_library(xz STATIC IMPORTED)
set_property(TARGET xz PROPERTY IMPORTED_LOCATION${install_dir}/lib/liblzma${CMAKE_STATIC_LIBRARY_SUFFIX})
add_dependencies(xz project_xz)

并且主文件夹CMakeList文件包含:

cmake_minimum_required(VERSION 3.1)
set(PROJECT_NAME_STR DM)
set(CMAKE_CXX_STANDARD 11)
project(${PROJECT_NAME_STR} C CXX)

set(CPP_SOURCE_FILES ${PROJECT_SOURCE_DIR}/src)
set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/include)
set(COMMON_LOCAL ${PROJECT_SOURCE_DIR}/local)
set(COMMON_LIB ${PROJECT_SOURCE_DIR}/lib)

set(EXT_PROJECTS_DIR ${PROJECT_SOURCE_DIR}/ext)

add_subdirectory(${EXT_PROJECTS_DIR}/gtest)
add_subdirectory(${EXT_PROJECTS_DIR}/xz)


#-------------------
# Test
#-------------------

enable_testing()
set(PROJECT_TEST_NAME ${PROJECT_NAME_STR}_test)

file(GLOB TEST_SRC_FILES ${PROJECT_SOURCE_DIR}/test/*.cpp)
file(GLOB ALL_SRC_FILES ${CPP_SOURCE_FILES}/*.cpp)

add_executable(${PROJECT_TEST_NAME} ${TEST_SRC_FILES} ${ALL_SRC_FILES})

add_dependencies(${PROJECT_TEST_NAME} googletest)

include_directories(${GTEST_INCLUDE_DIRS}
            ${COMMON_INCLUDES}
)

link_directories(${COMMON_LIB})

target_link_libraries(${PROJECT_TEST_NAME}
        xz
        ${GTEST_LIBS_DIR}/libgtest.a
        ${GTEST_LIBS_DIR}/libgtest_main.a
)
add_test(test1 ${PROJECT_TEST_NAME})

运行make时,ld无法找到xz库。我认为这是由ExternalProject引起的。如果我将ExternalProjects保留在主文件夹中,一切正常,而我的文件夹结构则不起作用。我也尝试过添加 add_dependencies(${PROJECT_TEST_NAME} googletest xz)但它不起作用,cmake无法找到项目xz。 add_dependencies(${PROJECT_TEST_NAME} googletest project_xz)下载了该库并对其进行了编译,但无论如何ld无法找到它,即使link_directories(${COMMON_LIB})也是如此。

0 个答案:

没有答案