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->Intermediate Directory”. Many people suggest storing the two files each at a different location while compiling, thus changing the output directory. However, this is not a good solution. The compiler warns you about the fact you made a design error. So I suggest, when you encounter this warning and thus having more than one file with the same name, you best should rename each of the files resulting in an unique name.

Posted in C++ by Bruno at March 19th, 2013.
Tags: ,

3 Responses to “Visual Studio Warning LNK4042: object specified more than once extras ignored”

  1. Semi Essessi says:

    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).

    • Bruno says:

      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.

  2. Mohammad says:

    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.

Leave a Reply