求助!关于c++编写和引用.so的问题,总是出错.
发表于 : 2011-01-06 11:20
我在网上看了一些关于.so 的问题,然后想自己编一编实验一下。
基本上就是按照网上的教程做的,但是还是有问题,请各位帮忙看看好吗?
我是用NetBeans写的,最后的结果总是:
错误!没有链接到libapp_fun.so。
/home/zqm/Workspace/cpp_fun/dist/Debug/GNU-Linux-x86/libapp_fun.so: cannot open shared object file: No such file or directory
/home/zqm/Workspace/cpp_app/dist/Debug/GNU-Linux-x86/cpp_app: undefined symbol: msg
Segmentation fault
按 [Enter] 键关闭终端...
真的不知道该怎么改了,求教。
基本上就是按照网上的教程做的,但是还是有问题,请各位帮忙看看好吗?
代码: 全选
//cpp_fun.h 是libcpp_fun.so的头文件
#ifndef CPP_FUN_H
#define CPP_FUN_H
#endif /* CPP_FUN_H */
#include "stdio.h"
#include "stdlib.h"
#include "stdarg.h"
#include <iostream>
#include <string>
using namespace std;
#ifdef __cplusplus
extern "C"
{
#endif
void msg();
#ifdef __cplusplus
}
#endif
代码: 全选
//cpp_fun.cpp 是libcpp_fun.so的源文件
#include "cpp_fun.h"
void msg(){
string s = "这是C++的动态函数库的程序,吼吼!";
cout<<s<<endl<<s<<endl;
}
int main(){
msg();
return 0;
}
代码: 全选
//这是我想调用libcpp_fun.so 的程序
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <iostream>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
void (*msg)();
string so("/home/zqm/Workspace/cpp_fun/dist/Debug/GNU-Linux-x86/libapp_fun.so");
void *pdlHandle = dlopen(so.c_str(),RTLD_NOW);
char *pzxErr = dlerror();
if(!pdlHandle){
std::cerr<<"错误!没有链接到libapp_fun.so。"<<std::endl<<pzxErr<<std::endl<<std::endl;
}
msg = (void(*)())dlsym(pdlHandle, "msg");
pzxErr = dlerror();
if(pzxErr != NULL){
std::cerr<<pzxErr<<std::endl;
}
msg();
dlclose(pdlHandle);
return 0;
}
错误!没有链接到libapp_fun.so。
/home/zqm/Workspace/cpp_fun/dist/Debug/GNU-Linux-x86/libapp_fun.so: cannot open shared object file: No such file or directory
/home/zqm/Workspace/cpp_app/dist/Debug/GNU-Linux-x86/cpp_app: undefined symbol: msg
Segmentation fault
按 [Enter] 键关闭终端...
真的不知道该怎么改了,求教。