Visual Studio Warning LNK4042: object specified more than once extras ignored
When I encountered this warning during compiling, I went searching what this warning really caused. It happened that somehow I had two separate files, having the same name. Due to this, the compiler warned me about this design flaw. When I searched on the internet on this warning, I saw a lot of posts of people who suggested to change the project settings of Visual Studio.
When the compiler compiles your files, the output of the files are stored in the location defined by “Project Options->Configuration Properties->C/C++->Output files->Object File Name”. By default, this is the intermediate directory. The intermediate directory is defined in “Project Properties->Configuration Properties->General->
I have to disagree – this is a bad default setting in Visual Studio. As such it is a bug.
It stems from MS not testing Visual Studio against many, if any, existing projects. There are two bugs like this I find especially annoying. this $(IntDir) instead of $(IntDir)/%(RelativeDir/ – which is the only correct choice for default in this scenario IMO – and $(ProjectDir) for the debug path, which should be $(TargetDir) for almost every software project I’ve ever seen or touched. Again this is the only sensible choice for the default…
Both cases encourage bad practice work arounds (e.g. running your app from the project dir [why?] and renaming files to work around compilation errors).
The first part I agree.
Still in many projects I’ve worked, we encourage to put only one class in each file and name this file after the class it contains. When you have two files with the same name, it suggests you have two classes with the same name. This could be a design problem. However, in case you have two files/classes in two different locations and they’re using a different namespace to distinguish each other. Then it would be a valid to say it isn’t a design issue.
I found the issue in my case was when saving a source code in the project using saveas. In this case the source code file already in the project source files ends up having two copies. Note the two copies are in the VS “source files” directory and not in windows directory.
To resolve the issue go to the “source files” directory in your project and remove the unwanted copy of the file.