Learning how to use SVN properly is a difficult task. But don’t be afraid! Here I want to teach you a good pattern for using SVN with Visual Studio projects. It is important to seperate source code, interim build files, build output, vendor directories and include headers properly from each other. Ever heard of SVN properties? We use them to exclude compiler generated files and link dependency-repositories (svn:externals) from your daily commit window, so that you only see the important things. You will end up falling in love with SVN.
We are going to need the following components in this tutorial. Once you understand this pattern you will be able to replace them if desired.
Finding the right host for your project can be difficult. There are multiple categories of hosts available:
In my opinion, OSDN is the best place to be hosted at so try to get a project here. If you are denied then try to look up using “best free SVN” or “list of source code hosts”.
Try creating a SVN repository at your host. It should come with appropriate documentation on how to do so.
In your SVN settings you should find a “check-out” URL. Copy it into your clipboard.
Select a directory location where you want to check-out your project in. Then right click into the folder and select “SVN Checkout”. You should see the Checkout dialog open up.
Leave everything else as is. When finished, click OK. You will be asked for username and password. Put in the credentials that you registered with at your SVN provider. If it does not ask for credentials then you will have to put them in once you start pushing code into the repository (soon ☺).
There are two templates I want to teach you. On the left you see a binary template and on the right a library template.
Binary Template: This template is used for DLL and EXE files.
Library Template: This template is used for LIB files.
Even though those templates are similar they must be separated. The include and src folders should be treated the same in this tutorial. We are going to focus on the Binary Template.
Now we want to change the actual repository. Select all folders that we created, right click and hit the option “TortoiseSVN -> Add”. This will mark the folders for addition during next commit and enable properties for them.
Select the “bin” and “obj” folders, right click on them and hit “Settings”. In the Windows dialog select the “Subversion” tab. This tab is provided by the TortoiseSVN shell extension to simplify a lot of things. We want to click on “Properties…” next.
You should see an empty list of Properties. Click on “New…”. The dropdown shows many possible SVN properties for application. Our goal is to ignore all content from inside of our selected folders. So select the “Other” option. Fill out the dialog as shown above and click OK on both the dialogs.
This is how you modify SVN properties of folders!
Inside of the “vendor” directory you should put all projects that you want to use but are a direct product of this repository. It is recommended that those projects are hosted on separate repositories. If they are hosted on another SVN repository right click on the “vendor” directory and add a new “Externals” property.
Make sure you add the vendors into correct local paths and their up-to-date remote SVN URL. Once you are finished adding go to your SVN root folder, right click into it and hit “SVN Update”. TortoiseSVN will fetch all externals into their folders automatically.
I assume you know how to develop applications using Visual Studio. So launch the IDE and create an empty project inside of the “build” folder of our repository.
Once ready click on “OK”. It should create a new folder inside of “build” called after the project name. We do not want that. Go into that folder, select all files, ctrl+x to cut, go into “build”, paste the files into “build” and delete the unnecessary folder.
Now it is time to set up the Visual Studio project itself. Remember how we created “bin” and “obj” folders? We have to instruct Visual Studio to use them. Launch the solution file of your project and open the project properties.
Make sure that you select All Configurations and All Platforms. Then change the following properties.
Optionally, it is recommended that you enable the static CRT.
If you added vendors then now is the time to add include paths and add library paths to them as relative paths. It is especially simple to do if all your vendors use the same rules as in this tutorial, for example.
The zlib project does use the same template. Because of that you can use the $(Platform) and $(Configuration) macros to select the correct lib version.
After that you have to add the .lib files as Additional Dependencies under Linker -> General. For all projects that use our template you can simply add “*project_name*.lib”.
Congratulations! Now your project is ready to compile source code!
Before you can add include directories you need to add C++ files to flag the project as C++ project. It is recommended to start off with a main.cpp file and a main.h file. To add them to Visual Studio simply drag+drop them on the project.
If you want to add an entire source folder consider using wildcard paths instead. For this you have manually edit your .vcxproj file.
Add the following lines into an ItemGroup node just before the targets include (as shown in the image above):
<ClCompile Include="..\src\**\*.cpp" /> <ClInclude Include="..\include\**\*.h" /> <ClInclude Include="..\src\**\*.h" /> <ClInclude Include="..\src\**\*.hxx" />
The double asterisk stands for recursive scanning. In the filename part is a regular wildcard to match files with.
When adding source code remember to right click and perform “TortoiseSVN -> Add” on the files! Never forget to push source code into the remote codebase.
Once you are confident that everything is compiling correctly it is time to push the code to our SVN host.
Right click into the root folder of your repository and select “SVN Commit”. In this dialog you see the current status of your SVN working copy (file list). Take your time to review the changes. Make sure your Commit will not break anything. Give your Commit a meaningful message. And then click “OK”.