gpt4 book ai didi

c++ - 如何告诉 CMake 将可执行文件链接到库?

转载 作者:搜寻专家 更新时间:2023-10-31 01:34:11 24 4
gpt4 key购买 nike

我的源码树是

  • CMakeLists.txt
    • 演示
      • CMakeLists.txt
      • 来源,标题。
    • 图书馆
      • CMakeLists.txt
      • 来源,标题。

所以 Demo 是用 add_executable() 完成的可执行文件而 Library 显然是图书馆。这两者如何联系在一起?

因为现在我正在使用 target_link_libraries(Demo Library) 但我收到错误:LNK2019,我认为这是由于未成功链接造成的。

有什么想法吗?

谢谢。

编辑:在根 CMakeLists.txt 上,将这两个添加为 -> add_subdirectory(Library) add_subdirectory(Demo)。

编辑2:
演示 CMakeLists

add_executable(Demo ${Headers}
${Source})

target_link_libraries(Demo ${blahblah}
${Library})

库 CMakeLists

add_library(Library blahblah.cpp
blahblah.h
foo.cpp
foo.h)

最佳答案

这是不正确的:

target_link_libraries(Demo ${blahblah}
${Library})

你说的是使用名为 Library 的字符串变量,它不会存在。

使用:

target_link_libraries(Demo ${blahblah}
Library)

这样 CMake 就会知道您引用的是目标“库”而不是变量。

关于c++ - 如何告诉 CMake 将可执行文件链接到库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40048249/

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