使用MSVC强制符号导出 [英] Forcing symbol export with MSVC

查看:266
本文介绍了使用MSVC强制符号导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序和几个插件在DLL文件。插件通过导出库使用
应用程序中的符号。应用程序链接在几个静态库中,这是大多数符号来自的地方。只要应用程序使用符号,这可以正常工作。如果没有使用该符号,则在编译DLL时会收到链接器错误。



如何强制导出仅在插件中使用的符号?



为了触发导出,我已经尝试过这样的东西:

  MyClassExporter类
{
MyClass mInstance;
public:
MyClassExporter(){}
};
static MyClassExporter TheMyClassExporter;

在其中一个静态库中,应用程序是强制导出的,这没有工作



回应Greg(感谢答案)并澄清:我要强制导出的类是MyClass(它有__declspec(...)定义,取决于我想要导出或导入)。 MyClassExport是我试图强制将未使用(按应用程序)符号包含在应用程序中。我想要触摸符号,以便链接器将它们视为使用,并将它们包含在应用程序中,从而将它们导出到我的插件。将静态库链接到插件不是一个选项,因为它们包含将被重复的单例(应用程序和DLL都有自己的静态变量副本)。

解决方案

/ INCLUDE 指令可用于强制MSVC链接器包含一个符号。或者,可以使用 / OPT:NOREF 一般来说,禁止删除未使用的符号。



通常的方法是创建一个引用为插件导出的所有对象的未使用的函数。那么你只需要一个/ INCLUDE指令来执行该功能。


I have a application and several plugins in DLL files. The plugins use symbols from the application via a export library. The application links in several static libraries and this is where most of the symbols come from. This works fine as long as the application uses a symbol. If the symbol is not used there, I get linker errors when compiling the DLL.

How can I force the export of the symbols only used in the plugins?

In order to trigger the export I've tried something like this:

    class MyClassExporter
    {
        MyClass mInstance;
    public:
        MyClassExporter() {}
    };
    static MyClassExporter TheMyClassExporter;

in one of the static libs the application is made of to force the export, which didn't work.

In response to Greg (thanks for the answer) and to clarify: The class I want to force the export for is MyClass (which has __declspec(...) defined, depending on wether I want to export or import). MyClassExport was my attempt to force the inclusion of unused (in terms of the application) symbols into the app. I want to 'touch' the symbols so that the linker recognizes them as used and includes them into the application so that it can in turn export these to my plugins. Linking the static libs into the plugins is not an option, since they contain singletons which would be duplicated (app and DLLs each have their own copy of static variables).

解决方案

The /INCLUDE directive can be used to force the MSVC linker to include a symbol. Alternatively, /OPT:NOREF can be used to disable removal of unused symbols in general.

A common approach is to create a single unused function that references all objects exported for your plugins. Then you only need a single /INCLUDE directive for that function.

这篇关于使用MSVC强制符号导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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