2
votes

I have a CMake project with the following directory structure:

  • CMakeLists.txt
  • A/CMakeLists.txt
  • B/CMakeLists.txt

A and B both describe shared libraries, and B depends upon A.

Calling cmake on the entire project gives no problems. Neither does building A, but building B tells me it misses symbols from A. This makes sense, and so I solved that by adding target_link_libraries(B A) to B/CMakeLists.txt.

However, when I call cmake now, I get errors about B/CMakeLists.txt not being able to find source files from A. Apparently, to solve this, I should add target_include_directories(A PUBLIC .) to A/CMakeLists.txt, but that does not work. What am I missing here?


For completion's sake, here's the dumbed-down CMake files:

cmake_minimum_required (VERSION 3.5.1)

project(main C CXX)

add_subdirectory(A)
add_subdirectory(B)

A/CMakeLists.txt

cmake_minimum_required (VERSION 3.5.1)

add_library(A SHARED "")

target_include_directories(A PUBLIC .)
target_sources(A PUBLIC ...)

B/CMakeLists.txt

cmake_minimum_required (VERSION 3.5.1)

add_library(B SHARED "")
target_link_libraries(B A)
target_sources(B PUBLIC ...)

The error I get is the following

CMake Error at B/CMakeLists.txt:3 (add_library): Cannot find source file:

Access.hpp

Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx


I'm following this tutorial, explaining about target_link_libraries and target_include_directories.

1
In add_library() you specify source files (*c, *.cpp) to build library from. What is Access.hpp header is doing there?Velkan
It's there because I'd like to see the headers turn up in the generated XCode and CodeBlocks projects. If I leave the headers out, the same error throws up the first .cpp file (BinPackingSpace.cpp).Stijn Frishert
It's in the same directory as that B/CMakeLists.txt file?Velkan
Good question, and no: That's a file from A. The error throws for B/CMakeLists.txt, but it's about a source file from A it says it can't find.Stijn Frishert
Then don't do that. Don't compile files from A into the B library.Velkan

1 Answers

2
votes

Don't use target_sources(). Add source files to the add_library() command.

Or use target_sources() with PRIVATE.