Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2008: Can I build a project with 2 CPP files of the same name in different folders?

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.

  1. Is this expected behaviour?
  2. How do I build a C++ binary which has two files with the same name (though in different folders)?
like image 624
Agnel Kurian Avatar asked Feb 17 '10 14:02

Agnel Kurian


People also ask

How do I have multiple cpp files in one project?

Visual C++ For creating more code files to go into a project, use the "Add New Item" under the "Project" menu to add new C++ code files. An executable can consist of many files, but can have only one main() function!

How do I link two .cpp files?

Basically linking or including is done by #include<filename> or #include”filename” in C++. Now, as per your question, two c++ files can be linked by using #include”filename. cpp” , if all the files are in same directory. Otherwise specify the directory name before the filename.

How do you add another .cpp file to a project?

Add a new source file to the project, as follows. In Solution Explorer, right-click the Source Files folder, point to Add, and then click New Item. In the Code node, click C++ File (. cpp), type a name for the file, and then click Add.

How do multiple cpp files work in Visual Studio?

By right clicking Project in solution explorer then click Add and in next option menu click on new item. Select C++ File (. cpp) File. Give the new file a name(we will use “square”), then click add and it will be added to your project.


3 Answers

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

like image 92
Afriza N. Arief Avatar answered Oct 21 '22 03:10

Afriza N. Arief


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.

like image 27
Dmitry Avatar answered Oct 21 '22 03:10

Dmitry


Do you really WANT two different but same-named files in the same project?

like image 28
MickeyfAgain_BeforeExitOfSO Avatar answered Oct 21 '22 02:10

MickeyfAgain_BeforeExitOfSO