I've been searching for how to do this for nearly an entire day.
At first, I thought that this can be done by...
Right click the project name -> "Add files" -> choosing a .cpp file which contains the class you need and the corresponding header file.
Then, the .cpp file and header file appeared with its original folder. After this, I wrote #include"random.h" on the project which needed to use random.h and its functions.
However, this produces an error, indicating that fatal error: random.h: No such file or directory. The compiler apparently can't find the file (even though I can).
I add a picture of this.
Also, I've been looking for how to add .cpp & header files without its folder.
(In the picture above, for example, you'll see that random.cpp inside Using_a_class_test is included in a folder named Random. To my shame, I haven't found how to eliminate such a folder.)
I'd appreciate if you'd offer any insight.
Unfortunately, what you did is not enough. When you try to compile
#include "random.h"
The compiler needs to know where the random.h file is, and for that it uses the include path info, which is unrelated to the files you included in the project.
A couple of solutions:
You modify all occurrences of #include "random.h" to be #include "/path/to/random.h"
You modify the include path information for your project. Go to Project >> Build options, select the tab "Search directories" and add all the paths to your .h files there.
Hope this helps.
Related
So I have been trying to learn cpp and I was writing a program, and when I try to build the solution, it gives an error saying
unexpected end of file while looking for precompiled header. Did you forget to add #include "pch.h" to your source?
Then I included it and I got the same error, and also another saying
cannot open source file pch.h
One option, if you are new to c++, is to just turn off pre-compiled headers in the project settings.
It needs to be the first include, you can't place it under other includes.
Your .cpp file is probably not in the same directory as pch.h
Try adding the directory that your pch.h is in to the additional includes, even if it is at the root of your project.
quick solution to a frustrating issue when trying to add .pch to an exisiting project:
if you have a /include /src dir structure, it might not work,
unless you place the "pch.h" and "pch.cpp" in the same dir /src.
Also: mark the "MyPreComp.cpp" as /Yc - create,
and in the .cpp files you want to use the .pch set them to Yu - use.
#include "pch.h" as the first #include in the .cpp
NB. You need to set "not using precompiled headers" to all .cpp files not using them,
yes, it IS a hassle.
( Visual Studio 2019 )
It needs to be included to each cpp file (by default)
It needs to be included in the very first line of your code (excluding the comments, it's ok to have the fancy comments on top)
It needs to be in a reachable directory. This error often happen when you have a folder structure in your project. So this can happen with a source files in some nested folder, when your precompile-header-file is up there in main. In this case, either add necessary number of "../" before the file name, or add the main folder to the "additional include directories" as it is already suggested above.
It needs to actually be the same precompile header file, that is set as the one in project setting. Check the file with "Precompiled Header" option set to "Create (/Yc)", ensure that it refers to he same header file, that you include ("pch.h" or "stdafx.h" by default) This error often happens when you include some old source to newer proj, or vice-versa, due to different default names in different studio versions: "stdafx.h" vs "pch.h".
If all above is set up, and you still have it, check if you actually set it up for the right build configuration. Always apply project setting change for all configurations. Costed me some nerves when I did it for only one config, and was trying to compile another:
It's surely a noobish problem, I'm sorry for wasting your time, but I can't find any solution (I searched a bit on the internet, but found nothing).
I have a Console Application Project in Visual Studio 2013 and I have many folders where I put various .h and .cpp files. In these files I use namespaces for grouping classes.
The problem is that I can't include headers that are in parent directories in the current .cpp or .h file.
For example, I have a Hello class under the path /a/b/c, that is included in the namespace A/B/C, and a Hi class under /a/b/ that is in the namespace A/B. In my situation the Hello class doesn't "see" class Hi, but class Hi can "see" class Hello. Also, if I have Bye class in folder a/d/ with namespace A/D, class Bye doesn't "see" class Hi and Hello, and viceversa.
What I mean for "doesn't see" is that #include doesn't find the headers and that when I use "using namespace ..." there aren't parent namespaces.
P.S. I hope my English is understandable (it's not my mother language) and that what I just wrote above is clear.
In the Solution Explorer Window. Right click on your project and choose Properties from the menu.
In the C/C++ -> General settings the first field is "Additional Include Directories"
This is where you add paths for the compiler to load include files.
When the compiler encounters an #include statement, it tries to open the specified file. If the file is an absolute path, it only tries to load from that specific absolute.
If the file is a relative path, it tries to load from the directory of the file being compiled first. If the file is not found in the same folder as the c/cpp file, the compiler tries each of the paths in its 'Include Directories' list to find the file.
In visual studio, the 'Additional Include Directories' are based on the current project directory by default. You can obviously specify whatever you want using absolute paths and/or variables.
For your example you might want include directories something like:
a/b;a/b/c;a/d
I found a solution for the problem. I used as a prefix in my #include the string "../../", then I can include all my files in my directories. I don't know if this the best thing to do, but it worked.
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.
I am having a problem of getting compile errors (red underlines) like:
Error: cannot open source file "stdafx.h"
Here an edited screenshot of the environment:
On the LEFT is my Visual Studio Solution Directory list with the "Show All Files" off.
I am working on a school project, and each Folder are the source files of different parts of the project with different people who are in-charge of them.
For example, Student A and B are incharge of AST and PARSER folders (we will call them sub-projects).
We have an API for each sub-project so other sub-projects know what to call.
At the TOP-CENTER, we have my Source File for a class QueryProcessor. (just the first few lines)
Below it, is the Output for the Build Success.
The red lines are all over all the classes, mainly cause the #include "stdafx.h" cannot be opened by the environment.
On the RIGHT, that is the stdafx.h where we include all the different sub-projects so we save the trouble of each project having a different stdafx.h
However, I am able to build the project. I am pretty sure I am doing this directory/linking wrongly.
This should work
Right click on the solution file
Click Open in Windows Explorer
Find file stdfx.h in explorer and copy the path of the folder
In visual studio solution explorer, Right click on the project file
Click properties-> C/C++ -> General
In the Additional Include Directories paste the path
Combining folders and virtual folders in VC is from my point of view messy because the virtual folders indicate that all files are in one directory and the folders created on the harddrive obviously indicate that all files are in different directories. You can combine it if you know what's going on but in your case I would not recommend it.
I assume you missunderstand the purpose of stdafx.h The purpose of this header file is NOT to put all header filles into it and then just include it to all other files. Here is a SO question about this Purpose of stdafx.h
After cleaning up your stdafx.h file include as many header files into your .cpp files and only put these includes in your header files if they are required in the header file
Turn on show all files, now you will work with actual folders and you can be sure that if you adress a folder like "PKB" that this folder really exists since you can see it in the left solution explorer.
If you use using namespace std; for example make sure you also include the required header files. You might think "hey I already included e.g. iostream in another header file which I now include in this header file so I don't need it" That will really destroy you when you work with bigger projects.
Oh and regarding the stdafx.h include problem as soon as you switch to show all files I assume you will realise that stdafx is in a different file than the file where you use the include. Maybe something like #include "..\stdafx.h" is required (depending on your structure).
I think it's obivious but if you include a header file the include is allway relative to the file which is including the other header file.
stdafx.h is commonly used for creating a precompiled-header, which essentially is a compile-time optimisation such that the compiler will not continually compile these headers for every compilation unit.
If any of these headers changes, you will need to do a full system rebuild.
In reality it is preferable only to use it to include standard headers plus third-party headers (like boost libraries and similar) that you are not ever going to change.
You may decide that some of your own libraries are "set in stone" and can also be included.
Every project, i.e. every part of the project that is built into a separate unit (DLL or .exe) should have its own precompiled header and its own version of stdafx.h
Projects should only ever include their own .stdafx and not those of other projects, therefore this header file can also be used to define your dllexport macro.
When arranging your project headers you should be aware of:
1. Which headers are included externally
2. Which headers are only included internally, and are not even included indirectly externally.
The latter sort should include your stdafx.h file and should ideally not be in the same directory as those headers included from outside your project.
I'm using VS2010 (downloaded via dreamspark) and although I can open the #include file by right clicking on it and pressing on Open Document, it complains "Error can not open source file "..."" which seems rather absurd. I'm using Qwt with Qt this time around and I'm specifically having the problem for:
#include <qwt_counter.h>
#include <qwt_plot.h>
(And I am using the "<>"); not sure how to make those appear properly in the code above.
Thanks in advance.
As Neil indicated, try using quotes instead of the <> characters around the filename. When using the quotes, MSVC will look in the same directory as the file the #include is in for the specified file, then if it's not found there will look in the directories specified by the include path. When the filename is surrounded by <> characters, the current file's directory isn't looked at - the compiler goes right to the include path.
See http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx for details.
Note that this is an implementation dependent behavior - it might not apply to other compilers.
If that doesn't help, make sure that your include path contains the directory that the file is located in by setting the "Include Directories" property appropriately:
http://msdn.microsoft.com/en-us/library/t9az1d21.aspx
Finally, you might be using a makefile project (I'm not sure how common it is for Qt projects to continue to use qmake when built from VS) , in which case you'll need to perform whatever configuration is necessary in the make file(s) or parameters passed on the command line that invokes the makefiles.
Is the path where these files are located either the same as that of this source file, or included in the "additional include directories" in your project settings?
Project -> properties -> c/c++ section -> additional include directories.
If they are located in a subdirectory of the source file you're editing or of one of the additional include directories (I think) you can also include them with:
#include <path_to_file_1/qwt_counter.h>
#include <path_to_file_2/qwt_plot.h>
[edit]
or of course what neil says
[/edit]
It turned out there was a circular linking happening and I had all my code in a .h file. I split it up and added the corresponding .cpp file, now everything works fine.