C++ #include relative path - c++

Is it even possible that relative path is correct, but that I nevertheless get the compilation error:
2>src\wfbuilderapp.cpp(15): fatal error C1083: Cannot open include file: '../project/include/public/core/paths.h': No such file or directory
In particular, is it possible that error occurs not because path is wrong, but because the included file "does not permit" that I have this binding between files ? The file where I am including and the included file are located in the same solution, in subfolders of folders located in different projects.
Here are my questions about #include:
Can I include any file as long as path is correct and included file is in the same solution ?
Can I include .cpp file ?
Do i sometimes need to include both .cpp and .h files ?

Can I include any file as long as path is correct and included file is in the same solution ?
If the path is correct, yes. It being in the same solution has no effect on the include. The compiler will look for the file in the places it's told to look (adding a file to a solution doesn't modify those places).
The proper way is adding the path to the Additional Include Directories in your Project properties.
Can I include .cpp file ?
Yes, but shouldn't, unless it's for bulk builds.
Do i sometimes need to include both .cpp and .h files ?
No. You can, but don't need to. You sometimes need to include headers, if the full definition of something defined in the header is needed. As for cpp files, see previous answer.

Can I include any file as long as path is correct
Yes. Note that I snipped the solution part.
Can I include .cpp file ?
Can, yes. Should? No.
Do I sometimes need to include both .cpp and .h files
If you "need" to do so, you are using the wrong tool for a problem. cpp files should never be #include'ed.

Can I include any file as long as path is correct and included file is
in the same solution ?
You can include any file you like, same solution or not. Whether the compiler can make any sense of it is a different question.
Can I include .cpp file ?
You can but you should not. cpp files should be 'included' in your project, not in other files.
Do i sometimes need to include both .cpp and .h files ?
See answer above. .h files only is the only sensible way to go

Related

How do I know which C++ source files include a specific h header file

I am studying a big C++ project and find a critical header file. I want to find all those .cpp files which include this header file to know the header file's influence scope. However I cannot find a simple way (several clicks or commands) to achieve this via vscode. Is there any trick can get this done? preferably using vscode.
Example, say we have source code files a.cpp, b.cpp, c.cpp..., and a header file foo.h, how do I know which source code file include this foo.h without opening them and check one by one?
Try "Edit->Find in Files" option inside VSCode for the header file you are looking for. The search result will give you the desired answer.

Esay way to include all header files from solutions explorer Visual Studio 2019

I want include all header files from solutions explorer like this:
without add all directories with this option:
Is there an easy way to tell VS2019 to use and link all header files from solutions explorer automatically?
Why?
If I have a lot of source code directories and in each directory are the header files... I need to add each directory manually.
Other simple Example:
The directory structure it this one:
And I need to add #include "test2.h" in "test1.c" and in this case VS cant find the header file. So the header "test2.h" is NOT where the test1.c is. Why VS dont find the header automatically by solutions explorer?
Ok. There is no solution.
Possible alternatives:
All headers are in one place and one include path is required.
Or headers in the same directory like the source code files.
Or headers need to be include like #include "../../header.h"
Thanks.
VS cannot provide the way of automatically including header files in all .cpp files. If the header file and the solution are in the same path, you don't need to add path in Additional Include Directories. If the header file is not in the same path with project, you only need to add the path.

CMake add_executable() use .cpp file or .hpp file?

I've read a bunch questions on this site about the usage of add_executable function in CMake, but have not found an exact answer to my confusion.
My question is why we only add .cpp files in the add_executable function but not .hpp files?
I understand header files are like "indices" for functions and classes in the corresponding .cpp files. But if we don't include them in the add_executable function, how are they used in the build process?
For example, will the A.hpp file be used when another source file import A.hpp? But then A.hpp is not in the add_executable function... How does the program know where to find A.hpp?
Thanks!
Header files, which often have .h or .hpp extension, although not always - for example, C++ standard library headers have no extensions - are "copy-pasted" by compiler into every .cpp (or .C, or .cc) which has #include directive to include the file.
Because of that, build system, such as CMake, doesn't have to know about them when the final executable is built - their contents is already accounted for by literal inclusion of their code into .cpp file.
However, build systems need to know about those files when dependencies are specified - to ensure that the whole application is rebuilt whenever any of those files is updated, and also to provide the proper inclusion path to the compilation command.

Referencing a cpp/h file in different location

I have a pretty basic question. If I have a .cpp and .h file in a different location than my project, how can I reference it by saying #include " ".
I am trying to use wxMathPlot.cpp/.h and it references wxWidget cpp files.
mathplot.cpp(19): fatal error C1083: Cannot open include file: 'wx/window.h': No such file or directory
So say my wxMathPlot.cpp is located in C:\Users\Owner\Desktop and my wx/window.h is in C:\Users\Owner\Documents
#include "../Documents/wxMathPlot.h"
Should work. To elaborate:
When you use an include such as #include "header.h" the same directory as the file is searched.
When you use an include such as #include <header.h> a specific directory is searched, chosen by your compiler, which is where you will find most standard library header files.
You can reference it by using its full path or by referencing through one or more ..'s in your path (that means "go up one level"), or you can specify the directory in which the header file resides in the 'header file search path' (the 'include path') and then just use the filename.
However, using the full path is not recommended because if you move the header file relative to the file it is being referenced from, then it will not work anymore.
Look at this SO question for a nice answer as well.
For CPP files you need add those files in your project. If you are using Visual Studio you can add the cpp file by right clicking on your working project and selecting add existing item. If you want to refer a .h file you need include this, e.g
#include "../Documents/wx/Windows.h"
It is always good to use relative path rather than using absolute path.

can't compile my code when the header is added to another file

I have a weird problem that I find hard to explain.
I have header file outPut.h and a source code file outPut.cpp. This compile easily on my code. However, if I add
#include "outPut.cpp"
to one of the header file in the same code I get an error about almost every line in outPut.cpp.
I now this is usually a problem with missing semicolor or something similar but I can't find it.
Don't include source (.cpp) files in header files, instead your source file should include the header and then you compile source file(s) directly (either explicitly passing to the compiler or with a makefile). Most likely you've introduced a circular dependency between the source and header file causing the many errors.
you're probably missing Include Guards