I'm trying to create a simple library using cmake and create an app that uses it. However, while creating an app, I must specify the path to the header file used in the library for the build to succeed. I want to create an app with only the library without the header file path.
create library
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(mylibrary)
include_directories("/path/to/src")
add_library(mylib SHARED ./foo.cpp ./foo.h)
target_include_directories(mylib PRIVATE "/path/to/src")
target_link_directories(mylib PRIVATE "/path/to/src)
set_property(TARGET mylib PROPERTY CXX_STANDARD 17)
create app
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(myapp)
add_executable(test ./main.cpp)
# target_include_directories(test PRIVATE "/path/to/src")
target_link_libraries(test PRIVATE "/path/to/src/libmylib.dylib")
target_link_directories(test PRIVATE "/path/to/src")
set_property(TARGET test PROPERTY CXX_STANDARD 17)
and include header in main.cpp
#include "foo.h"
int main(int argc, const char** argv) {
return 0;
}
If i build like this An error occurs.
'foo.h' file not found #include "foo.h"
but
target_include_directories(test PRIVATE "/path/to/src")
If i add this command and specify the path to the header file, the build will proceed normally. This applies not only to the libraries I created but also to external libraries.
I thought that as long as I had a library, I could import and use the header files within it. And I want to use it like that.
Is there a way to use the library without the header file path??
CMake knows how to link with the output of another CMake library target. You can add your library to the build using
add_subdirectory, which makes yourmyliblibrary target available in the executable project:The nice thing about this is that it automatically propagates required build flags to your main app, including the include paths. However, that only happens for include paths that are
PUBLIC, notPRIVATE, so in your library project you'll want:Full example:
mylib/CMakeLists.txt:myapp/CMakeLists.txt: