Reputation: 464
I am trying to build a libary with the following directory structure:
lib1
|-include
| |-lib1
| |-public.h
|-src
| |-private.h
| |-private.cpp
| |-public.cpp
|-lib
| |-liblib1.a
|-CMakeLists.txt
were the public header includes the private header to use its functionality.
My CMakeLists.txt
looks as follows:
project(math)
set(SRC_FILES src/private.cpp src/public.cpp)
add_library(${PROJECT_NAME} STATIC ${SRC_FILES})
target_include_directories(
${PROJECT_NAME}
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include"
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/src"
)
set_target_properties(
${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib"
)
But when I try to build the libary, I get an error from public.h
saying that there is not file private.h
When googling I either find quite old posts that don't use target_include_directories
or they support an unclean structure by putting all files in the same directory
Upvotes: 2
Views: 1941
Reputation: 238361
If you include a header, then you must tell where that header is. If you say that the header is private, then you aren't telling where the header is to the dependencies. When the dependencies include the private header through the public one, they don't know where it is.
Solution: Make all headers included by a public header to be public. Or to phrase it another way: Do not include private headers in public ones.
Upvotes: 5
Reputation: 5510
were the public header includes the private header to use its functionality.
You can't do that. Think of it that way: If you were to make a binary-only release of your library, all you'd ship are the public headers and the resulting .a/.so files of your library, but not the private headers and .cpp files.
A user of your library that would try to include the header public.h
then would get a compilation error because the private header included by public.h
cannot be found since it wasn't part of the release.
Upvotes: 2