I'm using Visual Studio 2010 to compile my C++ project, and the linker is barfing on two of my files which have the same name- but they're in completely different directories. How can I make the linker recognize that they are different files?
Asked
Active
Viewed 7,598 times
12
-
Please give us the exact details, the cause is probably *not* the fact that they have the same name, rather that they, for example, have a function with the same name or similar. – Irfy Feb 19 '12 at 16:59
-
There's no information here, not even what the error message is. All anyone can do is guess at the problem, and that's a waste of time. – Carey Gregory Feb 19 '12 at 17:09
-
Right-click one of the files, Properties, C/C++, Output Files, change the Object File Name to, say, $(IntDir)\$(InputName)2 – Hans Passant Feb 19 '12 at 17:33
-
For the record, the "duplicate" question was asked six months after this one, making that the duplicate. – Puppy May 31 '21 at 07:56
2 Answers
15
I believe the problem comes from the fact that all your .obj files are written to the same folder, and so the outputs from compiling those two source files are colliding. I think there are at least two possible solutions:
- Use a different output directory (build directory) for each input folder
- Create custom object file names for each (or just one) of your source files
I'm not certain about the first option, but for the second, you should be able to right-click the source file in the solution explorer, select "Properties", and find some configuration setting to over-ride the output (.obj) file created for that source file.

JonasVautherin
- 7,297
- 6
- 49
- 95

aldo
- 2,927
- 21
- 36
-
2Yes, this is it. They are all written to the same folder with the filename as the obj name, even if they originate from different directories. A very dumb design, IMO. However, reconfiguring the output name for one of the two worked to solve the problem. Thanks! – Puppy Feb 20 '12 at 05:45
12
Use $(IntDir)%(RelativeDir)
in "Object File Name" property
(Configuration Properties -> C/C++ -> Output Files -> Object File Name)
- of project, OR
- of .cpp file.
This is an answer from the related question VisualStudio project with multiple sourcefiles of the same name?.

Flaviu
- 931
- 11
- 16
-
1Jesus Christ, this error made me lose 2 hours until I realized the .obj file was being overwritten (big solution, I checked so many things). This is the true solution for the problem. Thanks! Hard to belive this is not already the default configuration. – Ricardo Alves Jul 03 '19 at 15:14