cmake 链接到ExternalProject生成的库时,“使用无效参数调用了target_link_directories”

atmip9wb  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(158)

CMake项目构建外部库:

include(ExternalProject)

set(EXTLIBS_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/extlibs)

ExternalProject_Add(ext-somelib
    URL https://github.com/.../archive/refs/tags/somelib.tar.gz
    CMAKE_ARGS
        -DCMAKE_INSTALL_PREFIX=${EXTLIBS_INSTALL_PREFIX}
        -DCMAKE_PREFIX_PATH=${EXTLIBS_INSTALL_PREFIX}
)

include_directories(${EXTLIBS_INSTALL_PREFIX}/include)

add_executable(mainTarget ...)

add_dependencies(mainTarget ext-somelib)

target_link_directories(mainTarget ${EXTLIBS_INSTALL_PREFIX}/lib)
target_link_libraries(mainTarget somelib)
# (link <build>/extlibs/lib/libsomelib.so)

字符串
当配置项目时,它会失败并显示错误:
CMakeLists.txt中的CMake错误:#(target_link_directories):
使用无效参数调用target_link_directories
为使用ExternalProject构建的依赖项指定链接目录的正确方法是什么(因此在配置时还不可用)?

vxf3dgd4

vxf3dgd41#

在命令target_link_directories中,需要关键字PUBLIC、PRIVATE或INTERFACE之一。
正确:

target_link_directories(mainTarget PRIVATE ${EXTLIBS_INSTALL_PREFIX}/lib)

字符串

相关问题