linux下将c++编译成so,调用该so文件

在其他资料上增加了过程中遇到的问题。

so文件为动态链接库文件,与windows下的dll文件相当,Linux下系统so文件一般保存在/usr/lib中。

下面就说明一下如何生成c++程序的so文件,以及如何在c++程序中调用该so文件

==========test.h===========

#ifdef __cplusplus //

extern "C"

{

#endif

  
class Test{
public:
int hello(int i);
};

int helloT(int j);

#ifdef __cplusplus

}

#endif 

 

==========test.cpp===========

#include"test.h"
#include<iostream>
using namespace std;
int Test::hello(int i){
       if(i>3)

                 cout<<"hello Class Test>3"<<endl;
       else

                  cout<<"hello Class Test<3"<<endl;
        return 0;
}
int helloT(int j){
      Test *t=new Test();
       t->hello(j);
       return 0;
}

 

编译test.cpp文件

g++ -shared -fpic -lm -ldl -o libtest.so test.cpp

其中,so文件名必须以lib开头。编译具体指令请参考帮助文档

编译调试test.cpp文件

gcc -ggdb3 -Wall -shared -fpic -o libtest.so test.cpp

 

==========main.cpp===========

动态链接:

#include <stdio.h>   
#include <dlfcn.h>
#include <stdlib.h>   
#include <iostream>   
using namespace std;  

/*

需要用到的函数

dlopen()

dlerror()

dlsym()

dlclose()

都存储在头文件dlfcn.h中

*/

int main()  {  
      
    void *handle = dlopen("./libtest.so", RTLD_LAZY);  //该处的./libtest.so表示so文件的存放位置,RTLD_LAZY是指示位
    if(!handle)  {  
        printf("open lib error\n");  
        cout<<dlerror()<<endl;  
        return -1;  
    }  
      
     typedef int (*hello)(int);//该处的函数与文件test.h中需调用的函数保持一致
     hello h= (hello)dlsym(handle, "helloT");// helloT为test.h中调用函数的名字,dlsym返回一个函数指针
     if(!h)  {  
        cout<<dlerror()<<endl; 
        dlclose(handle);  
        return -1;  
    }  
      
     int i;
     cin>>i;
    (*h)(i);//用函数指针形式调用函数
    dlclose(handle);  
    return 0;  
}  



in#include <stdio.h>
#include <stdlib.h>
#include "filter.h"

int main()  {


        printf("example\n");
        int lengthData = 10;
        //int Data[2][10] = { 1 };
        int *buf =(int*) malloc(20*4);
        int **Data = &buf;
        int Ecg_filter_V5[10]={0},Ecg_filter_V1[10]={0};

        int Fs = 128;
        printf("11111\n");
//        int **datatemp = (int**)Data;
        int result = 0;
        printf("start filter\n");
        result = filter(Data, lengthData, Ecg_filter_V5, Ecg_filter_V1, Fs);
        if (result != 1)
        {
                printf("filter error\n");
        }
        else
        {
                printf("filter OK\n");
        }


        return 0;
}

编译main.cpp文件

g++ main.cpp -ldl -o main

编译调试main.cpp文件

gcc -ggdb3 -Wall  -o test main.c -Llib  -lfilter

执行./main


调试 main

gdb -q test 

单步执行 s

run

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux调用动态库主要是通过dlopen、dlsym、dlclose等函数来实现。下面是一个简单的示例: 1. 编写动态库代码 首先,我们需要编写一个动态库的代码。这里我们以一个简单的例子来说明,假设我们需要编写一个名为libmylib.so的动态库,其中包含一个用于加法运算的函数add。 // mylib.h #ifndef MYLIB_H #define MYLIB_H #ifdef __cplusplus extern "C" { #endif int add(int a, int b); #ifdef __cplusplus } #endif #endif //MYLIB_H // mylib.cpp #include "mylib.h" int add(int a, int b) { return a + b; } 注意,在动态库中需要将函数声明为extern "C",这是因为在C++中函数名称会被编译器进行名称重整,而在动态库中需要使用原始的函数名称,所以需要使用extern "C"来告诉编译器不要进行名称重整。 2. 编译动态库 编译动态库的命令如下: g++ -shared -fPIC -o libmylib.so mylib.cpp 其中,-shared选项表示生动态库,-fPIC选项表示编译时需要生位置无关代码,-o选项表示指定输出文件名。 3. 编写调用动态库的代码 我们可以在另一个C++程序中调用上述动态库。下面是一个简单的示例: // main.cpp #include <iostream> #include <dlfcn.h> #include "mylib.h" int main() { void *handle = dlopen("./libmylib.so", RTLD_LAZY); if (!handle) { std::cerr << "Cannot open library: " << dlerror() << '\n'; return 1; } typedef int (*add_t)(int, int); add_t add_func = reinterpret_cast<add_t>(dlsym(handle, "add")); if (!add_func) { std::cerr << "Cannot load symbol add: " << dlerror() << '\n'; dlclose(handle); return 1; } int result = add_func(2, 3); std::cout << "The result is " << result << '\n'; dlclose(handle); return 0; } 该程序首先通过dlopen函数打开动态库,并将返回的句柄保存在变量handle中。然后通过dlsym函数获取动态库中的add函数地址,并将其转换为函数指针类型add_t。最后,我们可以通过add_func指针调用add函数。 注意,在调用dlsym函数时需要指定原始的函数名称,即在编写动态库代码时使用的名称。 4. 编译可执行程序 编译可执行程序的命令如下: g++ -o main main.cpp -ldl 其中,-ldl选项表示链接动态库加载器库。 5. 运行程序 运行可执行程序的命令如下: ./main 程序的输出应该是: The result is 5 至此,我们功地调用了动态库中的函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值