Reputation: 20360
I am maintaining a large codebase and some vcproj files are used in different solutions. Due to some horrendous configuration and dependencies it seems the best way to handle some build issues is to #ifdef the code but in order to do that I need to set a preprocessor definition at the solution file level and not at the vcproj level.
Is that possible?
How to do it?
Upvotes: 5
Views: 16036
Reputation: 91
I finally find somethings that suits me
in my "C:\Users\\AppData\Local\Microsoft\MSBuild\v4.0"
I change it a little bit for:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)\$(SolutionName).props" Condition="Exists('$(SolutionDir)\$(SolutionName).props')"/>
</Project>
So now, if a "mysolution.props" lays beside of "mysolution.sln" then I get a property sheet for the entire solution without changing anything inside my projects. It becomes a new features for my Visual Environement
Upvotes: 4
Reputation: 91
Another approch:
Edit your vcxproj (or your vcxproj.user) with something like this
<PreprocessorDefinitions Condition="'$(SolutionName)'=='NameOfYourSolution'">
YOUR_DEFINITION;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
it's not perfect as it depends on your sln filename. It would be great if we use a $(SolutionConfiguration) variable instead.
Unfortunatly, I only see variable for project configuration: $(Configuration).
In any case, it does the trick...
Upvotes: 1
Reputation: 20360
Well, I also looked at
Define a preprocessor value from command line using MSBuild
and
msbuild, defining Conditional Compilation Symbols
and those might work as well, however our build system is pretty brittle right now and I am not in a position to change it all.
The solution I came up with was to clone the build configuration for the project in a solution and give it a different name. Then I added a macros/preprocessor definition to that new configuration.
It appears to work as desired. So one solution uses the old "release" configuration and the other solution uses a clone "ReleaseSpecial" (not the name I really used) configuration with different preprocessor defs.
Not ideal, but it works.
It would be nice if the propoerties were easier to deal with or SLN files could pass in preprocessor defs.
Upvotes: 0
Reputation: 458
I believe what you may want to do is create a project property sheet with the VS Project Manager that all the projects could inherit from. This would allow you to set any common project settings, including preprocessor macros, in a single location and inherit them as you need.
Upvotes: 5
Reputation: 941218
Select all the projects in your solution. Project + Properties, C/C++, Preprocessor, Preprocessor Definitions. Add
/DSOLUTION=$(SolutionName)
You can now test the SOLUTION macro value in your source code.
Upvotes: 4
Reputation: 3358
2008 sln's are really dumb, they only have lists of projects/files to put in the solution explorer and project dependencies, so I don't think that's an option.
My gut instinct is to do something with relative paths. For example, in your stdafx.h's you could #include "....\project_configuration.h", then for building sln a, you'd check things out to one dir, and sln b another. Each would have its separate project_configuration.h.
I believe you could do something similar with vsprops files, which are essentially #includes for vcproj files, though I've found them a bit annoying to maintain over time.
Upvotes: 1