問題描述
直接問題:如果我有兩個同名的文件(但在不同的目錄中),似乎只有 Visual Studio 2005 可以透明地處理這個??VS 2008 &2010 需要大量調(diào)整?除了我的命名約定,我做錯了什么嗎?
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?
背景:
我正在開發(fā) C++ 統(tǒng)計庫...我有兩個文件夾:
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 似乎能夠正確處理不同目錄中具有相同名稱的兩個源文件,這讓我更加困惑;但是,如果我刪除重復(fù)文件之一,然后將其添加回項目,則會收到以下警告:
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:對象指定多次;忽略額外內(nèi)容
DistributionsReleaseAdaptive.obj : warning LNK4042: object specified more than once; extras ignored
現(xiàn)在我將中間目錄指定為 $(ProjectName)$(Configuration) -- 我需要將目標(biāo)文件放在與源樹不同的位置.所以我可以理解為什么它會在彼此之上復(fù)制目標(biāo)文件,但是當(dāng)項目從 2005 年轉(zhuǎn)換到 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 不會抱怨,直到我嘗試編譯,而且從不添加條件指令——所以為了讓事情正常工作,我必須自己為每個配置添加條件指令.我是在犯錯誤/糟糕的假設(shè)還是在 VS 2008/2010 中發(fā)現(xiàn)了一個有效的錯誤?
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/
所發(fā)布的問題實際上是我正在處理的問題的簡化版本——單個項目中有幾個級別的文件夾,并且存在一些名稱沖突.因此,我真的很想以某種方式修復(fù)"它......
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++ -> 輸出文件"并在對象文件名"框中鍵入以下內(nèi)容:
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)/
請注意,我還從下拉列表中選擇了(所有配置、所有平臺).這將編譯鏡像源樹的目錄層次結(jié)構(gòu)中的每個文件.如果這些目錄不存在,VS2010 將通過創(chuàng)建這些目錄來開始構(gòu)建.此外,對于那些討厭目錄名稱中的空格的人來說,這個宏確實刪除了所有空格,因此在使用它時無需使用雙引號.
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.
這篇關(guān)于Visual Studio 2010 &2008 無法處理不同文件夾中同名的源文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!