[英]generate vs2010 project file with my own source directories
在我的大多數c ++項目中,我想使用visual studio默認目錄結構中的不同目錄結構。 即:
/project
/build # put visual studio soluation and project files
/src # only put the c++ header files and source files
/bin # put the target executable files
/debug
/release
/tmp
/debug
/release
每次我在vs2010中創建一個solutaion我將配置這些目錄(例如OutputDirectory),但現在我真的很無聊。
那么有沒有一個工具可以根據我的配置文件自動生成vs2010解決方案和項目文件? 我唯一的要求是設置這些目錄。
您可以使用以下CMakeList實現該結構。 以下假定文件位於.../project/CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8) #every top-level project should start with this command; replace the version with the minimum you want to target
project(MyProjectName)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) # put .exe and .dll files here
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) # put .so files here (if you ever build on Linux)
set(CMAKE_MODULE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) # put .dll and .so files for MODULE targets here
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) # put .lib files here
# Note that for multi-configuration generators (like VS), the configuration name will be appended to the above directories automatically
# Now that the directories are set up, we can start defining targets
add_executable(MyExe src/myExe.cpp)
add_library(MyLib SHARED src/myDLL.cpp)
target_link_libraries(MyExe MyLib)
調用CMake時,將輸出目錄設置為.../project/build
(例如,在CMake GUI中)。 如果從命令行運行,請執行以下操作:
> cd .../project/build
> cmake .. -G "Visual Studio 10"
請注意,當輸出目錄是源目錄的子目錄時,某些生成器(Eclipse)不喜歡它。 對於這種情況,建議進行輕微的目錄重組。
您可以自己在C#中編寫這樣的工具,例如,查看Microsoft.Build.Construction
命名空間中的類,它們是用於以編程方式創建項目的。
但是,更簡單但更通用的選項是在所有項目中使用相同的屬性表,並設置所需的所有目錄路徑。 這也具有可重用的巨大優勢,因此如果您決定更改輸出目錄,則引用您的屬性表的所有項目都會自動受到影響。 例如:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MyMainDir>$(ProjectPath)\..\</MyMainDir>
<OutDir>$(MyMainDir)\bin\$(ConfigurationName)</OutDir>
<IntDir>$(MyMainDir)\tmp\$(ConfigurationName)</IntDir>
</PropertyGroup>
</Project>
這將首先找出你的'主目錄',即你的問題中名為'project'的那個,然后根據它和當前ConfigurationName
的名稱設置輸出和中間目錄,默認情況下是Debug
或Release
。
現在只需在項目中導入此屬性表:轉到View->Other Windows->Property Manager
,右鍵單擊項目,選擇Add Existing property Sheet
。 或者您可以在項目文件中手動添加<Import Project=....>
。
在您使用它時,您也可以在屬性表中添加編譯器/鏈接器選項,以便所有項目都使用相同的選項。 這需要一些時間,但將來可以節省大量時間,因為您不必一遍又一遍地更改項目設置中的相同選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.