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.
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!
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.
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With