静态库中的 VC++ 资源 [英] VC++ resources in a static library

查看:60
本文介绍了静态库中的 VC++ 资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将资源构建到静态库中并通过简单地与库链接来重用它们?

Is it possible to build resources into a static library and reuse them by simply linking with the library?

我主要考虑的情况是,您调用库中的函数,而该函数又会访问资源.

I'm primarily thinking about the case where you call a function in the library which in turn accesses resources.

推荐答案

可以做到,但是很痛苦:你不能通过简单的链接静态库来做到.

It can be done, but it's quite painful: You can't do it by simply linking with the static library.

考虑一下:资源嵌入在 EXE 或 DLL 中.当静态库中的某些代码调用(例如)LoadIcon 时,它将从与其链接的 EXE 或 DLL 中获取资源.

Consider this: resources are embedded in an EXE or DLL. When some code in the static library calls (e.g.) LoadIcon, it'll get the resources from the EXE or DLL that it's linked with.

因此,如果您的静态库需要可用资源,您有几个选择:

So, if your static library requires resources to be available, you've got a couple of options:

  1. 您可以让库动态构建它们,然后使用(例如)CreateDialogIndirect.请参阅 Raymond Chen 的 在运行时构建对话框模板".
  2. 您可以将它们作为简单数组嵌入到库中(即)char my_dialog_resource[] = { .... };,然后使用(例如)CreateDialogIndirect.您可能需要找到(或编写)一个将 .RES 文件转换为 .CPP 文件的实用程序.
  3. 您可以随同一个资源脚本(.RC 文件)和相应的头文件一起发布 LIB 文件.然后您将它们#include 视为相关.您需要为 LIB 保留一系列资源 ID 以供使用,以免它们与主 EXE 或 DLL 的资源 ID 发生冲突.这就是 MFC 在用作静态库时所做的.或者您可以使用字符串资源 ID(这不适用于 STRINGTABLE 资源).
  4. 您的静态库可以附带单独的资源 DLL.
  1. You can have the library build them on the fly, and then use (e.g.) CreateDialogIndirect. See Raymond Chen's "Building a dialog template at run-time".
  2. You can have them embedded in the library as simple arrays (i.e.) char my_dialog_resource[] = { .... };, and then use (e.g.) CreateDialogIndirect. You'll probably need to find (or write) a utility that converts from .RES files to .CPP files.
  3. You can ship the LIB file with a resource script (.RC file) and corresponding header file. You then #include them as relevant. You'll need to reserve a range of resource IDs for the LIB to use, so that they don't collide with those of the main EXE or DLL. This is what MFC does when used as a static library. Or you can use string resource IDs (this doesn't work for STRINGTABLE resources).
  4. Your static library can ship with a separate resource DLL.

这篇关于静态库中的 VC++ 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆