简体   繁体   English

如何告诉CMake对库链接可执行文件?

[英]How to tell CMake to link executable against library?

My source tree is 我的源代码树是

  • CMakeLists.txt 的CMakeLists.txt
    • Demo 演示
      • CMakeLists.txt 的CMakeLists.txt
      • source, headers. 源,标题。
    • Library 图书馆
      • CMakeLists.txt 的CMakeLists.txt
      • source, headers. 源,标题。

So Demo is the executable which is done with add_executable() and Library is obviously the library. 因此,演示是可通过add_executable()完成的可执行文件,而库显然是库。 How can those 2 be linked together? 这两个如何连接在一起?

Because right now I am using target_link_libraries(Demo Library) but I am getting Error: LNK2019 which I think is caused by not linking successfully. 因为现在我正在使用target_link_libraries(Demo库),但出现错误:LNK2019,我认为是由于未成功链接所致。

Any ideas? 有任何想法吗?

Thanks. 谢谢。

edit: On root CMakeLists.txt those 2 are added as -> add_subdirectory(Library) add_subdirectory(Demo). 编辑:在根CMakeLists.txt上,将这2个添加为-> add_subdirectory(Library)add_subdirectory(Demo)。

edit2: EDIT2:
Demo CMakeLists 演示CMakeLists

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

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

Library CMakeLists 图书馆CMakeLists

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

This is incorrect: 这是不正确的:

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

What you are saying is to use the string variable called Library, which won't exist. 您要说的是使用名为Library的字符串变量,该变量将不存在。

Use: 采用:

target_link_libraries(Demo ${blahblah}
                               Library)

so that CMake will know you are referencing the target "Library" and not a variable. 这样CMake就会知道您正在引用目标“库”而不是变量。

In your example above, Library is a target name, not a variable. 在上面的示例中,“库”是目标名称,而不是变量。 When you link it to your executable, use target_link_libraries(Demo Library) (no '${}' around 'Library'). 将其链接到可执行文件时,请使用target_link_libraries(Demo Library) (“库”周围没有“ $ {}”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A - CMake: How to link library A to library B, then link executable to library A CMAKE 如何告诉 makefile 链接库? - How does CMAKE tell the makefile to link a library? 如何使用 CMake FetchContent 将调试可执行文件与发布库链接? - How to link debug executable with release library using CMake FetchContent? CMAKE:构建可执行文件和库并将其链接 - CMAKE: Build executable and library and link them CMake-仅在需要时将库链接到可执行文件 - CMake - Only link a library to an executable if it is needed 无法在 CMake 中创建动态库并将其链接到另一个动态库? - Unable to create a dynamic library in CMake and link it against another dynamic library? 如何使用CMake选择性地链接静态或动态boost库? - How can I optionally link against static or dynamic boost library using CMake? CMake,如何在调试模式和发布模式下将动态库与可执行文件链接? - CMake, How I can link a dynamic library with an executable in debug mode and release mode? 你如何告诉 CMake 静态链接到使用 find_package 找到的 package 中的库? - How do you tell CMake to statically link to a library in a package found with find_package? 如何使用Cmake链接Android项目与libandroid? - How to link Android project against libandroid with Cmake?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM