How to remove error alert of header-only cmake project in vscode

Viewed 27

When I develop a project that output is a shared library or executable, normally, I will use include_directories in the CMakeLists.txt of root directory. In such case, before I include a header within a translate unit, I can't use angle brackets to let headers include each other. I think it is because files in include_directories is not a part of project. I can fix this by using relative path, but when I have to import external project, this can not fixed easily.

Assume that I have a external project with a root CMakeLists.txt, which has this snippet

add_library(mylib INTERFACE)
target_include_directories(mylib INTERFACE .)

and the directory seems like this

└──external
   ├───mylib.hpp
   └───CMakeLists.txt

I want to import this project, and I'm writing a header-only project. The file tree seems like this

├──external (above)
├──include
|  ├──header.hpp
|  └──CMakeLists.txt
└──CMakeLists.txt

The CMakeLists.txt in root directory is simply add_subdirectory(include), and here is the CMakeLists.txt in folder include

add_library(Header INTERFACE)
target_include_directories(Header INTERFACE .)
target_link_libraries(Header INTERFACE mylib)

Theoretically, I can now include mylib.hpp by #include <mylib.hpp>, but it is not the case. VSCode tells me it can't find this header file. As much as I know, the only way to solve this is add a translate unit and let the implementation code include the header.hpp. I have tested this several time. VSCode won't report this error as long as I add a cpp file and include that header, and once I remove the cpp file, the error appears again. By the way, I have set "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" in configuration file of vscode.

Please tell me how to fix this without a translate unit.

0 Answers
Related