![](/img/trans.png)
[英]Why am I getting “undefined symbol” when I link my static library in my C program?
[英]i cant get my program to work i keep getting undefined symbol: c
我正在尝试加载所有库,并从每个库中调用一个函数,以创建一个文件并通过指针调整小提琴,并在主程序中与它们一起编写内容,关闭它们。
这是我的主要功能:
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
char LIBA[]="./LIBA.SO";
char LIBB[]="./LIBB.SO";
char LIBC[]="./LIBC.SO";
typedef void (*FUNC_T)(int*);
FUNC_T FUNC[2];
void ifnull(void *detect)
{
if (detect == NULL)
{
cout << "ERROR:" << dlerror() << endl;
exit(-1);
}
}
void ifnull2(FUNC_T detect)
{
if (detect == NULL)
{
cout << "ERROR:" << dlerror() << endl;
exit(-1);
}
}
int main()
{
void *handle[2];
int FD[2];
handle[0]=dlopen(LIBA, RTLD_LAZY); ifnull(handle[0]);
handle[1]=dlopen(LIBB, RTLD_NOW); ifnull(handle[1]);
handle[2]=dlopen(LIBC, RTLD_NOW); ifnull(handle[2]);
FUNC[0]=(void(*)(int*))dlsym(handle[0], "c"); ifnull2(FUNC[0]);
FUNC[1]=(void(*)(int*))dlsym(handle[1], "c"); ifnull2(FUNC[1]);
FUNC[2]=(void(*)(int*))dlsym(handle[2], "c"); ifnull2(FUNC[2]);
FUNC[0](&FD[0]); FUNC[1](&FD[1]); FUNC[2](&FD[2]);
return 0;
}
这是在库里面:
#include <stdio.h>
void c(int *fd)
{
printf("ok A\n");
}
我不断得到
ERROR:./LIBA.SO: undefined symbol: c
请帮忙
C++ mungs 函数名是因为它可以有多个同名的函数。
因此对于
#include <stdio.h>
void c(int *fd)
{
printf("ok A\n");
}
我们得到
$ g++ -Wall -Wextra -pedantic -c -fPIC liba.cpp -o liba.so
liba.cpp: In function ‘void c(int*)’:
liba.cpp:3:13: warning: unused parameter ‘fd’ [-Wunused-parameter]
void c(int *fd)
^~
$ nm liba.so
U _GLOBAL_OFFSET_TABLE_
U puts
0000000000000000 T _Z1cPi
如您所见,该函数以_Z1cPi
存在。 我不确定这是否可以安全使用。 但是,您可以做的是使用extern "C"
。
为了
#include <stdio.h>
extern "C" void c(int *fd)
{
printf("ok A\n");
}
我们得到
$ g++ -Wall -Wextra -pedantic -c -fPIC liba.cpp -o liba.so
liba.cpp: In function ‘void c(int*)’:
liba.cpp:3:24: warning: unused parameter ‘fd’ [-Wunused-parameter]
extern "C" void c(int *fd)
^~
$ nm liba.so
0000000000000000 T c
U _GLOBAL_OFFSET_TABLE_
U puts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.