简体   繁体   中英

generate vs2010 project file with my own source directories

In most of my c++ projects, i want to use a different directory structure from visual studio default directory structure. ie:

/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

Everytime i create a solutaion in vs2010 i will config these directories (eg OutputDirectory), but now i'm really boring about this.

So is there a tool to generate vs2010 solution and project files automatically according my config file? And my only requirement is to set these directories.

You can achieve the structure with the following CMakeList. The following assumes the file is located at .../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)

When invoking CMake, set the output directory to .../project/build (eg in the CMake GUI). If running from the command line, do it like this:

> cd .../project/build
> cmake .. -G "Visual Studio 10"

Please note that some generators (Eclipse) don't like it when the output directory a subdirectory of the source directory. For such case, a slight directory re-structuring would be recommended.

You could write such a tool yourself in C# for instance, look at the classes in the Microsoft.Build.Construction namespace, they're made for creating projects programmatically.

However a simpler yet more versatile option is using the same property sheet in all your projects, and set all directory paths you need. This also has the huge advantage that it's reusable so should you ever decide to change your output directories, all projects referencing your property sheet are automatically affected. For example:

<?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>

This will first figure out your 'main dir' ie the one named 'project' in your question and then set the output and intermediate directories based on that and the name of the current ConfigurationName , which by default is Debug or Release .

Now it's just a matter of importing this property sheet in your project: go to View->Other Windows->Property Manager , right-click the project(s), select Add Existing property Sheet . Or you could manually add an <Import Project=....> in the project file.

While you're at it, you might as well also add compiler/linker options to the property sheet so all your projects use the same options. This takes some time now, but will save you tons of time in the future as you don't have to change the same options in the project settings over and over and over again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM