In our project, the directory structure of our source code files are linked with our namespaces. E.g., a class Util which belongs to the namespace MyNamespace_A would be implemented in the file
.../MyNamespace_A/Util.cpp
Now, the namespace 'OtherNamespace::SubNamespace' should also have a Util class. It should be implemented in the file
.../OtherNamespace/SubNamespace/Util.cpp
Without specifying an explicit object file (Properties of the .cpp file -> C/C++ -> Output Files -> Object File Name) this will lead to problems because two object files will have the same name and by default, they are stored in the same directory (which is '$(IntDir)').
Is there an automatic mechanism which lets me specify that the directory structure of the output files shall be the same as the structure of the source code directories?
Can I solve the problem in a different way than specifying the object file name for each of my source code files?
Right-click on project and go to...
Properties -> C/C++ -> Output Files -> Output File Name
Then enter...
$(IntDir)/%(RelativeDir)/
This will place every .obj file into a subfolder as in the source files.
Related
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"
In a typical C++ application (no Qt), I may have the following:
app/include/namespace1/Foo.h
app/src/namespace1/Foo.cpp
app/include/namespace2/Foo.h
app/src/namespace2/Foo.cpp
Where "app" is the root folder for the project. The classes in those files are:
//In app/include/namespace1/Foo.h
namespace namespace1 {
class Foo;
}
//In app/include/namespace2/Foo.h
namespace namespace2 {
class Foo;
}
In a build system like the one eclipse has, the object files for each .cpp files will be built into diffrent subdirectories.
In Qt, I create a .pri file in each of my folders which contains include, SOURCES, and HEADERS statements. However, when Qt builds the program, it places all of the object files in the same directory. So, [build output]/Foo.o gets generated twice and thus overwritten causing the linker to fail.
I looked into making each nested folder into its own SUBDIRS project with its own .pro file, but this doesn't work correctly since each folder is not an independent project, just an independent namespace.
What is the correct way to setup a project like this?
Here is one answer: https://riptutorial.com/qt/example/15975/preserving-source-directory-structure-in-a-build--undocumented---object-parallel-to-source--option--
This causes Qt to generate a directory structure in the build directory that "mirrors" the source directories.
I have the following question:
I was given the task - to build an application. There was a ready file counter.h and some other file - counter.obj. It turned out that in the counter.h there were only declarations of the functions - how can I include .obj file into the .cpp file so that it compiles? I am using Microsoft Visual Studio 2010 - and in which folder should the file itself go?
Add the obj-file to the Solution just as you would do with cpp-files (i usually do this by drag-and-drop, that is, drag the file from the Windows Explorer and drop it on a project in the Solution Exporer window).
You can put the obj-file together with cpp-files; it doesn't really matter.
You do cannot include object file in to a cpp file.
The compiler compiles the cpp file and generates the obj files, for each cpp file, these files are further linked together to create an libray or an executable.
Usually, you would link libraries(.lib or .dll) to an Application, Check if those are with you.
If not,
You can try linking the object file to your application by:
Go to project properties then from "Property Page" select the node "C/C++" their you will get "Additional Include Directories" add the name of your object file.Keep your obj file in the directory where your source code is or you can add the directory from:
Tools->Options->Projects and Solutions->VC++Directories.
I have never tried the second method except for academic projects,which was years ago, So not sure about it, Please check information on MSDN.
I am currently trying to compile a simple program that includes two header files. I see them in the Solution Explorer, where I included them through "include existing files". However, when I run my program it get the following error.
fatal error C1083: Cannot open include file: 'FileWrite.h': No such file or directory. THe problem is that I see the file included in the Header's folder and in the code I have written:
#include "FileWrite.h"
and then the rest of the program code.
Is there something else needed to do so that the compiler can see the header file and link it to the .cpp file I'm trying to compile?
If you write in your code something like #include "FileWrite.h" you need to make sure compiler can find that file. There are three options:
FileWrite.h should either be in the same directory as your source code file (.cpp) or
Path to that header file should should be listed in project's Properties (in C/C++ -> General -> Additional Include Directories) or
Path could be set in your VisualStudio - add it to Include Files in Tools->Options->Projects and Solutions->VC++ Directories
Which of these options shell be used depends on whether that header originates from this project (1st option) or some other project (any of other two options).
There are two ways to do this.
1) Only for the current project
Select your project -> properties -> C/C++ -> General -> Additional Include Directories -
Include your header file directory.
2) For all projects
Tools -> Options -> VC++ Directories -> Include files - Add the header file directory.
Refrain from using 2, as it would be difficult to figure out dependencies for a project when compiling it on a system different than yours.
When including files the compiler first looks in the current directory (the directory which contains the source .cpp file) then it looks in the additional include directories. If FileWrite.h isn't in the same directory as your source file check the additional included directories.
In the project's property page look at the additional include directories and see if they include the folder in which FileWrite.h is in.
You said the file is in the "headers" folder. This could either mean the headers filter or an actual headers directory on the filesystem. When including a file from your own project you need to specify the path from the file you're including into. So, if you had something like so:
src/main.cpp
include/my_object.h
You would use #include "../include/my_object.h" in main.cpp.
That's for directories. The folders you see in your project are called filters and have absolutely no relation to the directory structure of your project unless you force it to. You need to be paying attention to what the structure looks like in windows explorer to ascertain what path to use in an include statement.
Here is my folder structure:
/
|
-- program.cpp
-- utility.h
-- utility.cpp
|
-- module/
|
-- utility.h
-- utility.cpp
// Note that I have two files named utility.h and two named utility.cpp
On building the project, I get a link error (LNK2028: unresolved token and so on...) saying that some symbols aren't defined. I have confirmed that all symbols are defined and that all declared functions have a corresponding definition.
I have a feeling that on compiling my project, the utility.cpp files from both folders are compiled into the same utility.obj in the output folder. As a result, one overwrites the other.
Is this expected behaviour?
How do I
build a C++ binary which has two
files with the same name (though in
different folders)?
Right click both/either .cpp files > properties > C/C++ > Output Files > Object File Name > set a custom name. e.g. if both files are named MyFile.cpp in folder A and another in folder B, you can set the output to be AMyFile and BMyFile.
Alternatively, you can also use a macro to prefix the object names with the immediate parent folder name (i.e. using $(IntDir)\$(SafeParentName)$(SafeInputName)). If this is not enough (e.g. you have A/B/MyFile.cpp and C/B/MyFile.cpp) and you don't mind having some object files cluttering your source tree, you can also use $(InputDir)\ which will put the object files in the same folder as the source file.
the cpp files will then be compiled into two different object files..
enjoy!
Update for VS2010: There is a better solution in VS2010, check it out here. Thanks to n1ck's comment
btw, if the contents have the same name, do you separate them using different namespaces?
namespace A { // in folder A
class CMyFile {};
};
namespace B{ // in folder B
class CMyFile {};
};
// client.cpp
#include "A/MyFile.h"
#include "B/MyFile.h"
int main() {
A::CMyFile aMyFile;
B::CMyFile bMyFile;
return 0;
}
I don't know if it matters but it's definitely clearer to human : D
You could try adding another project to your solution, which will build a static mudule.lib file from your module's .cpp .h, then link your main project with that lib. It should make VS to output .obj files in a separate directory, and you should be able to link without problems.
Do you really WANT two different but same-named files in the same project?
The easiest thing that works well is put the conflicting .objs into different subfolders (I've used this technique with 2003 and 2008) based on the source dirs.
For example:
For src\gui\utils.cpp set "Object File Name" to ".\Debug\gui/" and
for src\database\utils.cpp set it to ".\Debug\database/".
While I do it manually whenever I spot a conflict, I can imagine that writing a script that updates the project for every .cpp file (or just for conflicting ones) would be a pretty trivial task.
Maybe libraries (static or dynamic) would help with your case. But you will still have problem if there are any public symbols with the same name like in executable or other library.
I don't know the VS compiling chain.
However, each .cpp is first compiled into a .obj file. The linking steps merges them together.
It's very common, to put all the .obj files in a same directory. So, as you guessed, when compiling the second one, erases the first one. Therefor some symbols are missing during compilation.
There probably is an option (again, I don't work with VS) to leave the .obj in the same directory as the .cpp file. The drawback is some garbage on your source code tree.
My personnal opinion would be to refactor.