如何更改从多个源文件编译的g++生成的Linux共享库的入口点

How to change the entry point of a g++ generated Linux shared library compiled from multiple source files?

本文关键字:Linux 共享 入口 g++ 源文件 编译 何更改      更新时间:2023-10-16

今天,我读了一篇web博客文章《如何制作可执行共享库》。在本文中,它指出,如果在Linux命令提示符下键入:

gcc -shared service.c -o libservice.so -Wl,-soname,libservice.so -W1,-e lib_entry

然后是

./libservice.so, then we can directly executer the lib_entry function.

然而,当我运行类似的g++命令时:

g++ -shared one.cpp two.cpp three.cpp -o libservice.so -Wl,-soname,libservice.so -W1,-e lib_entry

其中lib_entrytwo.cpp中定义的C函数,我得到警告消息:

找不到入口点lib_entry点。

如何修复此警告消息,以便可以直接运行入口点lib_entry?我应该附上C函数fooextern "C"链接的实现来解决这个问题吗?

这是我的答案:缺少include";bits/c++配置h";在Ubuntu中在32位上交叉编译64位程序时

sudo apt-get安装gcc-4.9-multilib g++-4.9-multilib

请忽略前面的答案。下面的答案测试成功。谢谢你的耐心。步骤1

#include <stdio.h>
#include <unistd.h>
#ifdef __LP64__
const char service_interp[] __attribute__((section(".interp"))) = "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2";
#else
const char service_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
#endif

void lib_service(void)
{
  printf("This is a service of the shared libraryn");
} // lib_service
void lib_entry(void)
{
  printf("Entry point of the service libraryn");
 _exit(0);
}

步骤2。

vendor@clickit:~/Downloads/DataServerLib$ g++   -shared -fPIC -DLINUX -Wl,-soname,libdataserver.so -efunc  -I /home/vendor/Downloads/waitForMultipleObjects -I /home/vendor/development/Test/Include  DataServer.cpp DataServerLib.cpp DataTransferClient.cpp CWinEventHandle.cpp WinEvent.cpp -o libDataServer.so -lpthread -lrt
maryych@uwash.edu:~/Downloads/DataServerLib$ chmod 777 libDataServer.so
maryych@uwash.edu:~/Downloads/DataServerLib$ ./libDataServer.so

内部入口点测试仪1添加用户