将同一项目中不同解决方案中的 #define 分开。

huangapple go评论78阅读模式
英文:

seperate #define on same project in different solutions

问题

我有一个C++项目,它包含在两个不同的解决方案中。我希望在每个解决方案下,该项目具有不同的#define常量。有没有办法做到这一点?

我最佳的解决方案是在其中构建不同的项目文件,但其中包含相同的C++代码,但这有点麻烦,我希望有一个更好的解决方案。

英文:

I have a c++ project which is included in two different solutions. I want the project to have different #define constants under each solution. Is there a way to do it?

My best solution is to build different project file with same c++ codes in it, but its a fuss, and I hope there is a better solution.

答案1

得分: 3

可以有条件地从相应的解决方案文件夹中提取特定于解决方案的属性表。

<ImportGroup Label="PropertySheets">
    <Import
        Project="$(SolutionDir)SolutionSpecific.props"
        Condition="exists('$(SolutionDir)SolutionSpecific.props')"
        Label="solution-specific props" />
</ImportGroup>

其中 SolutionSpecific.props 是一个包含宏定义的属性表文件。

英文:

It is possible to conditionally pull solution-specific property sheets from the corresponding solution folder.

<ImportGroup Label="PropertySheets">
    <Import
        Project="$(SolutionDir)SolutionSpecific.props"
        Condition="exists('$(SolutionDir)SolutionSpecific.props')"
        Label="solution-specific props" />
</ImportGroup>

where SolutionSpecific.props is a property sheet file containing macro definition.

答案2

得分: 1

你可以像这样做:

#include <iostream>

#ifdef Proj
    #define Bla "Klaf"
#else
    #define Bla "Flak"
#endif

int main()
{
    std::cout << Bla << '\n';
}

然后通过编译器的命令行参数控制 #ifdef

英文:

You can do something like this:

#include &lt;iostream&gt;

#ifdef Proj
    #define Bla &quot;Klaf&quot;
#else
    #define Bla &quot;Flak&quot;
#endif

int main()
{
    std::cout &lt;&lt; Bla &lt;&lt; &#39;\n&#39;;
}

And then control the #ifdef with a command line argument on your compiler.

Here the one that is compiling with -D Proj (this flag is for gcc, MSVC uses /D I think) prints Flak, and the other prints Klaf:

https://godbolt.org/z/TEKvrYdxG

huangapple
  • 本文由 发表于 2023年6月13日 14:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定