如何修复此cmake文件? - 链接到导入库的问题

时间:2010-06-23 08:58:45

标签: cmake

我刚刚开始使用cmake,并试图建立一个相当简单的项目。虽然项目本身很简单,但它链接到许多不是由cmake构建的静态库。我想它们可能是 - 我们是我的图书馆 - 但我还是需要弄清楚如何链接到第三方图书馆。

这是我到目前为止所拥有的......

cmake_minimum_required(VERSION 2.8.1)
cmake_policy(VERSION 2.8.1)

project( test01 )

include_directories("../../cpplib/sh_core" "../../cpplib/sh_core2" "../../cpplib/sh_genlib")

link_directories("../../cpplib/_vc_debug")

add_library( sh_core   STATIC IMPORTED )
add_library( sh_core2  STATIC IMPORTED )
add_library( sh_genlib STATIC IMPORTED )

add_executable( test01 test01 test01_ast test01_parse test01_scan test01_main )
target_link_libraries(test01 sh_core sh_core2 sh_genlib)

问题是我尝试链接的三个库未在生成的项目文件中正确引用。它们列为sh_core-NOTFOUNDsh_core2-NOTFOUNDsh_genlib-NOTFOUND

或许我不需要上面的link_directories,但我确实需要一个find_library命令。但是我已经快速浏览了文档中的那个命令...... WTF!我已经头疼了,我真的无法应对那些看似多余的复杂ATM。此外,看到这种非常复杂的东西应该非常简单,这告诉我,我正在寻找错误的地方。

那么......我怎么告诉cmake在哪里找到这些库?

奖金问题 - 我如何设置它以便生成的项目处理调试版本和发布版本?注 - 导入库的发行版具有相同的文件名,但位于“../../cpplib/_vc_release”文件夹中。

2 个答案:

答案 0 :(得分:6)

我认为您误解了include_directoriesadd_library指令。

include_directories添加要搜索包含文件的目录,而add_library可以像这样使用(在您的情况下):

add_library(core UNKNOWN IMPORTED)
set_target_properties(core PROPERTIES IMPORTED_LOCATION "../../cpplib/_vs_release/core.lib")

但是,如果我正确理解你想要完成的事情,那么应该这样做:

set(CPPLIB_DIR "${CMAKE_SOURCE_DIR}/../../cpplib")

set(CPPLIB_DEBUG_DIR "${CPPLIB_DIR}/_vc_debug")
set(CPPLIB_RELEASE_DIR "$(CPPLIB_DIR}/_vc_release")

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    set(CPPLIB_LIB_HINT ${CPPLIB_RELEASE_DIR})
else ()
    set(CPPLIB_LIB_HINT ${CPPLIB_DEBUG_DIR})
endif ()

find_library(CPPLIB_CORE_LIBRARY NAMES "core"
                                 PATHS ${CPPLIB_LIB_HINT})

find_library(CPPLIB_CORE2_LIBRARY NAMES "core2"
                                  PATHS ${CPPLIB_LIB_HINT})

find_library(CPPLIB_GENLIB_LIBRARY NAMES "genlib"
                                   PATHS ${CPPLIB_LIB_HINT})

if (("${CPPLIB_CORE_LIBRARY}" STREQUAL "CPPLIB_CORE_LIBRARY-NOTFOUND") OR
    ("${CPPLIB_CORE2_LIBRARY}" STREQUAL "CPPLIB_CORE2_LIBRARY-NOTFOUND") OR
    ("${CPPLIB_GENLIB_LIBRARY}" STREQUAL "CPPLIB_GENLIB_LIBRARY-NOTFOUND"))
    message(FATAL_ERROR "One of the libs wasn't found!")
endif ()

set(CPPLIB_LIBRARIES ${CPPLIB_CORE_LIBRARY} ${CPPLIB_CORE2_LIBRARY} ${CPPLIB_GENLIB_LIBRARY})

target_link_libraries(my_exe ${CPPLIB_LIBRARIES})

修改 如果您还想要包含一些标题,那么它就像:

一样简单
find_path(CPPLIB_INCLUDE_DIR "my_header.h"
    PATHS ${CPPLIB_HINT_INCLUDE_DIR})

...

include_directories(${CPPLIB_INCLUDE_DIR})

答案 1 :(得分:1)

跟进the_void's answer

您应该能够简化if测试表达式,其中包含:

if (("${CPPLIB_CORE_LIBRARY}" STREQUAL "CPPLIB_CORE_LIBRARY-NOTFOUND") OR
    ("${CPPLIB_CORE2_LIBRARY}" STREQUAL "CPPLIB_CORE2_LIBRARY-NOTFOUND") OR
    ("${CPPLIB_GENLIB_LIBRARY}" STREQUAL "CPPLIB_GENLIB_LIBRARY-NOTFOUND"))
    message(FATAL_ERROR "One of the libs wasn't found!")
endif ()

是这样的:

if (CPPLIB_CORE_LIBRARY OR
    CPPLIB_CORE2_LIBRARY OR
    CPPLIB_GENLIB_LIBRARY)
    message(FATAL_ERROR "One of the libs wasn't found!")
endif ()

参考CMake's if statement