kev*_*ynx 6 c c++ cmake visual-studio-2010
在我的大多数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
Run Code Online (Sandbox Code Playgroud)
每次我在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)
Run Code Online (Sandbox Code Playgroud)
调用CMake时,将输出目录设置为.../project/build(例如,在CMake GUI中).如果从命令行运行,请执行以下操作:
> cd .../project/build
> cmake .. -G "Visual Studio 10"
Run Code Online (Sandbox Code Playgroud)
请注意,当输出目录是源目录的子目录时,某些生成器(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>
Run Code Online (Sandbox Code Playgroud)
这将首先找出您的“主目录”,即问题中名为“项目”的目录,然后根据该目录和当前目录的名称(默认情况下ConfigurationName为Debugor )设置输出和中间目录Release。
现在只需将此属性表导入到您的项目中即可:转到View->Other Windows->Property Manager,右键单击项目,然后选择Add Existing property Sheet。<Import Project=....>或者您可以在项目文件中手动添加。
当您这样做时,您还可以将编译器/链接器选项添加到属性表中,以便所有项目都使用相同的选项。现在这需要一些时间,但将来会为您节省大量时间,因为您不必一遍又一遍地更改项目设置中的相同选项。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           228 次  |  
        
|   最近记录:  |