有没有办法抑制c ++名称mangling? [英] Is there a way to suppress c++ name mangling?

查看:113
本文介绍了有没有办法抑制c ++名称mangling?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的DLL,我想禁止几个导出方法的名称。这些方法是全局的,不是任何类的成员。有没有办法实现这个?

I have a DLL that is written in C++ and I want to suppress the name mangling for a few exported methods. The methods are global and are not members of any class. Is there a way to achieve this?

BTW:我使用VS2008。

BTW: I'm using VS2008.

推荐答案

bradtgmurray是正确的,但对于Visual C ++编译器,您需要显式导出函数。但是使用Serge-appTranslator提出的.DEF文件是错误的方法。

"bradtgmurray" is right, but for Visual C++ compilers, you need to explicitly export your function anyway. But using a .DEF file as proposed by "Serge - appTranslator" is the wrong way to do it.

使用declspec(dllexport / dllimport)指令,该指令适用于C和C ++代码,无论是否装饰(而.DEF仅限于C,除非您想用手工装饰你的代码)。

Using the declspec(dllexport/dllimport) instruction, which works for both C and C++ code, decorated or not (whereas, the .DEF is limited to C unless you want to decorate your code by hand).

因此,在Visual C ++中导出未加工函数的正确方法是结合导出C成语,如bradtgmurray和dllimport / dllexport关键字。

So, the right way to export undecorated functions in Visual C++ is combining the export "C" idiom, as answered by "bradtgmurray", and the dllimport/dllexport keyword.

例如,我在Visual C ++上创建了一个空DLL项目,并写了两个函数,一个被称为CPP,因为它被装饰,而另一个C,因为它不是。代码是:

As an example, I created on Visual C++ an empty DLL project, and wrote two functions, one dubbed CPP because it was decorated, and the other C because it wasn't. The code is:

// Exported header
#ifdef MY_DLL_EXPORTS
#define MY_DLL_API __declspec(dllexport)
#else
#define MY_DLL_API __declspec(dllimport)
#endif

// Decorated function export : ?myCppFunction@@YAHF@Z
MY_DLL_API int myCppFunction(short v) ;

// Undecorated function export : myCFunction
extern "C"
{
MY_DLL_API int myCFunction(short v) ;
} ;

我想你已经知道,但为了完整起见,MY_DLL_API宏定义在DLL makefile(即VCPROJ),但不是DLL用户。

I guess you already know, but for completeness' sake, the MY_DLL_API macro is to be defined in the DLL makefile (i.e. the VCPROJ), but not by DLL users.

C ++代码很容易写,但为了完整起见,

The C++ code is easy to write, but for completeness' sake, I'll write it below:

// Decorated function code
MY_DLL_API int myCppFunction(short v)
{
   return 42 * v ;
}

extern "C"
{

// Undecorated function code
MY_DLL_API int myCFunction(short v)
{
   return 42 * v ;
}

} ;

这篇关于有没有办法抑制c ++名称mangling?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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