我正在尝试改善项目布局。

这是我的CMakeLists.txt文件的一部分:

project(Numerical CXX)

include(cotire)

cmake_minimum_required(VERSION 3.1)

set (CMAKE_CXX_STANDARD 14)

add_executable(hw1 hw1.cpp
    linalg/lu.cpp
    linalg/banded.cpp
)

add_executable(hw2 hw2.cpp
    linalg/cholesky.cpp
    linalg/lu.cpp
    linalg/banded.cpp
)

add_executable(hw3 hw3.cpp
    linalg/solvers-new.cpp
    linalg/cholesky.cpp
    linalg/lu.cpp
    linalg/banded.cpp
)
...


lu.h包括banded.h,因此需要lu的任何内容也将需要banded。如上所述,这需要在我的项目中进行冗余处理。有没有一种方法我不必每次添加banded.cpp都添加lu.cpp

最佳答案

@Amadeus的答案有效,但我认为一个更好的答案是获取常用文件并将其移至库中:

project(Numerical CXX)
include(cotire)
cmake_minimum_required(VERSION 3.1)
set (CMAKE_CXX_STANDARD 14)

add_library(CommonLib STATIC
    linalg/lu.cpp
    linalg/banded.cpp
)

add_executable(hw1
    hw1.cpp
)

target_link_libraries(hw1 LINK_PUBLIC
    CommonLib
)

add_executable(hw2
    hw2.cpp
    linalg/cholesky.cpp
)

target_link_libraries(hw2 LINK_PUBLIC
    CommonLib
)

add_executable(hw3
    hw3.cpp
    linalg/solvers-new.cpp
    linalg/cholesky.cpp
)

target_link_libraries(hw3 LINK_PUBLIC
    CommonLib
)

关于c++ - 如何在CMake中创建依赖项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38285964/

10-12 20:50