C++ - 建设图书馆
我正在 Microsoft Visual Studio 2008 SP1 中构建静态库(现在是 libpng
)。
假设我的库中只有 C 代码,我是否有可能为 Debug
和 Release
模式构建单个库(一个文件) ?
据我记得,例如,gtkmm
有它的预构建包,其中基于 C++ 的库在 Debug
和 Release
中提供版本,但其他 - 仅作为单个文件。
例如,他们有用于基于 C++ 的库的 gtkmm-vc90-d-2_4.lib
和 gtkmm-vc90-2_4.lib
文件,并且他们有单个库,例如 gtk -win32-2.0.lib
用于调试
和发布
配置。
如何才能达到同样的效果?我应该怎么做才能使构建的库(纯C)配置独立?
I'm building static libraries (right now libpng
) in Microsoft Visual Studio 2008 SP1.
Do I have any possibility to build single library (one file) for both Debug
and Release
modes assuming that my library has only C code in it?
As far as I remember, gtkmm
, for instance, has it's pre-built package, where C++ based libraries are shipped in both Debug
and Release
versions, but other - as a single file only.
E.g. they have gtkmm-vc90-d-2_4.lib
and gtkmm-vc90-2_4.lib
files for C++ based libraries and they have single libraries such as gtk-win32-2.0.lib
for both Debug
and Release
configurations.
How can I achieve the same effect? What should I do to make the built library (pure C) configuration-independent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理论上,如果库的所有外部标头(即由客户端拉入的标头)不使用#ifdef _DEBUG(或可能在 考虑这样的
情况:
在这种情况下,如果您要将库链接到您自己产品的调试版本中,那么 A 将具有与发布版本不同的大小,这意味着您'将有一些非常可怕的内存损坏错误需要追踪(曾经在那里......)
。
忘了提及,您应该将您的一个配置设为发布配置,这样您就不会最终引用调试 CRT,并且库也会得到优化。正如 lsalamon 指出的那样,创建一个 pdb 文件并将其与 .lib 文件一起保存将有助于将来的调试。
In theory you could build just one library if all the external headers for the library (ie the ones pulled in by the client) don't use #ifdef _DEBUG (or any other macro that may be defined in a debug build but not a release build.
Consider a case like this:
In this case, if you were to link the library into a debug build of your own product then A will have a different size to the release build, which means you're going to have some pretty horrendous memory corruption bugs to track down (been there...).
EDIT:
Forgot to mention, you should make your one configuration a Release configuration so that you don't end up with any references to the debug CRT, and also so the library is optimised. As lsalamon points out, creating a pdb file and saving this along with your .lib file will be useful in the future for debugging.
要在发布版本中包含调试信息,请使用此配置:
C/C++->常规->调试信息格式:程序数据库(/Zi)
和
链接器 -> 调试 -> 生成调试信息:是 (/DEBUG)
To include debug info at release version use this configuration :
C/C++->General->Debug Information Format: Program Database (/Zi)
and
Linker->Debugging->Generate Debug Info : Yes (/DEBUG)