解决方案范围#define

发布于 2024-10-19 19:06:09 字数 134 浏览 2 评论 0原文

有没有办法全局声明#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

山田美奈子 2024-10-26 19:06:09

更新:您不能对“解决方案范围”进行定义,但是下面的答案在每个项目的基础上都是可行的。

您可以在编译属性或构建选项中设置它们:

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.

Project Build Options

划一舟意中人 2024-10-26 19:06:09

我知道 C# 项目的解决方案(我没有对任何其他项目进行测试)

例如,您有:

Project1\
Project2\
Solution1\Solution1.sln
Solution2\Solution2.sln

解决方案目录中创建 SolutionDefines.targets 文件

Project1\
Project2\
Solution1\Solution1.sln
Solution1\SolutionDefines.targets
Solution2\Solution2.sln
Solution2\SolutionDefines.targets
Solution3\Solution2.sln
Solution3\|no target file|

在每个项目文件的

<Import Project="$(SolutionDir)SolutionDefines.targets" Condition="exists('$(SolutionDir)SolutionDefines.targets')" />

添加:在 Solution1\ SolutionDefines.targets 添加:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <DefineConstants>$(DefineConstants);TRACING_BUILD</DefineConstants>
    </PropertyGroup>
</Project>

Solution2\SolutionDefines.targets 添加:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <DefineConstants>$(DefineConstants);ANOTHER_DEFINE</DefineConstants>
    </PropertyGroup>
</Project>

在本例中,您有:

对于 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:

Project1\
Project2\
Solution1\Solution1.sln
Solution2\Solution2.sln

Create SolutionDefines.targets file in solution directory

Project1\
Project2\
Solution1\Solution1.sln
Solution1\SolutionDefines.targets
Solution2\Solution2.sln
Solution2\SolutionDefines.targets
Solution3\Solution2.sln
Solution3\|no target file|

in each project file add:

<Import Project="$(SolutionDir)SolutionDefines.targets" Condition="exists('$(SolutionDir)SolutionDefines.targets')" />

In Solution1\SolutionDefines.targets add:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <DefineConstants>$(DefineConstants);TRACING_BUILD</DefineConstants>
    </PropertyGroup>
</Project>

In Solution2\SolutionDefines.targets add:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <DefineConstants>$(DefineConstants);ANOTHER_DEFINE</DefineConstants>
    </PropertyGroup>
</Project>

In this case you have:

For Solution1 - all projects have TRACING_BUILD define added

For Solution2 - all projects have ANOTHER_DEFINE define added

For Solution3 - all projects - no defines added

In this approach you must store all solutions with solution wide defines in separate directories

淡紫姑娘! 2024-10-26 19:06:09

几年后,与 Alexei 的答案类似,但本质上受到支持。

NuGet.Config 文件的 Directory.Build.props

人们可以根据https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view= vs-2019

我们的看起来像:

<Project>
    <PropertyGroup>
        <DefineConstants>RC_427</DefineConstants>
    </PropertyGroup>
</Project>

它有效地将其包含到 SLN 中的所有 CSPROJ 文件中。由于某种原因,通过谷歌很难找到特定的解决方案。自 MSBuild 15 以来一直存在

Years later, and similar to Alexei's answer but supported innately.

One can make a Directory.Build.props similar to a NuGet.Config file as per

https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019

Ours looks like:

<Project>
    <PropertyGroup>
        <DefineConstants>RC_427</DefineConstants>
    </PropertyGroup>
</Project>

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

等你爱我 2024-10-26 19:06:09

我认为没有办法创建解决方案范围#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.

知足的幸福 2024-10-26 19:06:09

要扩展 @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.

enter image description here

心作怪 2024-10-26 19:06:09

如果解决方案中同时有 C# 和 C++ 项目并且需要在它们之间共享预处理器定义。
*.sln 附近创建一些 SolutionDefines.targets 文件,如下所示

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <DefineConstants>$(DefineConstants);MY_DEFINITION</DefineConstants>
    </PropertyGroup>
  <ItemDefinitionGroup>
    <ClCompile>
      <PreprocessorDefinitions>MY_DEFINITION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>
</Project>

然后添加到 C/C++/C# *.vcproj/* .vcxproj/*.csproj 项目将此行文件放在最后的某个位置

<Import Project="$(SolutionDir)SolutionDefines.targets" Condition="exists('$(SolutionDir)SolutionDefines.targets')" />

好的,现在您可以在 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 follows

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <DefineConstants>$(DefineConstants);MY_DEFINITION</DefineConstants>
    </PropertyGroup>
  <ItemDefinitionGroup>
    <ClCompile>
      <PreprocessorDefinitions>MY_DEFINITION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>
</Project>

Then add to C/C++/C# *.vcproj/*.vcxproj/*.csproj project files this line somewhere in the end

<Import Project="$(SolutionDir)SolutionDefines.targets" Condition="exists('$(SolutionDir)SolutionDefines.targets')" />

Ok, 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 no MY_DEFINITION defined. But, C# compiler csc.exe gets the symbol properly.

孤独患者 2024-10-26 19:06:09

还有一种解决方法,如果我错了,请纠正我:

例如,ProjectA 引用ProjectB,并且ProjectsCommon 包含基本静态变量。这两个项目都引用 ProjectsCommon

ProjectsCommon 中的代码:

[assembly: InternalsVisibleTo("ProjectA")]

------------------------------------------

public class ConditionVariables
{
    public static bool IsABC { get; internal set; } = false;
}

ProjectA 中的代码:

#if ABC
    ProjectsCommon.ConditionVariables.IsABC = true;
#endif

ProjectB 中的代码:

if (ProjectsCommon.ConditionVariables.IsABC )
{
    // first scenario
}
else
{
    // second one
}

但是,应首先运行 ProjectA 中的代码。当 ProjectA 是 bootstarapper 项目时,这可能很好。

There is one more workaround, correct me if i'm wrong:

For example, ProjectA references ProjectB, and ProjectsCommon contains base static variable. Both projects refernece ProjectsCommon.

Code in ProjectsCommon:

[assembly: InternalsVisibleTo("ProjectA")]

------------------------------------------

public class ConditionVariables
{
    public static bool IsABC { get; internal set; } = false;
}

Code in ProjectA:

#if ABC
    ProjectsCommon.ConditionVariables.IsABC = true;
#endif

Code in ProjectB:

if (ProjectsCommon.ConditionVariables.IsABC )
{
    // first scenario
}
else
{
    // second one
}

However, code in ProjectA should run first. This is probably good when ProjectA is a bootstarapper project.

爱本泡沫多脆弱 2024-10-26 19:06:09

在配置管理器中创建新的解决方案配置,并确保选中该框以创建新的项目配置。然后在每个项目的“构建”属性中的“条件编译符号”框中输入“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.

酒解孤独 2024-10-26 19:06:09

在 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

夏夜暖风 2024-10-26 19:06:09

预处理器指令的正确位置是 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.

一杯敬自由 2024-10-26 19:06:09

我不知道解决方案范围内的定义,但我可以通过执行以下操作快速将预处理器定义添加到所有项目:

  1. 按住 ctrl 并选择每个项目,
  2. 右键单击任何选定的项目并选择“属性” >
  3. 转到C/C++ --> 预处理器
  4. 单击预处理器定义旁边的小向下箭头,
  5. 选择<编辑...>
  6. <不同选项>后添加我的定义;
  7. 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:

  1. hold ctrl and select each project
  2. right click on any selected project and choose Properties
  3. go to C/C++ --> Preprocessor
  4. click the little down arrow beside Preprocessor Definitions
  5. choose <Edit...>
  6. add my definition after <different options>
  7. OK out of the menus

I'm using Visual Studio Community 2019 with a C code base.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文