我如何修复这个cmake文件? - 链接到导入库的问题 [英] How do I fix this cmake file? - problem linking to imported library

查看:119
本文介绍了我如何修复这个cmake文件? - 链接到导入库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm just starting out with cmake, and trying to set up a fairly simple project. Although the project itself is simple, it links to a number of static libraries which aren't built by cmake. They could be, I suppose - they are my libraries - but I need to sort out how to link to third party libraries anyway.

这是我到目前为止。 ..

Here is what I have so far...

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-NOTFOUND sh_core2-NOTFOUND sh_genlib-NOTFOUND

The problem is that the three libraries that I'm trying to link to aren't being referenced correctly in the generated project file. They are listed as sh_core-NOTFOUND, sh_core2-NOTFOUND and sh_genlib-NOTFOUND.

或许我不需要 link_directories 从上面,但我需要一个 find_library 命令。但我已经快速看看这个命令在文档和... WTF!我已经有一个头痛,我真的不能应付那种大量的看似冗余的复杂ATM。此外,看到这么复杂的东西,应该是完全简单的建议,我在寻找错误的地方。

I think perhaps I don't need the link_directories from above, but I do need a find_library command. But I've taken a quick look at that command in the docs and... WTF!!! I already have a headache, and I really can't cope with that mass of seemingly redundant complexity ATM. Besides, seeing this much complexity for something that should be perfectly simple suggests to me that I'm looking in the wrong place.

所以...我如何告诉cmake在哪里可以找到这些库?

So... how do I tell cmake where to find those libraries?

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

Bonus question - how do I set this up so the generated project handles both a debug build and a release build? Note - the release versions of the imported libraries have the same filenames, but are in a "../../cpplib/_vc_release" folder.

推荐答案

我认为你误解了 include_directories add_library 指令。

I think you've misunderstood the include_directories and add_library directives.

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

include_directories adds directories to be searched for include files, while add_library can be used like this (in your case):

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

但是,如果我正确理解你要完成什么,这样的东西应该这样做:

However, if I correctly understand what you're trying to accomplish, something like this should do it:

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})


如果您还想包含一些标题,它的简单如下:

If you also want to include some headers, it's as simple as:

find_path(CPPLIB_INCLUDE_DIR "my_header.h"
    PATHS ${CPPLIB_HINT_INCLUDE_DIR})

...

include_directories(${CPPLIB_INCLUDE_DIR})

这篇关于我如何修复这个cmake文件? - 链接到导入库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆