首页 > 解决方案 > 有没有办法将库更改添加为 cmake 中的依赖项?

问题描述

我有一个使用 CMake 构建的应用程序和一个库,如果可能的话,我想简化构建过程。就目前而言,如果库发生更改并且我重建应用程序,CMake 不会重建修改后的库文件。同样,如果我重建库然后make在应用程序上运行,它会说无事可做。我目前的解决方法是使用 bash 脚本来重建所有内容,但这会不必要地重新编译大量文件,如果可能的话,我想在应用程序的构建目录中处理它们。

用简单的例子更新:

有 3 个文件夹:applibincludeinclude包含test_program.hlib包含test_program.cppapp包含test.cpp其中包含test_program.h

这是CMakeLists.txtfor lib

include_directories(.)
add_library (test_lib STATIC
    test_program.cpp
)

这是CMakeLists.txtfor app

include_directories(
    .
    ../include
)

link_directories(
    ../../lib/build
)

add_executable(test_exe
    test.cpp
)

target_link_libraries(
    test_exe
    test_lib
)

我想这样做,test_program.cpp如果我对.cmake ../makeapp/buildtest_program.cpp

更新:我在项目的顶层添加了一个顶层CMakeLists.txt和一个build。该文件非常简单,似乎从一开始就是我想要的:

add_subdirectory(app)
add_subdirectory(lib)

如果这里有任何改进,我很乐意接受建议。

标签: c++cmake

解决方案


推荐阅读