解决方案范围#define
有没有办法全局声明#define?
就像我想要一个包含例如的文件,
#define MONO
并且我希望所有源代码文件都知道定义了这个预处理器指令。我将如何实现这一目标?
Is there a way to globally declare a #define?
Like I want to have a file that has for instance,
#define MONO
and I want all source-code files to know that this pre-processor directive is defined. How would I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
更新:您不能对“解决方案范围”进行定义,但是下面的答案在每个项目的基础上都是可行的。
您可以在编译属性或构建选项中设置它们:
http ://msdn.microsoft.com/en-US/library/76zdzba1(v=VS.80).aspx (VS2008)
http://msdn.microsoft.com/en- US/library/76zdzba1(v=VS.100).aspx (VS2010)
请参阅“设置自定义常量”标题。
更新
有关构建选项的 Microsoft 文档
您可以通过右键单击项目来访问构建选项,然后从菜单中选择属性。
Update: You cannot do a "solution-wide" define afaik, however the answer below is workable on a per-project basis.
You set them in your Compilation Properties or Build options:
http://msdn.microsoft.com/en-US/library/76zdzba1(v=VS.80).aspx (VS2008)
http://msdn.microsoft.com/en-US/library/76zdzba1(v=VS.100).aspx (VS2010)
see the "To set a custom constant" heading.
Update
Microsoft Documentation on Build Options
You get to the build options by right-clicking the project and selecting properties from the menu.
我知道 C# 项目的解决方案(我没有对任何其他项目进行测试)
例如,您有:
解决方案目录中创建
SolutionDefines.targets
文件在每个项目文件的
添加:在
Solution1\ SolutionDefines.targets
添加:在
Solution2\SolutionDefines.targets
添加:在本例中,您有:
对于 Solution1 - 所有项目都添加了
TRACING_BUILD
定义对于 Solution2 - 全部项目已添加
ANOTHER_DEFINE
定义对于解决方案 3 - 所有项目 - 未添加定义
在此方法中,您必须将具有解决方案范围定义的所有解决方案存储在单独的目录中
I know solution for C# projects (I don't tested it for any other projects)
For example you have:
Create
SolutionDefines.targets
file in solution directoryin each project file add:
In
Solution1\SolutionDefines.targets
add:In
Solution2\SolutionDefines.targets
add:In this case you have:
For Solution1 - all projects have
TRACING_BUILD
define addedFor Solution2 - all projects have
ANOTHER_DEFINE
define addedFor Solution3 - all projects - no defines added
In this approach you must store all solutions with solution wide defines in separate directories
几年后,与 Alexei 的答案类似,但本质上受到支持。
NuGet.Config 文件的
Directory.Build.props
人们可以根据https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view= vs-2019
我们的看起来像:
它有效地将其包含到 SLN 中的所有 CSPROJ 文件中。由于某种原因,通过谷歌很难找到特定的解决方案。自 MSBuild 15 以来一直存在
Years later, and similar to Alexei's answer but supported innately.
One can make a
Directory.Build.props
similar to aNuGet.Config
file as perhttps://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019
Ours looks like:
And it effectively includes this into all CSPROJ files in your SLN. For some reason that particular solution is insanely hard to find via google. Been around since MSBuild 15
我认为没有办法创建解决方案范围#define。您可以为每个项目/程序集创建一个,正如其他答案所描述的那样,但是如果您需要了解所有源代码文件,则需要为解决方案中的每个项目执行此操作那个#define。
I don't think there is a way to create a solution-wide #define. You can create one for each project/assembly, as the other answers have described, but you'll need to do this for each and every project in the solution if you need all source code files to know of that #define.
要扩展 @Murtago 和 @Mark Cidade 的答案,使其适用于解决方案:
打开解决方案的配置管理器并创建一个新配置(从最接近的匹配复制),然后更改需要不同条件符号的项目。
To expand on the answers @Murtago and @Mark Cidade to make it solution wide:
Open the configuration manager for the solution and create a new configuration (copied from the closest match) then change conditional symbols for the projects that need different ones.
如果解决方案中同时有 C# 和 C++ 项目并且需要在它们之间共享预处理器定义。
在
*.sln
附近创建一些SolutionDefines.targets
文件,如下所示然后添加到 C/C++/C#
*.vcproj
/* .vcxproj
/*.csproj
项目将此行文件放在最后的某个位置好的,现在您可以在 C# 中使用
#if MY_DEFINITION
和#ifdef MY_DEFINITION在 C/C++ 中。 Visual Studio C# 编辑器不喜欢这种方式,并告诉您没有定义
MY_DEFINITION
。但是,C# 编译器 csc.exe 可以正确获取符号。In case you have both C# and C++ projects in the solution and need to share preprocessor definitions among them.
Create some
SolutionDefines.targets
file near your*.sln
as followsThen add to C/C++/C#
*.vcproj
/*.vcxproj
/*.csproj
project files this line somewhere in the endOk, now you can enjoy
#if MY_DEFINITION
in C# and#ifdef MY_DEFINITION
in C/C++. Visual Studio C# editor doesn't like this way and tells there is noMY_DEFINITION
defined. But, C# compiler csc.exe gets the symbol properly.还有一种解决方法,如果我错了,请纠正我:
例如,
ProjectA
引用ProjectB
,并且ProjectsCommon
包含基本静态变量。这两个项目都引用ProjectsCommon
。ProjectsCommon
中的代码:ProjectA
中的代码:ProjectB
中的代码:但是,应首先运行
ProjectA
中的代码。当ProjectA
是 bootstarapper 项目时,这可能很好。There is one more workaround, correct me if i'm wrong:
For example,
ProjectA
referencesProjectB
, andProjectsCommon
contains base static variable. Both projects referneceProjectsCommon
.Code in
ProjectsCommon
:Code in
ProjectA
:Code in
ProjectB
:However, code in
ProjectA
should run first. This is probably good whenProjectA
is a bootstarapper project.在配置管理器中创建新的解决方案配置,并确保选中该框以创建新的项目配置。然后在每个项目的“构建”属性中的“条件编译符号”框中输入“MONO”。
Create a new solution configuration in Configuration Manager and make sure that the box is checked to create new project configurations as well. And then in the Build properties of each project enter
MONO
into the Conditional compilation symbols box.在 vs 2013 中,您可以使用 /define:x 或 /d:x http:// msdn.microsoft.com/en-us/library/0feaad6z.aspx
in vs 2013 you can use /define:x or /d:x http://msdn.microsoft.com/en-us/library/0feaad6z.aspx
预处理器指令的正确位置是 VS 构建配置。如果您使用命令行来构建解决方案,您还可以使用 /define 选项传递它。
The proper place for a pre-processor directive is the VS build configuration. If you are using a command line to build your solution you can also pass it using /define option.
我不知道解决方案范围内的定义,但我可以通过执行以下操作快速将预处理器定义添加到所有项目:
C/C++
-->预处理器
预处理器定义
旁边的小向下箭头,<编辑...>
<不同选项>后添加我的定义;
OK
在菜单中我正在使用带有 C 代码库的 Visual Studio Community 2019。
I don't know of a solution wide definition, but I was able to quickly add a preprocessor definition to all projects by doing the following:
Properties
C/C++
-->Preprocessor
Preprocessor Definitions
<Edit...>
<different options>
OK
out of the menusI'm using Visual Studio Community 2019 with a C code base.