Visual Studio 2010 - 尝试根据目标 .exe 有条件地编译静态库
我有一个用于生产代码和测试代码的静态库。我希望仅在构建测试 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要设置多个解决方案配置来支持您想要的每个场景。首先,我们将向静态库添加一个新的测试配置,并创建一个随附的测试解决方案配置:
现在,您可以使用所需的 #defines 修改静态库的新测试配置:
...现在,您想要设置构建,以便拥有构建测试的构建配置。 exe 具有静态库的 Test 配置,Release.exe 具有库的 Release 配置。返回到配置管理器(正如我们在上面的前两个步骤中所做的那样):
为 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:
Now you can modify your static library's new Test configuration with your desired #defines:
...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):
Set up your build configuration for Release.exe:
Set up your build configuration for Test.exe:
Now, when you change your solution to Test or Release configuration, I'd expect you to have the build behavior you desire for each.
这听起来很错误,测试数据不属于.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.