- 论坛徽章:
- 0
|
20可用积分
我的程序大体框架是这样的:
我把dll.cpp用-shared选项编译成dll.so,在dll.cpp当中引用了静态库libA.a当中的符号。然后dll.so是在main当中用dlopen打开的。
编译和链接都没有问题,但是在运行的时候会报如下的错误:
./dll.so: undefined symbol: _ZN1AC1EiSegmentation fault |
所以我猜想可能是我在动态库中引用了静态库中的符号的缘故(参见Makefile中g++ -shared -L. -lA -o dll.so dll.cpp)。
请大家帮我看看怎么办呢? 应该加上什么参数呢?
各个文件文件的代码如下:
makefile
All: main dll
main: static dll main.cpp
g++ -ldl -o main main.cpp
dll: dll.cpp static
g++ -shared -L. -lA -o dll.so dll.cpp
static: A.cpp
g++ -c A.cpp
ar rc libA.a A.o
|
A.h
class A
{
public:
A(int a);
}; |
A.cpp
#include "A.h"
#include <iostream>
using namespace std;
A::A(int a)
{
cout << "in A.A() " << a << endl;
}
|
dll.cpp
#include <string>
#include <iostream>
#include "A.h"
using namespace std;
template<int F>
class dll
{
public:
A *a;
public:
dll(int id)
{
a = new A(id);
cout << id << " " << F << endl;
}
};
template class dll<4>;
extern "C"
{
void maker(int id)
{
new dll<4>(id);
}
}
|
main.cpp
#include <dlfcn.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
char *p;
void *lib;
lib = dlopen("./dll.so", RTLD_LAZY);
if (lib == NULL)
{
cout << "NULL" << endl;
p = dlerror();
cout << p << endl;
}
else
cout << std::hex << lib << endl;
typedef void (*FUNC) (int);
FUNC sym = (FUNC) dlsym(lib, "maker");
sym(3);
return 0;
}
|
|
最佳答案
查看完整内容
GCC官方文档里讲是不是顺序问题g++ -shared -L. -lA -o dll.so dll.cpp在win下挂驴呢,LZ自己试试调整顺序吧
|