简体   繁体   中英

How to create dependencies in CMake

I'm trying to improve my project layout.

Here's (some of) my CMakeLists.txt files:

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 includes banded.h , so anything that needs lu will also need banded . This requires redundancy in my project as seen above. Is there a way that I don't have to add banded.cpp every time I add lu.cpp ?

@Amadeus's answer works, but I think a better answer is to take the common files and move them into a library:

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM