I am trying to learn how to better organize a project and it's files in C++.
My simple test project only seems to work whenever main.cpp (and main.exe) is in the root project folder. Here's how I wanted to try to set it up.
Inside of main.cpp, I have #include "headers/header.h"
When I try to compile it with this setup, I get this error fatal error: headers/header.h: No such file or directory
I am wondering why this doesn't work when the following setup, which doesn't have main.cpp in a subfolder, compiles and runs just fine (without changing any of the code).
I am using Visual Studio Code with the C/C++ and C/C++ Compile Run extensions (in case that's relevant).
Edit:
Changing it to "../headers/header.h"
does work, but I am wondering how I would do this without using ../
.
If I'm understanding correctly I just need to add something to the includePath in c_cpp_properties.json. Here's what the "includePath"
section looks like right now.
"includePath": [
"${workspaceFolder}/**"
],
So I think I just need to make it something like
"includePath": [
"${workspaceFolder}/**",
"..."
],
What exactly should I be putting in the ...
part?
When compiling a specific file, your compiler will look at the path of this file. So, in your first example, it is .../Project/main/main.cpp
, but in your second example it's ../Project/main.cpp
. So now its obvious why looking at the path that is headers/header.h
is valid in second case, but not the first one.
However, you can of course leave your main.cpp
in your main
directory if you wish, you'd only need to set INCLUDE_PATH
directory. If you were using Makefile
, it'd be a simple -I
flag addition, but since you're using VS code, you might want to look at this question . Sorry I can't provide more detailed solution, I'm not really familiar with VS Code environment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.