Visual Studio 2010 - 尝试根据目标 .exe 有条件地编译静态库

发布于 2024-10-22 11:44:27 字数 387 浏览 2 评论 0原文

我有一个用于生产代码和测试代码的静态库。我希望仅在构建测试 .exe 时才能注入测试数据。我想使用预处理器 #defines 来执行此操作,而不是使用 MSVS 配置(例如,调试与发布),因为我正在测试性能。调试构建会关闭优化,这在调试期间非常有用,但当我想测试性能时则不太好。

举个例子,假设我在 MSVS 中有 Foo.lib、Production.exe 和 Test.exe 项目。 Production.exe 和 Test.exe 都链接 Foo.lib。我希望 Production.exe 和 Test.exe 使用各自的预处理器定义重建 Foo.lib,因此 Foo.lib 中的代码将根据其目标可执行文件有条件地进行编译。

我对其他解决方案持开放态度,我希望我清楚地说明了我的问题。第一篇关于堆栈溢出的文章。

I have a static library that is used in production code and test code. I want to be able to inject test data only if I am building the test .exe. I would like to do this using preprocessor #defines, rather than MSVS configurations (e.g., Debug vs. Release) because I am testing performance. Debug builds turn off optimization, which is great during debugging but not so great when I want to test performance.

As an example, say I have Foo.lib, Production.exe, and Test.exe projects in MSVS. Production.exe and Test.exe both link Foo.lib. I would like Production.exe and Test.exe to rebuild Foo.lib with their respective preprocessor definitions, so the code in Foo.lib will conditionally compile based on which executable it is targeted for.

I'm open to other solutions, and I hope I stated my problem clearly. First post on stack overflow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

暖树树初阳… 2024-10-29 11:44:27

您需要设置多个解决方案配置来支持您想要的每个场景。首先,我们将向静态库添加一个新的测试配置,并创建一个随附的测试解决方案配置:

  • 在 Visual Studio 的解决方案资源管理器中,右键单击顶级解决方案节点。
  • 单击配置管理器...
  • 在项目列表中找到您的 C/C++ 静态库项目。单击该项目的配置单元格。它将变成一个下拉菜单。从该下拉列表中选择新建..
    • 输入新项目配置的名称。随便你怎么称呼它。在这些步骤中,我将其称为“测试”。
    • 对于复制设置自:字段,选择发布,因为您需要一个启用了优化的测试配置。
    • 保持选中创建新的解决方案配置复选框。
    • 点击确定

现在,您可以使用所需的 #defines 修改静态库的新测试配置:

  • 解决方案资源管理器中查找您的 C/C++ 项目。右键单击它并选择属性...
  • 从右上角的配置下拉列表中选择新的测试配置。
  • 在左侧窗格中,选择配置属性| C/C++ |预处理器
  • 在右侧,将所需的#defines 添加到预处理器定义列表中

...现在,您想要设置构建,以便拥有构建测试的构建配置。 exe 具有静态库的 Test 配置,Release.exe 具有库的 Release 配置。返回到配置管理器(正如我们在上面的前两个步骤中所做的那样):

  1. 为 Release.exe 设置构建配置:

    • 在左上角的下拉列表中,选择发布作为活动解决方案配置
    • 对于您的 Release.exe 项目,选择其Release 配置,并确保选中Build
    • 对于您的 Test.exe 项目,请确保未选中构建
    • 对于静态库,选择其Release 配置,并确保选中Build
  2. 设置 Test.exe 的构建配置:

    • 在左上角的下拉列表中,选择测试作为活动解决方案配置
    • 对于您的 Release.exe 项目,请确保未选中构建
    • 对于您的 Test.exe 项目,选择其发布配置(我假设这就是您想要的),并确保选中构建
    • 对于静态库,选择其测试配置,并确保选中构建

现在,当您将解决方案更改为测试发布配置时,我会期望您能够拥有您想要的每个构建行为。

You'll need to set up multiple solution configurations to support each of the scenarios you desire. First we'll add a new Test configuration to the static library, and create an accompanying Test solution configuration:

  • In Solution Explorer in Visual Studio, right click on the top-level solution node.
  • Click Configuration Manager...
  • Find your C/C++ static library project in the project list. Click the Configuration cell for that project. It will turn into a drop-down. Pick New.. from that drop-down.
    • Enter in a name for a new project configuration. Call it whatever you like. In these steps, I'll call it Test.
    • For the Copy settings from: field, select Release, since you want a test configuration that does have optimization enabled.
    • Leave the Create new solution configurations checkbox checked.
    • Click OK

Now you can modify your static library's new Test configuration with your desired #defines:

  • Find your C/C++ project in Solution Explorer. Right-click on it and select Properties...
  • Select your new Test configuration from the Configuration drop-down at the top-right.
  • In the left-hand pane, select Configuration Properties | C/C++ | Preprocessor
  • On the right, add your desired #defines to the list of Preprocessor Definitions

...now, you want to set your builds up so that you have a build configuration that builds Test.exe with the static library's Test configuration, and the Release.exe with the library's Release configuration. Go back to the Configuration Manager (as we did in the first two steps above):

  1. Set up your build configuration for Release.exe:

    • In the top-left drop down, select Release as the Active solution configuration:
    • For your Release.exe project, select its Release configuration, and make sure Build is checked
    • For your Test.exe project, make sure Build is unchecked.
    • For the static library, select its Release configuration, and make sure Build is checked
  2. Set up your build configuration for Test.exe:

    • In the top-left drop down, select Test as the Active solution configuration:
    • For your Release.exe project, make sure Build is unchecked.
    • For your Test.exe project, select its Release configuration (I assume that's what you want), and make sure Build is checked
    • For the static library, select its Test configuration, and make sure Build is checked

Now, when you change your solution to Test or Release configuration, I'd expect you to have the build behavior you desire for each.

有木有妳兜一样 2024-10-29 11:44:27

这听起来很错误,测试数据不属于.lib。测试以不同于目标机器上使用的方式构建的代码并不是真正的测试。 VS 让你远离麻烦,这是不可能的。您必须使用其他配置来更改.lib 的构建方式。

让测试应用程序将测试数据提供给 .lib。正如真正的应用程序在部署后为其提供真实数据一样。

This sounds very wrong, test data doesn't belong in a .lib. And testing code that's built in another way than it is used on the target machine is not a true test. VS keeps you out of trouble here, this just isn't possible. You have to use another configuration to change the way the .lib gets built.

Have the test app supply the test data to the .lib instead. Just as the real app supplies it with real data after you deployed it.

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