开发者

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

开发者 https://www.devze.com 2022-12-20 05:47 出处:网络
Here is my folder structure: / -- program.cpp -- utility.h -- utility.cpp -- module/ -- utility.h -- utility.cpp

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 def开发者_JS百科ined. 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)?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号