CMake: C++ linting not recognising include directory

60 Views Asked by At

I am very new to CMake and I am trying to setup my C++ project workspace so that all of the ".h" files are in a "includes" directory while all of the ".cpp" files are in the "sources" directory. The file tree looks something like this:

.
└── project/
    ├── CMakeLists.txt
    ├── main.cpp
    ├── includes/
    │   ├── test.h
    │   └── ...
    └── sources/
        ├── test.cpp
        └── ...

My goal is that I would like to be able to call #include "test.h" directly in main.cpp without the need of having to give it the relative path, i.e. avoid having to write #include "includes/test.h". The relevant files are as follows:

CMakeLists.txt

cmake_minimum_required(VERSION 3.21)
project(MyProject)

# Set C++ standard
set(CMAKE_CXX_STANDARD 23)

# Include directories
set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/includes" )
include_directories(${INCLUDE_PATH})

# Source files
file(GLOB SOURCES "sources/*.cpp")

# Create executable
add_executable(${PROJECT_NAME} main.cpp ${SOURCES})
target_include_directories( ${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )

main.cpp

#include "test.h"

int main(){
    messenger::print_header();
}

test.cpp

#include <iostream>

#include "test.h"

void messenger::print_header(){
    std::cout << "INSIDE HEADER" << std::endl;

}

test.h

namespace messenger {
    void print_header();
}

This combination compiles fine and I can run the program but my lsp still complains that the test.h file is not being found. This error appears in both main.cpp and test.cpp when I use just #include "test.h"

main.cpp

test.cpp

The warnings nicely disappear when I switch to relative paths to the includes directory.

main.cpp (no errors)

test.cpp (no errors)

I am using the clangd lsp installed using the mason-lspconfig package for neovim. Is this a problem with the configuration for the lsp or a CMake issue?

0

There are 0 best solutions below