Wang Lihe
Wang Lihe

Reputation: 95

how to write a cmake file that can run test depend on same level lib

Suppose that my project dirs like this:

src
|
|---lib1
|    |
|    |--lib1.cpp
|    |--lib1.hpp
|    |--lib1test.cpp
|
|---lib2
|    |
|    |--lib2.cpp
|    |--lib2.hpp
|    |--lib2test.cpp
|
|main.cpp

Now the lib1 has developed,and lib2 is on developing. Lib2 uses some features in lib1.I need do some test for lib2, this means when build lib2 tests under lib2, it should build lib1 first. How can I write three CMakeLists.txt files that lib2 test build depend on same level dir lib1?

Upvotes: 1

Views: 756

Answers (1)

beduin
beduin

Reputation: 8263

You didn't mention are your libs static or dynamic. I'll assume they are both static (but it really doesn't matter). You should simply do something like this:

//core CMakeLists.txt

#...
add_subdirectory(lib1)
add_subdirectory(lib2)

//lib1 CMakeLists.txt

add_library(lib1 lib1.cpp)
target_link_libraries(lib1 ${lib1_deps})

//lib2 CMakeLists.txt

add_library(lib2 lib2.cpp)
add_dependencies(lib2 lib2.cpp) #actually don't need this line
target_link_libraries(lib2 lib1 ${lib2_deps})

add_executable(lib2test lib2test.cpp)
target_link_libraries(lib2test lib2)

Upvotes: 1

Related Questions