VisualStudio项目与多个源文件同名?

时间:2023-01-25 11:43:58

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

我有一个较大的c++项目,它的源文件在多个文件夹中组织(在文件系统上)。

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

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)

这个项目是跨平台的,我在linux和OSX上使用autoconf,但必须在W32上使用MSVC(由于我在W32上使用的一些第三方c++库,以及跨编译器的c++二进制接口不兼容)。

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:

现在的问题是,当我构建这个项目时,MSVC把目标文件放在一个单一的平面diretory中,结果是:

\MyProject\Release\Blurp.obj
\MyProject\Release\File.obj
\MyProject\Release\Knoll.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.

可以看到,只有一个文件。obj,一个二进制对象丢失了。很明显,链接器会抱怨,因为它不能找到类/函数/…在那个丢失的对象文件中定义。

is there a way to tell MSVC to create object files with a unique name depending on the directories (or filters) those files live in?

是否有一种方法可以让MSVC根据目录(或过滤器)来创建具有惟一名称的对象文件?

i imagine something like:

我想这样:

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

or

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

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

之类的。我所知道的所有其他构建系统(CMake、autotools)都能够处理相同名称的多个文件。

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)

这个问题类似于3729515,但是我现在仍然坚持VS2008。(在VS2008中提出的解决方案——为每个文件设置对象目录——确实在理论上可行,但出于实际原因,我希望避免使用它)

2 个解决方案

#1


6  

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.

只需右击文件名而不是项目名称,只需要为该文件设置属性。

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.

例如,如果你这样做\MyProject\foo\文件。然后,该源文件的对象文件将进入\MyProject\foo\Release\ file。obj所以它不会与\MyProject\bar\ file .cpp的对象文件冲突。

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.

这样做的缺点是,它可以将编译器输出的源代码树弄得乱七八糟(但希望没有太多),甚至更糟糕的文件特定的项目设置往往会被遗忘/隐藏,因为它们在IDE中根本没有被调用。如果某个时候,你(或者其他人)需要改变一些事情,那么为什么对于特定的文件如此奇怪,直到有一天有人用它来处理这些文件,直到他们意识到发生了什么事情,这都是一个谜。

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.

我个人更倾向于一个项目范围的设置$(InputDir)\$(IntDir)\将会导致对象文件到相对于源文件的目录,但是它实际上并不能很好地作为一个项目级别设置。在这种情况下,VS仍然只设置输出目录一次,它最终会相对于列表中的第一个源文件。然后,链接器会对它应该查找对象文件的位置感到困惑。

#2


8  

Maybe you can set the project wide 'object file name' (Configurtion Properties->C/C++->Output Files) to

也许您可以设置项目范围的“对象文件名”(配置属性->C/ c++ ->输出文件)。

$(IntDir)%(RelativeDir)

which uses the relative source folder of the source files. Note the %, but this gets ugly if your source files are located out of your project directory, containing ..\

它使用源文件的相对源文件夹。注意%,但是如果您的源文件位于您的项目目录中,其中包含..\。

#1


6  

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.

只需右击文件名而不是项目名称,只需要为该文件设置属性。

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.

例如,如果你这样做\MyProject\foo\文件。然后,该源文件的对象文件将进入\MyProject\foo\Release\ file。obj所以它不会与\MyProject\bar\ file .cpp的对象文件冲突。

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.

这样做的缺点是,它可以将编译器输出的源代码树弄得乱七八糟(但希望没有太多),甚至更糟糕的文件特定的项目设置往往会被遗忘/隐藏,因为它们在IDE中根本没有被调用。如果某个时候,你(或者其他人)需要改变一些事情,那么为什么对于特定的文件如此奇怪,直到有一天有人用它来处理这些文件,直到他们意识到发生了什么事情,这都是一个谜。

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.

我个人更倾向于一个项目范围的设置$(InputDir)\$(IntDir)\将会导致对象文件到相对于源文件的目录,但是它实际上并不能很好地作为一个项目级别设置。在这种情况下,VS仍然只设置输出目录一次,它最终会相对于列表中的第一个源文件。然后,链接器会对它应该查找对象文件的位置感到困惑。

#2


8  

Maybe you can set the project wide 'object file name' (Configurtion Properties->C/C++->Output Files) to

也许您可以设置项目范围的“对象文件名”(配置属性->C/ c++ ->输出文件)。

$(IntDir)%(RelativeDir)

which uses the relative source folder of the source files. Note the %, but this gets ugly if your source files are located out of your project directory, containing ..\

它使用源文件的相对源文件夹。注意%,但是如果您的源文件位于您的项目目录中,其中包含..\。