【问题标题】:Setting Visual Studio Project Dependencies from CMake从 CMake 设置 Visual Studio 项目依赖项
【发布时间】:2011-03-16 21:39:12
【问题描述】:

我正在使用 CMake 构建一个由四个项目组成的应用程序:

  • a.dll,没有依赖关系
  • b.dll,依赖于a.dll
  • c.exe,依赖于a.dllb.dll
  • d.exe,依赖于a.dllb.dll

每个项目(或 CMake 术语中的目标)都位于其自己的子目录中,并且有自己的 CMakeLists.txt 文件。此外,还有一个顶级 CMakeLists.txt 文件将它们全部绑定(使用add_subdirectory)。

b.dllc.exed.exeCMakeLists.txt文件中,我使用target_link_libraries 将这些目标链接到a.dllb.dll 的导入库。这工作正常。到目前为止,一切顺利。

现在,在 Visual Studio 中,CMake 似乎没有在解决方案的各个项目之间设置任何“项目依赖关系”:如果我转到 Project -> Project Dependencies...,我可以看到确实 b.dll 不依赖于 a.dllc.exe 不依赖于既不是 a.dll 也不是 b.dll 等。结果是项目的构建顺序本质上是随机的,实际上解决方案根本无法构建(因为,比如c.exe是在a.dll之前构建的,所以对应的导入库a.lib还没有生成) .

我很困惑。我一直在非常彻底地寻找答案,但没有运气(这让我认为这对大多数 CMake 用户来说不是问题,而且我一定做错了什么)。

请注意,我也尝试使用 add_dependencies 命令,但没有效果。

我正在使用 CMake 2.8.1 和 Visual Studio 2008 (9.0)。

谢谢, 弗朗茨


这里是 CMakeLists.txt 文件,供参考:

顶级CMakeLists.txt

add_subdirectory(a)
add_subdirectory(b)
add_subdirectory(c)
add_subdirectory(d)

a/CMakeLists.txt

add_library(a SHARED a.cpp)

b/CMakeLists.txt

add_library(b SHARED b.cpp)
target_link_libraries(b a)
# also tried to add add_dependencies(b a) here

c/CMakeLists.txt

add_executable(c c.cpp)
target_link_libraries(c a b)
# also tried to add add_dependencies(c a b) here

d/CMakeLists.txt

add_executable(d d.cpp)
target_link_libraries(d a b)
# also tried to add add_dependencies(d a b) here

【问题讨论】:

  • target_link_libraries 是正确的命令。您能否确认您使用的是库/可执行文件名称而不是项目名称。
  • 我在问题中描述的设置实际上是正确的,并且确实按预期工作。看起来我的问题是由于对我的 CMakeLists.txt 文件进行大量调整后缓存污染/去同步造成的(特别是,我感觉将 project 命令从一个 将 CMakeLists.txt 文件添加到父目录中的文件会严重破坏缓存...)。无论如何,感谢您的评论。

标签: dll dependencies cmake


【解决方案1】:

我的问题中描述的设置是正确的,并且可以按预期工作。请参阅我上面的评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    相关资源
    最近更新 更多