Visual Studio 2010 的奇怪“警告 LNK4042"

我刚刚被来自 Visual Studio 2010 (C++) 的一些重要警告击中了(相当严重).

I've just been beaten (rather hardly) on the head by some non-trivial warning from Visual Studio 2010 (C++).

编译给出了以下输出:

1 Debugis.obj : 警告 LNK4042: 对象指定不止一次;额外被忽略
1 Debugmake.obj : 警告 LNK4042: 对象指定不止一次;额外被忽略
1 Debugview.obj : 警告 LNK4042: 多次指定对象;额外被忽略
1 identity.obj : error LNK2019: 未解析的外部符号 void __cdecltest::identity::view(void) (?view@identity@test@@YAXXZ) 引用在函数 void __cdecl test::identity::identity(void) (?identity@0test@@YAXXZ)
1 identity.obj : error LNK2019: unresolved external symbol void __cdecl test::identity::make(void) (?make@identity@test@@YAXXZ) 在函数 void __cdecl test 中引用::identity::identity(void) (?identity@0test@@YAXXZ)
1 range.obj : error LNK2019: unresolved external symbol void __cdecl test::range::is(void) (?is@range@test@@YAXXZ) 在函数 void __cdecl test 中引用::range::range(void) (?range@0test@@YAXXZ)

1 Debugis.obj : warning LNK4042: object specified more than once; extras ignored
1 Debugmake.obj : warning LNK4042: object specified more than once; extras ignored
1 Debugview.obj : warning LNK4042: object specified more than once; extras ignored
1 identity.obj : error LNK2019: unresolved external symbol void __cdecl test::identity::view(void) (?view@identity@test@@YAXXZ) referenced in function void __cdecl test::identity::identity(void) (?identity@0test@@YAXXZ)
1 identity.obj : error LNK2019: unresolved external symbol void __cdecl test::identity::make(void) (?make@identity@test@@YAXXZ) referenced in function void __cdecl test::identity::identity(void) (?identity@0test@@YAXXZ)
1 range.obj : error LNK2019: unresolved external symbol void __cdecl test::range::is(void) (?is@range@test@@YAXXZ) referenced in function void __cdecl test::range::range(void) (?range@0test@@YAXXZ)

链接器错误总是很难调试......但是有未解析的引用,所以我检查了......但源格式正确......最后它击中了我:

Linker errors are always a pain to debug... but there were unresolved references, and so I checked... but the source is well-formed... and finally it hit me:

我的文件夹层次结构如下:

My folder hierarchy looks like so:

src/
  identity/
    is.cpp
    make.cpp
    view.cpp
  range/
    is.cpp
    make.cpp
    view.cpp

解决方案中的层次结构也是如此(我总是对其进行设置,使其模仿真实"的文件夹结构).

and so does the hierarchy in the Solution (I always set it up so that it mimicks the "real" folder structure).

和诊断输出:

Debugis.obj
Debugmake.obj
Debugview.obj

伴随着一个警告,指出 .obj 已被传递给链接器两次,而那个将被忽略.

Along with a warning which says that the .obj has been passed twice to the linker and that one will be ignored.

不再搜索:Visual 已经整齐地扁平化我的文件夹层次结构,因此无法整齐地编译源代码.

Search no more: Visual has neatly flatten my folder hierarchy, and therefore is unable to neatly compile the source.

目前,我只是想重命名文件,这应该可以解决问题......

At the moment, I am simply thinking of renaming the files, that should cover the issue...

...但是有没有办法让 Visual Studio 不扁平化文件层次结构?

... but is there a way to have Visual Studio NOT flatten the file hierarchy ?

推荐答案

只是想交叉发布我认为是答案的内容,如果您打开整个项目的属性,并更改 C 下的值/C++ ->输出文件 ->目标文件名"如下:

Just wanted to cross post what I believe to be the answer, if you open the properties for the entire project, and the change the value under C/C++ -> Output Files -> "Object File Name" to be the following:

$(IntDir)/%(RelativeDir)/

$(IntDir)/%(RelativeDir)/

在 VS 2010 下,我相信这将消除所有目标文件的歧义(因为我相信 Windows 不会让您在任何疯狂的情况下在同一目录中有两个同名的文件).另请查看详细信息 这里.

Under VS 2010, I believe this will disambiguate all of the object files (as I believe windows won't let you under any crazy circumstances have two files with the same names in the same directory). Please also check out the details here.

相关文章