2

I can run and compile with no problem, but VSCode Intellisense is saying that it cannot open source file boost/asio.hpp (even tho it obviously can) and is marking it as an error all the time, I want to know why its doing it and how to fix it

I have the following directory:

root/
  include/
    atr_include.hpp
    user_interface.hpp
  src/
    user_interface.cpp
    CMakeList.txt
  main.cpp
  CMakeList.txt

With CMakeList.txt:

#CMake minimum version
cmake_minimum_required(VERSION 3.0.0)


#C++ Standard version
set(CMAKE_CXX_STANDARD 20)

### Searches for the VCPKG
if(DEFINED ENV{VCPKG_ROOT})
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
endif()

project(main LANGUAGES CXX VERSION 0.1.0 )

find_package(Boost COMPONENTS system json REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

include_directories(include)
link_directories(src)
add_subdirectory(src)
link_libraries(atr_lib)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
endif()

if(MSVC OR MSYS OR MINGW)
    target_link_libraries(${PROJECT_NAME} ws2_32)
endif()

src/CMakeList:

add_library(atr_lib STATIC user_interface.cpp)

include/atr_include.hpp:

#include <boost/asio.hpp>
#include <chrono>
#include <iostream>
#include <math.h>
#include <mutex>
#include <thread>

include/user_interface.hpp:

#include "atr_include.hpp"

class UserInterface
{
  private:
  public:
};

src/user_interface.cpp:

#include <user_interface.hpp>

main.cpp:

#include <user_interface.hpp>

int main() { return 0; }
igorgsouza
  • 21
  • 1
  • 4

2 Answers2

10

I had the same problem and it was related to the incorrect configuration of the IntelliSense Configuration Provider item.

"configurations": [
    {
        ...
        "configurationProvider": "ms-vscode.cmake-tools"
        ...        
    }

The previous value corresponded to another Cmake extension that was no longer installed. After changing the value to ms-vscode.cmake-tools (I have the "CMake Tools" extension installed), IntelliSense started working correctly. Also boost headers includes correctly without errors from VS Code.

rdN
  • 111
  • 1
  • 4
  • 2
    You can also set it with the command palette (Shift + F1 -> `>C++: Change Configuration Provider` -> and select `CMake Tools`) – Guy Shefer Nov 15 '22 at 14:48
-1

VSCode is not a C++ compiler. It is (mostly) a text editor, and some development tools. It merely runs some C++ compiler in order to actually compile and build C++ code.

Your actual C++ compiler is the final and ultimate authority on where it can find various header files.

For whatever reason, your VSCode configuration does not know where boost's header files are installed, so that's what it tells you. Your compiler knows, but that's your compiler, not VSCode.

So, no, VSCode really can't open or find these header files. Your question was:

I want to know why its doing it

Because it simply doesn't know. It's not your C++ compiler, and it doesn't automatically know where all header files are installed.

and how to fix it

That ...depends on your very, very specific computer, operating system (VSCode is available for multiple operating systems), whichever C++ compiler you are actually using with VSCode, and how exactly you are using VSCode (for a brief time, for example, I used VSCode to edit files that weren't even on the same computer, but accessed via ssh). Each one of these factors affects the how everything works, together.

If you know where boost's header files are installed, this may simply be a matter of digging through VSCode's configuration settings, there must be a setting somewhere that tells boost which directories contain header files to search for. If you don't know where boost's header files are installed, your first step will be to figure that part out, before anything else can be done.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • This is weird because I feel like I already told VSCode where it is, including the "Forced Include Path" for C/C++ configurations and it still tells me its wrong – igorgsouza Jun 29 '22 at 00:58