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

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

问题描述

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

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.

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

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).

推荐答案

/ INCLUDE 指令可用于强制MSVC链接器包括符号。或者, / OPT:NOREF 可用于禁用删除未使用的符号。

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.

一个常见的方法是创建一个未使用的函数,引用为插件导出的所有对象。那么你只需要一个单独的/ INCLUDE指令为该函数。

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天全站免登陆