Issue
The project has the following structure:
.
|- CMakeList.txt
`- src
|- lib
| |- CMakeList.txt
| `- libA
| |- CMakeList.txt
| |- libA.c
| `- libA.h
`- main
|- CMakeList.txt
`- main.c
and I want to include the libA.h
file in main.c
using #include "libA.h"
, but an error occurs when trying to compile fatal error: hello.h: No such file or directory
. Can I make cmake add the appropriate include flags and I could write #include "libA.h"
.
I understand that I can manually add flags like -Ipath/to/src/lib/libA -Ipath/to/src/lib/libB ...
, but I would like to automate this process.
Solution
Solved.
If I create library
add_library(MyLib src/MyLib.cpp include/MyLib.h)
target_include_directories(MyLib PUBLIC include)
then I can write
add_executable(MyExe ${SOURCES} ${HEADERS})
target_link_libraries(MyExe MyLib)
The last line will automatically add the flag '-I/path/to/MyLib/include' when compiling MyExe
.
Answered By - Alexey Ismagilov