VisualStudio项目具有多个同名的源文件? [英] VisualStudio project with multiple sourcefiles of the same name?

查看:785
本文介绍了VisualStudio项目具有多个同名的源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型的C ++项目,其源文件组织在多个文件夹中(在文件系统上).

i have a largish C++ project, with source-files organised in multiple folders (on the filesystem).

在这些文件夹中的两个中,我具有相同名称的文件. 例如

in two of these folders, i have files with the same name. e.g.

\MyProject\foo\Blurp.cpp
\MyProject\foo\File.cpp
\MyProject\bar\File.cpp
\MyProject\bar\Knoll.cpp

该项目是跨平台的,我在Linux和OSX上使用autoconf,但必须在W32上使用MSVC(由于我在W32上使用了某些第三方C ++库,并且编译器之间的C ++二进制接口不兼容)

the project is cross platform, and i use autoconf on linux and OSX, but have to use MSVC on W32 (due to some 3rd party C++ libraries i use on W32 and the C++ binary interface incompatibilities across compilers)

在MSVC方面,该项目也被组织为多个过滤器"(这些虚拟文件夹)(名称大致与文件所在的目录相对应),因此我可以区分它们.

on the MSVC side, the project is organized into multiple "Filters" (those virtual Folders) as well (with names roughly corresponding to the Directories the files live in), so i can distinguish them.

现在的问题是,当我构建项目时,MSVC将目标文件放在单个平面目录中,最后我得到:

now the problem is, when i build the project, MSVC puts the object files in a single flat diretory, and i end up with:

\MyProject\Release\Blurp.obj
\MyProject\Release\File.obj
\MyProject\Release\Knoll.obj

可以看到,只有一个File.obj,因此缺少一个二进制对象. 显然,链接器会抱怨,因为它找不到丢失的目标文件中定义的类/函数/....

as can be seen, there's only one File.obj, so one binary object is missing. obviously, the linker complains, since it cannot find classes/functions/... defined in that missing object file.

是否有一种方法可以告诉MSVC根据目标文件所在的目录(或过滤器)创建具有唯一名称的目标文件?

我想像这样的东西:

\MyProject\Release\foo\Blurp.obj
\MyProject\Release\foo\File.obj
\MyProject\Release\bar\File.obj
\MyProject\Release\bar\Knoll.obj

\MyProject\Release\foo-Blurp.obj
...

或其他. 我知道的所有其他构建系统(CMake,自动工具)都可以处理同名的多个文件.

or whatever. all other build-systems i know (CMake, autotools) are able to deal with multiple files of the same name.

此问题类似于 3729515 ,但我目前仍停留在VS2008上. (那里为VS2008建议的解决方案-为每个有问题的文件设置对象目录-确实在理论上可行,但出于实际原因我希望避免这种情况)

this question is similar to 3729515, but i'm currently stuck to VS2008. (the solution suggested there for VS2008 - to set the Object-Directory for each file in question - is something which indeed works theoretically, but which i would like to avoid for practical reasons)

推荐答案

您可以为一个(或两个)冲突文件设置特定于文件的项目设置,并设置对象文件"名称"属性为:

You can set a file-specific project setting for one (or both) of the files that conflict and set the "Object File Name" property to:

$(InputDir)\$(IntDir)\

只需右键单击文件名而不是项目名称,即可仅设置该文件的属性.

Just right-click the filename instead of the project name to set the property for that file only.

例如,如果对\MyProject\foo\File.cpp执行此操作,则该源文件的目标文件将转到\MyProject\foo\Release\File.obj,因此不会与\MyProject\bar\File.cpp的目标文件冲突.

For example, if you do that for \MyProject\foo\File.cpp then the object file for that source file will go to \MyProject\foo\Release\File.obj so it won't conflict with the object file for \MyProject\bar\File.cpp.

这样做的缺点是,它可能会使编译器输出中的源代码树混乱(但希望不会太多),而且-更糟糕的是,特定文件的项目设置往往会被忘记/隐藏,因为它们没有完全在IDE中被调用.如果某个时候您(或其他人)需要进行更改,那么对于为什么特定文件的构建如此奇怪地起作用,直到有人花了半天时间直到它突然出现,这可能是一个谜.上.

The drawbacks to this are that it can clutter your source tree with compiler outputs (but hopefully there aren't too many), and - even worse - file-specific project settings tend to get forgotten/hidden since they're not called out in the IDE at all. If sometime down the road you (or someone else) needs to change things, it can be quite a mystery about why the build acts so strangely for particular files until someone screws around with it for a half a day until it dawns on them what's going on.

我个人更愿意在项目范围内将$(InputDir)\$(IntDir)\设置为将目标文件转到相对于源文件的目录中,但实际上在项目级别设置中根本无法正常工作.在那种情况下,VS仍然只设置一次输出目录,并且最终相对于列表中的第一个源文件.然后,链接器对于应在何处查找目标文件感到困惑.

I would personally prefer that a project-wide setting of $(InputDir)\$(IntDir)\ would cause object files to go to directories relative to the source file, but it actually doesn't work well at all as a project level setting. In that case VS still only sets the output directory once and it ends up being relative to the first source file in the list. Then the linker gets confused about where it should look for object files.

这篇关于VisualStudio项目具有多个同名的源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆