Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. i.e:

/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 (e.g 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.

like image 774
kevin lynx Avatar asked Mar 27 '13 07:03

kevin lynx


People also ask

How do I add additional directories in Visual Studio 2022?

Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio. Select the Configuration Properties > C/C++ > General property page. Modify the Additional Include Directories property.

How do I change the build output directory in Visual Studio?

Right-click on the project node in Solution Explorer and select Properties. Expand the Build section, and select the Output subsection. Find the Base output path for C#, and type in the path to generate output to (absolute or relative to the root project directory), or choose Browse to browse to that folder instead.


2 Answers

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 (e.g. 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.

like image 189
Angew is no longer proud of SO Avatar answered Sep 20 '22 11:09

Angew is no longer proud of SO


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' i.e. 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.

like image 24
stijn Avatar answered Sep 20 '22 11:09

stijn