visual-studio 是否可以在 sln 文件中而不是在项目中设置预处理器宏?(VS2008 C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3780035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to set preprocessor macro in sln file and not in a project? (VS2008 c++)
提问by Tim
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.
我正在维护一个大型代码库,并且在不同的解决方案中使用了一些 vcproj 文件。由于一些可怕的配置和依赖关系,处理某些构建问题的最佳方法似乎是#ifdef 代码,但为了做到这一点,我需要在解决方案文件级别而不是在 vcproj 级别设置预处理器定义。
Is that possible?
那可能吗?
How to do it?
怎么做?
回答by Daryl Hanson
I believe what you may want to do is create a project property sheet with the VS Project Managerthat 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.
我相信您可能想要做的是使用 VS项目管理器创建一个项目属性表,所有项目都可以从中继承。这将允许您在单个位置设置任何常见的项目设置,包括预处理器宏,并根据需要继承它们。
回答by Hans Passant
Select all the projects in your solution. Project + Properties, C/C++, Preprocessor, Preprocessor Definitions. Add
选择解决方案中的所有项目。项目 + 属性、C/C++、预处理器、预处理器定义。添加
/DSOLUTION=$(SolutionName)
You can now test the SOLUTION macro value in your source code.
您现在可以在源代码中测试 SOLUTION 宏值。
回答by Jan
I finally find somethings that suits me
我终于找到了适合我的东西
in my "C:\Users\\AppData\Local\Microsoft\MSBuild\v4.0"
在我的“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
所以现在,如果“mysolution.props”位于“mysolution.sln”旁边,那么我将获得整个解决方案的属性表,而无需更改我的项目中的任何内容。它成为我的视觉环境的新功能
回答by Jan
Another approch:
另一种方法:
Edit your vcxproj (or your vcxproj.user) with something like this
用这样的东西编辑你的 vcxproj(或你的 vcxproj.user)
<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.
它并不完美,因为它取决于您的 sln 文件名。如果我们改用 $(SolutionConfiguration) 变量,那就太好了。
Unfortunatly, I only see variable for project configuration: $(Configuration).
不幸的是,我只看到项目配置的变量:$(Configuration)。
In any case, it does the trick...
无论如何,它可以解决问题......
回答by Thomas
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.
2008 sln 真的很愚蠢,他们只有要放入解决方案资源管理器和项目依赖项的项目/文件列表,所以我不认为这是一个选项。
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.
我的直觉是用相对路径做一些事情。例如,在您的 stdafx.h 中,您可以 #include "....\project_configuration.h",然后为了构建 sln a,您需要检查一个目录,然后检查另一个目录 sln b。每个都有其单独的 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.
我相信你可以对 vsprops 文件做类似的事情,这些文件本质上是 vcproj 文件的#includes,尽管我发现随着时间的推移维护它们有点烦人。
回答by Tim
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.
它似乎按预期工作。因此,一种解决方案使用旧的“发布”配置,另一种解决方案使用具有不同预处理器定义的克隆“ReleaseSpecial”(不是我真正使用的名称)配置。
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.
如果属性更容易处理或者 SLN 文件可以传入预处理器 defs,那就太好了。

