Is it possible to set a preprocessor macro in a sln file, and not in a project? (VS2008 C ++)

I support a large code base, and some vcproj files are used in different solutions. Due to some horrific configuration and dependencies, it seems that the best way to deal with some build problems is to #ifdef for the code, but for this I need to set the preprocessor definition at the solution file level, not at the vcproj level.

Is it possible?

How to do it?

+4
source share
6 answers

Select all projects in your solution. Project + Properties, C / C ++, Preprocessor, Preprocessor Definitions. Add

/DSOLUTION=$(SolutionName) 

Now you can check the value of the SOLUTION macro in the source code.

+4
source

I believe that you may need to create a project properties sheet with VS Project Manager , which all projects can inherit from. This will allow you to set any common project settings, including preprocessor macros, in one place and inherit them as needed.

+4
source

I finally find what suits me

in my "C: \ Users \\ AppData \ Local \ Microsoft \ MSBuild \ v4.0"

I changed it a 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, if "mysolution.props" lies next to "mysolution.sln", then I get a property sheet for the whole solution without changing anything inside my projects. This will become new functionality for my Visual Environement.

+3
source

2008 sln is really dumb, they only have lists of projects / files that can be placed in the solution explorer and project dependencies, so I don't think this is an option.

My gut instinct must do something with relative paths. For example, in your stdafx.h you can # include ".... \ project_configuration.h", then to build sln a you will check things in one directory and sln b another. Each of them will have a separate project_configuration.h.

I believe that you could do something similar with vsprops files, which are essentially #includes for vcproj files, although I found them a little annoying to maintain over time.

+1
source

Another approach:

Edit your vcxproj (or your vcxproj.user) with something like this

 <PreprocessorDefinitions Condition="'$(SolutionName)'=='NameOfYourSolution'"> YOUR_DEFINITION;%(PreprocessorDefinitions) </PreprocessorDefinitions> 

this is not ideal as it depends on your sln file name. It would be great if we instead use the $ (SolutionConfiguration) variable.

Unfortunately, I only see a variable for project configuration: $ (Configuration).

In any case, he does the trick ...

+1
source

Well, I also looked

Define a preprocessor value from the command line using MSBuild

and

msbuild defining conditional compilation characters

and that might work, but our build system is pretty fragile right now, and Iโ€™m not able to change all that.

The solution I came across was to clone the build configuration for the project in the solution and give it a different name. Then I added the macro / preprocessor definition to this new configuration.

He works as desired. Thus, one solution uses the old "release" configuration, and the other solution uses the "ReleaseSpecial" clone (not the name that I actually used) with different preprocessor settings.

Not perfect, but it works.

It would be nice if propoerties were easier to deal with SLN files that could go through in preprocessing defs.

0
source

All Articles