在gcc中提供函数别名? [英] Providing a function alias in gcc?

查看:75
本文介绍了在gcc中提供函数别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,用于在使用GCC编译的DLL中为导出的函数定义一个未损坏的别名.

I'm looking for a way to define an unmangled alias for an exported function in a DLL compiled with GCC.

根据此StackOverflow答案,Visual Studio支持

According to this StackOverflow answer, Visual Studio supports

#pragma comment(linker, "/EXPORT:SomeFunction=_SomeFunction@@@23mangledstuff#@@@@")

指定导出的函数应具有给定的别名.我正在寻找GCC中的类似内容. GCC函数属性文档建议 __ attribute__((别名("target")))可以解决问题,但这只会生成错误的函数名称:

to specify that an exported function shall have a given alias. I'm looking for something similar in GCC. The GCC function attribute docs suggest __attribute__ ((alias ("target"))) will do the trick, but this just generates a mangled function name:

#undef DLL_EXPORT
#ifdef CCDLL_EXPORTS
#define DLL_EXPORT __stdcall __declspec(dllexport)
#else
#define DLL_EXPORT __stdcall __declspec(dllimport)
#endif

DLL_EXPORT CCDLL_v1* GetCCDLL_premangled();

DLL_EXPORT CCDLL_v1* GetCCDLL () __attribute__ ((alias ("_Z19GetCCDLL_premangledv")));

这是生成的DLL中的.def文件:

and here's the .def file from the resulting DLL:

EXPORTS
    _Z19GetCCDLL_premangledv @1
    _Z8GetCCDLLv @2

原始函数已按预期导出和处理,但函数别名也是如此.如果我从函数别名中删除 DLL_EXPORT ,我希望它不会被破坏,但也不会从DLL中导出.我以为stdcall调用约定可能会破坏函数别名,但是当我从 DLL_EXPORT 定义中删除它时,.def文件仍然完全相同.

The original function is exported and mangled as expected, but so is the function alias. If I drop the DLL_EXPORT from the function alias, I expect it would remain unmangled, but it's also not exported from the DLL. I thought the stdcall calling convention might be mangling the function alias, but when I removed it from the DLL_EXPORT definition, the .def file remained exactly the same.

此SO答案建议我可以使用-defsym = alias_name = target ,但ld似乎无法识别这一点,因此我在所有官方GCC链接器文档.

This SO answer suggests I can define an alias using --defsym=alias_name=target, but ld doesn't seem to recognize this and I can't find anything in the official GCC linker docs at all.

有没有一种方法可以满足我的需求,或者GCC是否不支持导出功能的完整别名?

Is there a way to do what I'm looking for, or does GCC just not support unmangled aliases for exported functions?

推荐答案

您是否尝试过.def文件:

Did you try in your .def file:

normalName=mangled_name @ 1

另一种方法可能是定义一个调用内部C ++的extern"C"函数,然后将其导出.

Another way may be to define an extern "C" function that calls the internal C++ one, and export that.

这篇关于在gcc中提供函数别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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