【问题标题】:How to properly organize a CMake project with multiple subdirectories?如何正确组织具有多个子目录的 CMake 项目?
【发布时间】:2021-10-10 06:30:01
【问题描述】:

我有这个项目结构:

我正在尝试正确设置 CMakeList.txt 文件。我的目标只是将库链接到 main.cpp。所以我有这些 CMakeList.txt 文件:

库/CMakeList.txt:

来源/CMakeList.txt:

根:

当我尝试构建项目时,我收到此错误: ld: library not found for -lMyLib

错误在哪里,我应该如何正确组织此类项目的结构?

【问题讨论】:

  • 请勿张贴文字图片。请改为将文本作为文本发布到问题中。

标签: cmake linker static-libraries build-system


【解决方案1】:

你使用了find_library,但你没有使用${MyLib}变量。请参阅find_library 文档。

您使用了link_directories,但它在使用它的目录中是本地的。上层目录不受影响。

使用IMPORTED 库或INTERFACE 库链接.a 文件。请参阅 https://stackoverflow.com/a/10550334/9072753https://stackoverflow.com/a/41909627/9072753 答案。如果它只是一个图书馆,我认为更喜欢IMPORTED。将target_include_directories(Source ...) 移动到library/CMakeLists.txt,而不是Source 将包含目录添加到您的库中,它在概念上是库包含目录。

我建议,主观的,取决于:将target_include_directories(Source.. 移动到source/CMakeList.txt 中。 IE。将所有Source 相关内容保留在source/CMakeLists.txt 中。并且可能将EXCLUDE_FROM_ALL 添加到add_subdirectory(library)

使用set_target_properties 而不是set(CMAKE_CXX_STANDARD。总的来说,更喜欢单独使用 *target* 特定的东西而不是 set(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多