define cpp file location - c++

I have a.cpp and a.h files in separate from the main project directory. I include a.h header file into main project using absolute path. Hot to tell c++ where it must look for a.cpp file?

The best way is to just add the cpp file to the project.
If it's outside the project or excluded from the build, you could include it in a another source file (#include "othercpp.cpp") and specify its path in the additional include directories under project settings.
But the correct approach would be to add it to the project. It doesn't have to be in the same place as other cpp files for this.

If your .cpp is in the project, it will find it. If it is not, you might work around that with the include directories, but thats just bad practice.

Visual studio normally compile all the cpp files included in the project,
so you don't need to 'find' it actually. Just drag it into current project.

Add the a.h to your projects default header file location. Do the same for your a.cpp. Add both the files to your project. You can now successfully build.
When I say add them to the default location, I mean the actual physical location that your main.cpp for the project is located as well as the header files for your project is located. If you put them anywhere else you will need to "Add additional include" directory paths for your header file.

Related

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.

Visual Studio C++ able to compile with compile errors (red underlines)

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.

Include header file in Xcode c++ project

I have a header file named A.h, another header file named B.h.
In B.h,
#include "XYZ/A.h"
but Xcode complains "XYZ/A.h" file not found.
I tried to make a subfolder XYZ in the folder where B.h is, and put A.h into XYZ, but the same error.
How to solve this? ===> Set parent folder of XYZ folder in project build setting "header search path". Problem resolved!
I don't want to change the header file B.h - I want to keep the relative path "XYZ/A.h".
PLEASE NOTE:
Both A.h and B.h are added into project (dragging...), and their physical location is not same with group folder in Xcode project. They may be in different folders in disk.
I am not particularly familiar with XCode but generally the compiler needs to know where to start looking when using relative include paths. For this reason, through compiler options or project options (since you are using an IDE), you must set the path to the directory that contains the directory XYZ. So if the path to XYZ is '/home/user/XYZ', then you need to add '/home/user/' to the search paths.
Now this is generally speaking. XCode may do it in a slightly other way but the point should be the same.

Xcode 4 C++ header file with relative path to another header

I'm using a library with an include structure where the .h files are all in a single directory. These .h files contains a single line, a #include directive which points to the 'real' header file in specific source folder locations. The #include path in these files is relative.
So, here's an example. The directory structure is:
/project
/sources
<my .cpp files>
<my .cpp files>
...
/include
/component
foo1.h
foo2.h
/platformA/something/foo1.h
/platformB/somethingelse/foo2.h
/include/component/foo1.h contains a single line of code:
#include "../platformA/something/foo1.h"
/include/component/foo2.h contains the single line of code #include "../platformB/somethingelse/foo2.h"
In my sources, I simply have:
#include "component/foo1.h"
The header search path for my project points to /include
Now, Xcode 4 is able to find component/foo1.h in /include, but it's unable to follow the relative include path within those headers and find the 'real' foo1.h in the `/platformA/something' directory, and so on.
I suspect it's because the include paths in the top-level foo1.h file is relative to its location, but Xcode might be treating it as relative to some other location (project root or something)? FWIW, Visual Studio has no problems with an identical configuration.
What can I do to remedy this?
From your directory structure it seems that the directories platformA and platformB are placed outside the include folder. There are two possible solutions to this:
Solution A
Move these
to include folder.
Solution B
Add project/platformA and
project/platformB to the
directories where include files
should be looked for in project
settings.
Don't use relative paths. Seriously, it's implementation-defined behavior how they work, so different compilers/environments/platforms will behave differently, and in your case, Xcode is almost certainly invoking GCC or clang in some sort of "build" directory, which may or may not be a sibling to your sources directory.
It's not worth the headache.
Put platformA and platformB in include, or add another directory (say, platform-include) put them in there, and add that directory to your include path.

Having trouble adding a dll to my project in visual C++

So I have gone through this tutorial three times:
http://msdn.microsoft.com/en-us/library/1ez7dh12.aspx
Every time I get to the end and try to run the program, it says:
Error 1 fatal error C1083: Cannot open include file: 'MathFuncsDll.h': No such file or directory
Using a .dll was so simple in C#.
Could anyone explain to me, assuming I have a header file C:\bob.h and a corresponding dll C:\bob.dll, how I would use the functions described in the header file?
Could anyone also explain why, even if a header file is added to the header files folder with Add Existing Item, the header file cannot seem to be found?
Thank you
The 'folders' in the solution are a grouping mechanism for managin the solution, and not related to 'finding' includes or libs when compiling or linking.
If all the code isn't in the same folder, or identified using references then you may want to add include directories - with VC this is typically located under project properties/configuration properties/C C++/General/Additional Include Directories. This sets on the compiler the -I option which is to specify a path to other locations for your header files.
Say you have
C:\A.h
C:\A.cpp
C:\Project1\B.h
C:\Project1\B.cpp
And B needs to use A.
You could:
Move A files into Project 1 folder and in B.h use #include "A.h"
Change B.h to #include "../A.h"
Add addition include directories of C:\ and use #include "A.h" or #include <A.h>
Meanwhile add A.cpp and B.cpp to the project will compile them in the location they are in the file system, the object file output should all be located in the intermediate directory and usable by the linker without further issue.