CMake:如何链接静态第 3 方库?

发布于 2024-11-29 16:21:19 字数 1651 浏览 1 评论 0原文

我所拥有的:我的代码(简单的 main.cpp)、第 3 方库的标头(EnvVar TPLIB_INCLUDE)、二进制库(TPLIB_BINARY_PATH 中的几个 .a 文件)以及以下 CMakeLists.txt:

# current source directory: CMAKE_CURRENT_SOURCE_DIR
# current binary directory: CMAKE_CURRENT_BINARY_DIR

# require 2.6 to get support for the simple IF construct
CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0)

PROJECT( simpleapp)
SET( PROGNAME simpleapp )

SET( CMAKE_COLOR_MAKEFILE ON )
SET( CMAKE_VERBOSE_MAKEFILE ON )
SET( CMAKE_INCLUDE_CURRENT_DIR TRUE )

# the actual .cpp files go here
SET(project_SOURCES
    main.cpp
)

# add here all files that need processing by Qt's MOC if there are any
set(project_MOC_SOURCES
    # files would go here
)

# add here all files that will be processed by Qt's UIC
set(project_UIS
    # all .ui files would go here
)

# additional Qt resources go here
set(project_RCCS
    # all .qrc files would go here
)

# setup Qt
FIND_PACKAGE(Qt4 REQUIRED)

INCLUDE(${QT_USE_FILE})

QT4_WRAP_CPP(test_MOCS ${project_MOC_SOURCES})
QT4_WRAP_UI(test_UIS_H ${project_UIS})
QT4_WRAP_CPP(test_MOC_UI ${project_UIS_H})
QT4_ADD_RESOURCES(test_RCC_SRCS ${project_RCCS})

include_directories( ${CMAKE_BINARY_DIR} $ENV{TPLIB_INCLUDE})

ADD_EXECUTABLE( ${PROGNAME} ${project_SOURCES} ${test_MOCS} ${test_RCC_SRCS} ${test_MOC_UI} )

link_directories($ENV{TPLIB_BINARY_PATH})

TARGET_LINK_LIBRARIES(${PROGNAME} ${QT_LIBRARIES} lib_misc lib_tools)

make创建 main.cpp.o 的项目照常工作。因此 CMake 找到了第三方标头。当谈到链接ld时,抱怨找不到-llib_misc和-llib_tools。 删除“link_directories”行或指定库绝对名称会导致“没有规则使目标/path/to/lib_misc.a”

那么如何告诉CMake仅使用这些库进行链接呢?

What I have: my code (simple main.cpp), headers of the 3rd party lib (EnvVar TPLIB_INCLUDE), binary lib (several .a files in TPLIB_BINARY_PATH) and the following CMakeLists.txt:

# current source directory: CMAKE_CURRENT_SOURCE_DIR
# current binary directory: CMAKE_CURRENT_BINARY_DIR

# require 2.6 to get support for the simple IF construct
CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0)

PROJECT( simpleapp)
SET( PROGNAME simpleapp )

SET( CMAKE_COLOR_MAKEFILE ON )
SET( CMAKE_VERBOSE_MAKEFILE ON )
SET( CMAKE_INCLUDE_CURRENT_DIR TRUE )

# the actual .cpp files go here
SET(project_SOURCES
    main.cpp
)

# add here all files that need processing by Qt's MOC if there are any
set(project_MOC_SOURCES
    # files would go here
)

# add here all files that will be processed by Qt's UIC
set(project_UIS
    # all .ui files would go here
)

# additional Qt resources go here
set(project_RCCS
    # all .qrc files would go here
)

# setup Qt
FIND_PACKAGE(Qt4 REQUIRED)

INCLUDE(${QT_USE_FILE})

QT4_WRAP_CPP(test_MOCS ${project_MOC_SOURCES})
QT4_WRAP_UI(test_UIS_H ${project_UIS})
QT4_WRAP_CPP(test_MOC_UI ${project_UIS_H})
QT4_ADD_RESOURCES(test_RCC_SRCS ${project_RCCS})

include_directories( ${CMAKE_BINARY_DIR} $ENV{TPLIB_INCLUDE})

ADD_EXECUTABLE( ${PROGNAME} ${project_SOURCES} ${test_MOCS} ${test_RCC_SRCS} ${test_MOC_UI} )

link_directories($ENV{TPLIB_BINARY_PATH})

TARGET_LINK_LIBRARIES(${PROGNAME} ${QT_LIBRARIES} lib_misc lib_tools)

makeing the project creating the main.cpp.o works as usual. Thus CMake found the third party headers. When it comes to link ld complains cannot find -llib_misc and -llib_tools.
Removing the "link_directories" line or specifiingy the libs absolute name leads to "No rule to make target /path/to/lib_misc.a"

So how to tell CMake to use these libs for linking only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-12-06 16:21:19

link_directories($ENV{TPLIB_BINARY_PATH}) 应放置在 ADD_EXECUTABLE 之前。

来自CMake的官方文档,有注释在 link_directories 上:

该命令仅适用于调用后创建的目标。

link_directories($ENV{TPLIB_BINARY_PATH}) should be placed before ADD_EXECUTABLE.

From the official documents of CMake, there are notes on link_directories:

The command will apply only to targets created after it is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文