问题描述
直接问题:如果我有两个同名的文件(但在不同的目录中),似乎只有 Visual Studio 2005 可以透明地处理这个??VS 2008 &2010 需要大量调整?除了我的命名约定,我做错了什么吗?
Direct Question: If I have two files with the same name (but in different directories), it appears that only Visual Studio 2005 can handle this transparently?? VS 2008 & 2010 require a bunch of tweaking? Aside from my naming convention, am I doing something wrong?
背景:
我正在开发 C++ 统计库...我有两个文件夹:
I'm developing C++ statistical libraries... I have two folders:
/ Univariate
Normal.cpp
Normal.h
Beta.cpp
Beta.h
Adaptive.cpp
Adaptive.h
/ Multivariate
Normal.cpp
Normal.h
Beta.cpp
Beta.h
Adaptive.cpp
Adaptive.h
我需要支持交叉编译——我在 Linux 中使用 g++/make 将这些相同的文件编译到一个库中.他们工作得很好.
I need to support cross compilation -- I'm using g++/make to compile these same files into a library in Linux. They work just fine.
我一直在使用 Visual Studio 2005 没有问题,但我需要升级到 Visual Studio 2008 或 2010(目前对 nVidia 的 nsight 工具垂涎三尺).但是,如果我将文件添加到具有相同名称的项目(即使它们位于不同的目录中),我会遇到问题.我愿意改变我的命名约定,但我很好奇其他人是否遇到过这个问题并找到了任何完善文档化的解决方案??
I had been using Visual Studio 2005 without issue, but I need to upgrade to Visual Studio 2008 or 2010 (currently drooling over nVidia's nsight tool). However, I'm having trouble if I add files to a project with the same name (even if they're in a different directory). I'm willing to change my naming convention, but I'm curious if others have encountered this problem and have found any well documented solutions??
如果我从 2005 项目升级到 2010 项目,VS 2010 似乎能够正确处理不同目录中具有相同名称的两个源文件,这让我更加困惑;但是,如果我删除重复文件之一,然后将其添加回项目,则会收到以下警告:
I'm further boggled by the fact that if I upgrade from 2005 projects to 2010 projects, it appears that VS 2010 is able to correctly handle two source files with the same name in different directories; however, if I remove one of the duplicate files and then add it back to the project I am greeted by the following warning:
DistributionsReleaseAdaptive.obj:警告 LNK4042:对象指定多次;忽略额外内容
DistributionsReleaseAdaptive.obj : warning LNK4042: object specified more than once; extras ignored
现在我将中间目录指定为 $(ProjectName)$(Configuration) -- 我需要将目标文件放在与源树不同的位置.所以我可以理解为什么它会在彼此之上复制目标文件,但是当项目从 2005 年转换到 2008 年或 2010 年时,会添加一堆条件编译:
Now I have the intermediate directory specified as $(ProjectName)$(Configuration) -- I need to have my object files in a different location from my source tree. So I can see why it's copying the object files on top of each other, but when the projects are converted from 2005 to 2008 or 2010, a bunch of conditional compiles are added:
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)%(Filename)1.obj</ObjectFileName>
<XMLDocumentationFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)%(Filename)1.xdc</XMLDocumentationFileName>
这些可以从 C/C++ 中的源文件属性页面访问 -> 输出文件 -> 对象文件名" &XML 文档文件名".但是如果我只是直接添加文件(或删除并重新添加它们),VS 不会抱怨,直到我尝试编译,而且从不添加条件指令——所以为了让事情正常工作,我必须自己为每个配置添加条件指令.我是在犯错误/糟糕的假设还是在 VS 2008/2010 中发现了一个有效的错误?
These are accessible from the Source file Properties page in C/C++ -> Output Files -> "Object File Name" & "XML Documentation File Name". But if I simply add the file directly (or remove and re-add them), VS doesn't complain until I try to compile, but also never adds the conditional directives -- So in order for things to work correctly, I have to add the conditional directives myself for every single configuration. Am I making a mistake / poor assumption or have I uncovered a valid bug in VS 2008 / 2010?
推荐答案
所以@Hans Passant 指出了正确的方向,谢谢!!您不必列出文件,一个文件夹就足够了.然后,如果您查看 VS 2010 列表底部的已定义宏,您将看到:
So @Hans Passant pointed in the right direction, Thanks!! You don't have to list the file, a folder is sufficient. Then if you look in the defined macros at the bottom of the VS 2010 list, you'll see:
%(RelativeDir)/单变量/
%(RelativeDir)/ Univariate/
所发布的问题实际上是我正在处理的问题的简化版本——单个项目中有几个级别的文件夹,并且存在一些名称冲突.因此,我真的很想以某种方式修复"它......
The problem, as posted, was actually a simplified version of what I'm working on -- a couple of levels of folders in a single project and there are a couple of name conflicts. Hence, I really wanted someway to just "fix" it...
如果您在解决方案资源管理器中右键单击项目,选择 C/C++ -> 输出文件"并在对象文件名"框中键入以下内容:
If you right click on the project in the solution explorer, choose C/C++ -> "Output Files" and type the following into the "Object File Name" box:
$(IntDir)/%(RelativeDir)/
$(IntDir)/%(RelativeDir)/
请注意,我还从下拉列表中选择了(所有配置、所有平台).这将编译镜像源树的目录层次结构中的每个文件.如果这些目录不存在,VS2010 将通过创建这些目录来开始构建.此外,对于那些讨厌目录名称中的空格的人来说,这个宏确实删除了所有空格,因此在使用它时无需使用双引号.
Note that I also selected (All Configurations, All Platforms) from the drop downs. This will compile every file in a directory hierarchy which mirrors the source tree. VS2010 will begin the build by creating these directories if they don't exist. Further, for those who hate white space in their directory names, this macro does remove all spaces, so there is no need to play around with double quotes when using it.
这正是我想要的——与我的 Makefile 在 Ubuntu 端的工作方式相同,同时仍然保持源代码树干净.
This is exactly what I wanted -- identical to the way my Makefiles work on the Ubuntu side, while still keeping the source tree clean.
这篇关于Visual Studio 2010 &2008 无法处理不同文件夹中同名的源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!