在运行时检查程序中函数是否存在

发布于 2024-07-13 01:37:54 字数 590 浏览 8 评论 0原文

我正在编写一个程序,该程序旨在通过一些函数定义进行扩展。 编译程序的方法之一是创建一个将代码链接到主代码的可执行文件。 问题是:要定义的函数之一是可选的,我需要对此进行测试。

在 Linux 上,我正在做的事情是:

使用 g++ 的“-rdynamic”选项或 ld 的“--export-dynamic”选项编译程序。 然后,像这样使用ldsym:

fct_type myfct = (fct_type)dlsym(RTLD_DEFAULT, "fct");

如果程序中存在函数“fct”,则返回其地址,否则返回NULL。

现在,在 Windows 上,我应该能够这样做:

dll_handle = GetModuleHandle(0);
fct_type myfct = (fct_type)GetProcAddress(dll_handle, "fct");

但是 MinGW32 上的 g++ 没有“-rdynamic”或“--export-dynamic”选项! 所以这是行不通的。 有谁知道在 Windows 上使用 MinGW32 做什么?

I am writing a program that is meant to be extended by some function definitions. One of the way of compiling the program is to create a single executable linking your code to the main code. The problem is: one of the function to define is optional and I need to test for that.

On Linux, here is what I am doing:

Compile the program with the "-rdynamic" option to g++ or "--export-dynamic" option to ld. Then, Use ldsym like this:

fct_type myfct = (fct_type)dlsym(RTLD_DEFAULT, "fct");

If the function "fct" exists in the program, this will return its address, otherwise, it returns NULL.

Now, on Windows, I am supposed to be able to do so:

dll_handle = GetModuleHandle(0);
fct_type myfct = (fct_type)GetProcAddress(dll_handle, "fct");

But there is no "-rdynamic" or "--export-dynamic" option to g++ on MinGW32! So this doesn't work. Does anybody knows what to do on windows with MinGW32 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2024-07-20 01:37:54

好吧,最后,我会回答我自己的问题......

你必须链接到标志 -Wl,--export-all-symbols 并且它可以工作......

Ok, so in the end, I will answer my own question ...

You have to link with the flag -Wl,--export-all-symbols and it works ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文