0

Let's say I have a project directory where it has an include folder that has headers files (.hpp) and a src folder (.cpp). I am using visual studio code. How can I instead of writing #include "../include/Person.hpp" write #include "Person.hpp"? My project has a structure like this:

project-name
     |
     |--src -- Main.cpp & Person.cpp
     |
     |--include -- Person.hpp

My Person.cpp has the implementations of Person.hpp. My main file looks like this:

#include <iostream>
#include "../include/Person.hpp"
int main() {
    Person p("Allison");
    std::cout << p.name << std::endl;
    return 0;
}

Orville Daley
  • 41
  • 1
  • 4
  • 1
    Possible duplicate: https://stackoverflow.com/questions/37522462/visual-studio-code-includepath https://stackoverflow.com/questions/57458423/vscode-not-recognizing-includes-from-includepath https://stackoverflow.com/questions/52910102/vscode-c-task-json-include-path-and-libraries – Jerry Jeremiah Nov 17 '20 at 00:56
  • 2
    It looks like you need to add the include paths twice: once in c_cpp_properties.json for intellisense and once in tasks.json for the compiler to build it. – Jerry Jeremiah Nov 17 '20 at 01:02
  • g++ -l ./include ./src/main.cpp -o ./bin/main.o gives fatal error: "Person.hpp": No such file or directory – Orville Daley Nov 17 '20 at 01:10
  • Should it have been `../Include` instead of `./Include` in the above comment? Also remember this path has to be relative to the `main.cpp` file I believe. – drescherjm Nov 17 '20 at 01:35
  • Tried that too... the same error – Orville Daley Nov 17 '20 at 01:41
  • Guess there is no way but to write the full relative include path in the source file – Orville Daley Nov 17 '20 at 01:41
  • I was able to do a toy example of this using `g++ -I ./include/ ./src/main.cpp ./src/Person.cpp -o ./bin/test_app` with your described folder structure with just `#include "Person.hpp"` in my source files, running `g++` from the enclosing directory that contains `bin`, `src`, and `include` as folders. No fatal error. – Nathan Pierson Nov 17 '20 at 02:28
  • @NathanPierson That actually worked.Thanks – Orville Daley Nov 17 '20 at 16:12

0 Answers0