I am refactoring a code project (written in both c++ and c#) in visual studio and the structure of my directory is roughly like the attached picture here : enter image description here
P.s project2.sln is a part of a big project in the same directory.
Inside the commonFolder there are some .h and .cpp files that are used by the project2.sln located in folder 2.
I want to move this commonFolder to the SharedFolder and instead of giving an absolute path, I would like to give relative path (using Macros) to the properties of project2.sln so it can be compiled for all the users that checkout this trunk folder.
How can i define this relative path for the commonFolder using Macros?
Additional Include Directory is the convenient way to give relative path.
As an alternative, you can use shared item project.
These “shared items” projects don’t participate in build but they can
contain any number of C++ headers and sources.
move .h and .cpp files to shared item project folder.
add existing item in shared item project
add references in the project2
find the places where the error: cannot open the file. Modify path:
For example: #include "E:XXX/folder2/commonFolder/test.h" to #include "test.h"
Related
I started coding in C++ few days ago. I am using windows as my OS for writing code. I have been keeping all of my "well-written" codes in a single location. Now, I am working on a project that requires the use of those codes. So, I planned to include those files that I require as header files in my project. However, to make my project "self-contained", I created shortcuts of those folders that I require and kept them in the source folder of my new project and decided to use relative paths to the shortcuts in the "include" statements in my project.
However, I am getting an error. Is there any way to use relative (or, in general, absolute) paths to shortcuts in the include statements of C++ in windows?
Thanks.
It really depends on how you include the header files.
If you include with double-quotes, like e.g.
#include "some_header_file.h"
Then the relative path is from the current files location.
If you include using angle-brackets, like e.g.
#include <some_header_file.h>
Then the relative path is based on the system include paths.
You can always add a path to the system include path. How to do it depend on your environment and compiler. If you're using Visual Studio you go into the project properties dialog, and in the "C/C++" / "General" tab there is a field called "Additional Include Directories" where you can add directories. (This is for VS 2015, might be a little different on other versions.)
Regarding double quotes inclusion. Lets say your project hierarchy looks like this (on disk!):
Project
|-- Include
|-- Source
| `-- MoreSource
`-- Other
In Project/Source you have your source files, and if one of them want to include a header file from Project/Include, then it will look something like
#include "../Include/header.h"
Now if you have a source file in Project/Source/MoreSource that want to include the same header file it will be
#include "../../Include/header.h"
It could be useful to add the Project/Include directory to the system header search path. You can still use double-quotes to include the files, since if they are not found then the preprocessor will search the system paths as well, but you don't need the "full" relative path. If you add Project/Include to the system header path, you could write just
#include "header.h"
Or
#include <header.h>
Be careful though, if you have a header file with the same name as an actual system header file you might have some trouble.
From https://superuser.com/questions/253935/what-is-the-difference-between-symbolic-link-and-shortcut
You can't include folder shortcut since it's a file, not a folder.
You can read the guide to create symbolic link on windows.
I am developing a product with a team using CMake. We have several Visual Studio projects (libraries and executables) inside of our CMake project which reference other project headers (via target_include_directories()). In a source file these header includes look like:
#include "some_header.h" // from project_x
#include "another_header.h" // from project_y
I'd like to be able to include these headers with paths that reference the project they are pulled from, e.g.:
#include "project_x/some_header.h"
#include "project_y/another_header.h"
What is the most acceptable way to do this? I have thought of a couple solutions:
Add the directory which contains the project as an include path. This has the undesirable side-effect of including everything and seems like a bad solution.
Include a subfolder of the project called 'include' which contains a folder named with the same as the project, which creates a slightly redundant path: /<project_name>/include/<project_name>/<...>
There is a third solution, to use a shared include directory with a subfolder for each project, but it will not work for our project because we group our build projects by category in the file system and Visual Studio solution and it will cause the folder structure inside of /include/ to diverge from the rest of source tree which is undesirable.
Are there any better (or more canonical/idiomatic) ways to accomplish this?
If you have a project structure like this:
project_x/some_header.h
project_y/another_header.h
and you want to keep all of your CMakeLists the same, then I would introduce another folder in each project:
project_x/project_x/some_header.h
project_y/project_y/another_header.h
Of course, this requires changing the includes in each project to reflect this new structure, including the project where the header is defined proper.
There's some precedence to this, as this is how curl and googletest do it.
Edit: I understand this is very similar to the second approach you outlined. If your directory structure already employs include directories, then my suggestion is exactly the same as your second one. At the very least, this should confirm your intuition that this isn't an entirely absurd thing to do, even if it creates some redundancy.
I want to include a specific header file (MyHeader.h) in a C++ project. The solution for my project is located in the folder:
C:\\Projects\\MyProgram
The header file is located in the folder:
C:\\Projects\\MyProgram\\Files
I tried the following line of code, but it doesn't work.
#include <Files\MyHeader.h>
Is there an easy way to include the header file without adding the full path to "Include directories" in the configuration properties?
Thanks in advance for any help. :)
Try this
#include "files/myheader.h"
It will work if the header is in a files folder in the same directory as the current source.
If you're trying to include a 3rd party library and not your own header, I'd suggest you to save the library headers in a particular path (say C:\Library\headers). (If there are static libraries put them in some other path like C:\Library\lib).
In your Visual Studio C++ Project, go to View > Other Windows > Property Manager.
Double Click on the Project Name. You will see a dialog box like this:
Make sure All Configurations is chosen in the dropdown, if you want the change to be applied to both the Debug and the Release Configurations. Else just choose the Configuration you want the properties to be applied to.
Go to VC++ Directories on the left and choose Include Directories in the right, and enter the path(s) in the textbox separated by a ;.
You can also use the drop down and use the Dialog box to add the paths if you'd prefer to browse to each path separately
Add the library path the same way to Library Directories
Save the changes using the Save button on the Property Manager Pane's toolbox.
You will then be able to access the header file contained in the directory you added by something like:
#include <myheader.h>
This approach will help, because it won't matter where the headers saved. The header path is not hard-coded.
The current directory of the source file is always searched, although if you use angled brackets it is searched after your include path, whilst if you use quotes it will be the first directory searched.
The directory of your solution or makefile/project file is irrelevant, the local path is relative to the compilation unit, i.e. the cpp file.
If that cpp file includes a header, that headers own includes are relative to itself, not the cpp file that included it. (It would be hell to manage if it were not).
Ideally you should use forward slashes in paths too.
Your actual correct setup here is to include the solution directory in your search path. If it is Visual Studio you can use a macro for this, $(SolutionDir) I think.
That means that if anyone else is going to build your solution, they can put it in a directory they choose and as long as the structure underneath is the same, it will still work.
To use a relative path in your cpp file without any include directory settings, you might need something like:
#include "../Files/MyHeader.h"
You just need to replace your brackets <> with double quotes "" like this:
#include "Files\MyHeader.h"
Brackets is used when you want Visual Studio to find the path from your project settings and double quotes when you want to access the header from a specific path or relative to your project.
I come from a Java/AS3/Javascript background where all of my classes are organized into packages that help denote their functionality.
In starting a C++ project I sought to mimic this file system structure in mostly the same way but I've been running into issues with the includes.
Currently I have an src directory with the main.cpp file inside it. Then I have some root directories and with other files inside. Here's an example:
src
->main.cpp
->window
---->Window.h
---->Window.cpp
main.cpp includes Window.h with the statement #include "Window.h" and everything builds just fine. But if i restart Visual Studio, it complains that it can't find "Window.h".
In looking a open source projects, I've seen some that just have all the source files in one directory with no nesting for easy includes I suppose. Some have the headers and cpp files separated.
What is the correct way (or at least a way that will cause less headaches) to organize a large-ish C++ project on the file system?
Thanks!
Breaking it out like you've tried to do is reasonable and easy enough to accomplish.
You just need to set your project's include paths. From Visual Studio, right click on the project name and click "Properties". From there, in the tree control on the left hand side, expand "C/C++", and then select "General" in the tree. The first option on the right hand side should then be "Additional Include Directories".
There you have several options:
You can specify specific include directories (separated by semicolons). For instance, if you had folders "Window" and "Printing" you could put in:
..\Window;..\Printing
Which would allow you to include the files from window and printing easily, like this:
#include <Window.h> // from src/window
#include <Printing.h> // from src/printing
The above approach has some drawbacks, as you can easily collide with names from other libraries you may be using, making the include ORDER very important.
A better approach (in my opinion) is to add the following as an include path:
..\
This will make it search the parent directory when looking for includes. This allows you to be more verbose in your include paths, like this:
#include <Window/Window.h> // it's more clear where these are coming from
#include <Printing/Printing.h> // and much less likely to collide with other library
// header files
It makes sense to follow the Java example and arrange source files by C++ namespace. Create sub-folders under your /src directory that correspond to the namespaces.
I am working on a game using Visual C++. I have some components in separate projects, and have set the project dependencies. How do I #include a header file from a different project? I have no idea how to use classes from one project in another.
Settings for compiler
In the project where you want to #include the header file from another project, you will need to add the path of the header file into the Additional Include Directories section in the project configuration.
To access the project configuration:
Right-click on the project, and select Properties.
Select Configuration Properties->C/C++->General.
Set the path under Additional Include Directories.
How to include
To include the header file, simply write the following in your code:
#include "filename.h"
Note that you don't need to specify the path here, because you include the directory in the Additional Include Directories already, so Visual Studio will know where to look for it.
If you don't want to add every header file location in the project settings, you could just include a directory up to a point, and then #include relative to that point:
// In project settings
Additional Include Directories ..\..\libroot
// In code
#include "lib1/lib1.h" // path is relative to libroot
#include "lib2/lib2.h" // path is relative to libroot
Setting for linker
If using static libraries (i.e. .lib file), you will also need to add the library to the linker input, so that at linkage time the symbols can be linked against (otherwise you'll get an unresolved symbol):
Right-click on the project, and select Properties.
Select Configuration Properties->Linker->Input
Enter the library under Additional Dependencies.
Since both projects are under the same solution, there's a simpler way for the include files and linker as described in https://learn.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-projects?view=vs-2019 :
The include can be written in a relative path (E.g. #include "../libProject/libHeader.h").
For the linker, right click on "References", Click on Add Reference, and choose the other project.
Expanding on #Benav's answer, my preferred approach is to:
Add the solution directory to your include paths:
right click on your project in the Solution Explorer
select Properties
select All Configurations and All Platforms from the drop-downs
select C/C++ > General
add $(SolutionDir) to the Additional Include Directories
Add references to each project you want to use:
right click on your project's References in the Solution Explorer
select Add Reference...
select the project(s) you want to refer to
Now you can include headers from your referenced projects like so:
#include "OtherProject/Header.h"
Notes:
This assumes that your solution file is stored one folder up from each of your projects, which is the default organization when creating projects with Visual Studio.
You could now include any file from a path relative to the solution folder, which may not be desirable but for the simplicity of the approach I'm ok with this.
Step 2 isn't necessary for #includes, but it sets the correct build dependencies, which you probably want.
#include has nothing to do with projects - it just tells the preprocessor "put the contents of the header file here". If you give it a path that points to the correct location (can be a relative path, like ../your_file.h) it will be included correctly.
You will, however, have to learn about libraries (static/dynamic libraries) in order to make such projects link properly - but that's another question.
You need to set the path to the headers in the project properties so the compiler looks there when trying to find the header file(s). I can't remember the exact location, but look though the Project properties and you should see it.
Try to avoid complete path references in the #include directive, whether they are absolute or relative. Instead, add the location of the other project's include folder in your project settings. Use only subfolders in path references when necessary. That way, it is easier to move things around without having to update your code.