user674155
user674155

Reputation:

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?

Upvotes: 0

Views: 86

Answers (1)

Caleb
Caleb

Reputation: 930

@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
)

Upvotes: 1

Related Questions