linux下c动态库设置可执行入口

代码如下
mytest.c

#include<stdio.h>
#include<stdlib.h>
 
//在elf格式中加入一个节.interp  此节含有 ELF 程序解析器的路径名
//如果动态库不包括此节就不能被执行
//注意你的连接器地址不一定是/lib64/ld-linux-x86-64.so.2这个路径哈 自己查找下你环境中的连接器地址
const char ldpath[] __attribute__ ((section (".interp")))  = "/lib64/ld-linux-x86-64.so.2";
 
//动态库执行的入口函数 由编译器指定(-Wl,-e,myso_entry)
static void myso_entry()
{
     printf("this is a output infomation for test\n");
     exit(0);
}
 
/*
下面是动态库提供的两个对外函数
*/
int myadd(int a,int b)
{
    return a+b;
}
 
int mysub(int a,int b)
{
    return a-b;
}
编译方式
gcc -fPIC -shared -Wl,-e,myso_entry -o mytest.so mytest.c
Wl:告诉编译器将后面的参数(-e myso_entry)交给连接器处理
ld连接器使用-e 来指定程序的入口地址(显然此处指定入口地址为myso_entry)

运行结果

错误提示

原因排查:编译过程中gcc得-Wl,-e指定程序入口标识未找到 

 但是查看库有这个方法得标识

 原因调查:

编译指定得c标准规范c11,static修饰方法为内联方法,对外不可见,ld无法检索到内敛方法,解决去掉static修饰

猜你喜欢

转载自blog.csdn.net/HideInTime/article/details/129795087