I have a CMake project with the following folder structure:
my_project
├── build
├── CMakeLists.txt
├── hello_test
│ ├── CMakeLists.txt
│ └── main.cpp
└── my_libs
└── hello_lib
├── CMakeLists.txt
├── include
│ └── hello.hpp
└── src
└── hello.cpp
The top level “CMakeList.txt” is as:
cmake_minimum_required(VERSION 3.17.2)
project(my_project)
add_subdirectory(my_libs/hello_lib)
add_subdirectory(hello_test)
The “CMakeList.txt” under “hello_test” is:
add_executable(hello_test main.cpp)
target_link_libraries(hello_test PUBLIC hello-lib)
The “CMakeList.txt” under “my_libs/hello_lib” is:
add_library(
hello_lib SHARED
include/hello.hpp
src/hello.cpp
)
target_include_directories(hello_lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
Here’s the main.cpp:
main.cpp
#include <iostream>
#include <hello.hpp> // <--- cannot be found.
using namespace std;
int main()
{
hello::say_hello();
return 0;
}
The lib has no issue to build. But there’s an issue to build “main.cpp” as the header “hello.hpp” cannot be found. I wonder if there’s wrong the CMakeLists.txt files. Thanks for the help!