Visual C ++:在def文件中导出装饰函数名 [英] Visual C++: Exporting decorated function name in a def file
问题描述
我要在def文件中导出装饰的函数名,如下所示:
LIBRARY示例
EXPORTS
?? 0__non_rtti_object @ std @@ QAE @ ABV01 @@ Z = myfunc @ 1
问题是链接器在第一个@ -sign处删除函数名,并将
?? 0__non_rtti_object放在导出表中。我的问题是现在,如果有一个方法,包括
的@字符?我使用Visual Studio 2010.也许有人可以帮助我。
先感谢Hannes。
前言
您没有回答我对使用.DEF文件的意见,所以我假设您不熟悉与 dllexport
和 dllimport
限定符。与它们一起,没有必要使用.DEF文件导出符号。
如果特别需要.DEF文件,使使用 dllimport
/ dllexport
功能,请忽略以下内容。
使用 dllimport
/ dllexport
?
头像(例如 public.hpp
),写成:
#ifdef MY_PROJECT_EXPORTS
pre>
#define MY_PROJECT_API __declspec(dllexport)
#else
#define MY_PROJECT_API __declspec(dllimport)
#endif
这样,宏
MY_PROJECT_API
将启用导出/导入符号。例如,稍后在同一个public.hpp
中,您可以声明://全局变量
MY_PROJECT_API int myGlobalVariable;
//函数
MY_PROJECT_API void my_function();
//类或结构
class MY_PROJECT_API MyClass
{
public:
int i;
virtual int foo();
//等
};
然后,您需要做的是,在您的库的项目选项中,定义
MY_PROJECT_EXPORTS
:这样,当你编译库时,上面的符号被声明为dllexport
,当有人包括你的public.hpp
头,以上符号将为dllimport
如果你的代码是跨平台的(
dllimport
/dllexport
是MS编译器特性)围绕编译器测试。例如:#ifdef _MSC_VER
//用于MS Visual Studio
#ifdef MY_PROJECT_EXPORTS
#define MY_PROJECT_API __declspec(dllexport)
#else
#define MY_PROJECT_API __declspec(dllimport)
#endif
#else
//其他编译器
#define MY_PROJECT_API
#endif
关于.DEF档案?
.DEF文件在之前使用,当可导出的C函数在Visual Studio上仍然是要走的路时。
对于强类型安全,C ++装饰其符号。
缺点是每个编译器都有自己的装饰方案
但是这样做的优点是:
- 您现在可以导出重载的/命名空间的函数/符号
- 参数类型是ABI的一部分, 和
dllexport
功能具有以下优点:
- 在源代码级别,而不是使用另一个项目文件
- 程序员现在可以忽略特定的装饰方案(通常只关注链接器),同时从强类型安全C ++扩展到链接器。
源
有关详细信息,请参阅:
__ declspec
: http://msdn.microsoft.com/en-us/library/dabb5z75.aspx
dllexport
,dllimport
: http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx
- 使用
dllimport
和dllexport
在C ++课程中: http://msdn.microsoft.com/en-us/library/81h27t8c.aspx
I want to export a decorated function name in a def file like this:
LIBRARY Example EXPORTS ??0__non_rtti_object@std@@QAE@ABV01@@Z=myfunc @1
The Problem is that the linker strips the function name at the first @-sign and places just "??0__non_rtti_object" into the export table. My question is now, if there is a way to include the @-characters as well? I use Visual Studio 2010. Maybe there is someone who can help me.
Thanks in advance, Hannes.
解决方案Preamble
You didn't answer my comment about the use of the .DEF file, so I assume you must be unfamiliar with the the
dllexport
anddllimport
qualifiers. With them, there is no need for the .DEF file to export symbols.If there is a particular need for the .DEF file that invalidate the use of the
dllimport
/dllexport
feature, please ignore the following.How to use
dllimport
/dllexport
?In your public header (say,
public.hpp
), write something like:#ifdef MY_PROJECT_EXPORTS #define MY_PROJECT_API __declspec(dllexport) #else #define MY_PROJECT_API __declspec(dllimport) #endif
This way, the macro
MY_PROJECT_API
will enable the export/import of your symbols. For example, later, in the samepublic.hpp
, you can declare:// A global variable MY_PROJECT_API int myGlobalVariable ; // A function MY_PROJECT_API void my_function() ; // A class or struct class MY_PROJECT_API MyClass { public : int i; virtual int foo() ; // etc. } ;
Then, what you need to do is, in the project options of your library, define the
MY_PROJECT_EXPORTS
: This way, when you compile your library, the symbols above are declareddllexport
, and when someone else includes yourpublic.hpp
header, the symbols above will bedllimport
And if your code is cross-platform (
dllimport
/dllexport
is a MS compiler feature), just wrap the defines above around a compiler test. For example:#ifdef _MSC_VER // For MS Visual Studio #ifdef MY_PROJECT_EXPORTS #define MY_PROJECT_API __declspec(dllexport) #else #define MY_PROJECT_API __declspec(dllimport) #endif #else // For other compilers #define MY_PROJECT_API #endif
About the .DEF file?
The .DEF file was used before, when exportable C functions were still "the way to go" on Visual Studio.
For strong type safety, C++ decorate its symbols.
The downside is that each compiler has its own decoration scheme (which never bothered me in 12-years of development), and that finding the exact, decorated name of a symbol can't be painful.
But the merits of that is that:
- You can now export overloaded/namespaced functions/symbols
- the parameter types are part of the ABI, meaning the linker can verify you aren't screwing up or cheating with your types
The
dllimport
anddllexport
features have the following advantages:
- it enables the export to be done at source level, instead of using yet another project file
- the programmer can now ignore the particular decoration scheme (which usually only interests the linker), all the while profiting from the strong type safety of C++ extended to the linker.
Sources
For more information, see:
__declspec
: http://msdn.microsoft.com/en-us/library/dabb5z75.aspxdllexport
,dllimport
: http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx- Using
dllimport
anddllexport
in C++ Classes : http://msdn.microsoft.com/en-us/library/81h27t8c.aspx这篇关于Visual C ++:在def文件中导出装饰函数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!