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 namedMyFile.cppin folderAand another in folderB, you can set the output to beAMyFileandBMyFile.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 haveA/B/MyFile.cppandC/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?
I don’t know if it matters but it’s definitely clearer to human : D