gpt4 book ai didi

build - CMake:如何链接静态 3rd 方库?

转载 作者:行者123 更新时间:2023-12-04 17:39:44 28 4
gpt4 key购买 nike

我所拥有的:我的代码(简单的 main.cpp)、第 3 方库的 header (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 找到了第三方 header 。说到链接 ld提示找不到 -llib_misc 和 -llib_tools。
删除“link_directories”行或指定 libs 绝对名称会导致“没有规则可以制作目标/path/to/lib_misc.a”

那么如何告诉 CMake 仅将这些库用于链接呢?

最佳答案

link_directories($ENV{TPLIB_BINARY_PATH})应该放在ADD_EXECUTABLE之前.

来自 the official documents of CMake , link_directories上有注释:

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

关于build - CMake:如何链接静态 3rd 方库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059532/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com