`
oraclestudy
  • 浏览: 482096 次
文章分类
社区版块
存档分类

Linux操作系统下动态库的编写与调用

 
阅读更多

1. c 语言写动态库:

/*

* libsthc.h

* Declarations for function add

*/

#include "stdio.h"

#include "stdlib.h"

#include "stdarg.h"

#ifdef __cplusplus

extern "C"

{

#endif

int add(int x, int y);

#ifdef __cplusplus

}

#endif

/*

* libsthc.c

* Implementation of function add declared in libsthc.h

* in c language

*/

#include "libsthc.h"

int add(int x, int y)

{

return x + y;

}

#makefile

libsthc.so:libsthc.o

gcc -shared libsthc.o -lc -o libsthc.so

libsthc.o:libsthc.c libsthc.h

gcc -fPIC -c libsthc.c -o libsthc.o

all:libsthc.so

clean:

rm -f *.o *.so

make 完成后,会生成一个动态库,即 libsthc.so 。为了使其他程序也可以使用该动态库,需要将库文件 libsthc.so 拷贝到 /usr/lib 目录下 ( 由于权限的问题,一般要以 root 的身分进行拷贝 ) ,为了使其他程序也可以使用该动态库,需要将头文件 libsthc.h 拷贝到 /usr/include 目录下 ( 由于权限的问题 , 一般要以 root 的身分进行拷贝 )

1.1 c 语言静态方式调用动态库 libsthc.so

/*

* ctest.c

* Testing program for libsthc.so library

* in c languange

* by 玄机逸士

*/

#include "libsthc.h"

int main(void)

{

printf("%d/n", add(1, 2));

return 0;

}

#makefile:

ctest:ctest.o

gcc ctest.o -lsthc -o ctest

ctest.o:ctest.c

gcc -c ctest.c -o ctest.o

all:ctest

clean:

rm -f *.o ctest

1.2 c 语言动态方式调用动态库 libsthc.so

/*cdltest.c*/

#include "stdio.h"

#include "stdlib.h"

#include "dlfcn.h"

int main(void)

{

void *handle;

int (*fcn)(int x, int y);

const char *errmsg;

/* open the library */

handle = dlopen("libsthc.so", RTLD_NOW);

if(handle == NULL)

{

fprintf(stderr, "Failed to load libsthc.so: %s/n", dlerror());

return 1;

}

dlerror();

//*(void **)(&fcn) = dlsym(handle, "add"); //ok

fcn = dlsym(handle, "add"); //ok

if((errmsg = dlerror()) != NULL)

{

printf("%s/n", errmsg);

return 1;

}

printf("%d/n", fcn(1, 5));

dlclose(handle);

return 0;

}

#makefile

cdltest:cdltest.o

gcc cdltest.o -ldl -lsthc -o cdltest

cdltest.o:cdltest.c

gcc -c cdltest.c -o cdltest.o

all:cdltest

clean:

rm -f *.o cdltest

1.3 c++ 静态方式调用动态库 libsthc.so

/*cpptest.cc*/

#include "libsthc.h"

using namespace std;

int main(void)

{

printf("%d/n", add(1, 2));

return 0;

}

#makefile:

cpptest:cpptest.o

g++ cpptest.o –o cpptest -lsthc

cpptest.o:cpptest.cc

g++ -c cpptest.cc -Wno-deprecated -o cpptest.o

all:cpptest

clean:

rm -f *.o cpptest

1.4 c++ 动态方式调用动态库 libsthc.so

/*cppdltest.cpp*/

#include "stdio.h"

#include "stdlib.h"

#include "dlfcn.h"

int main(void)

{

void *handle;

int (*fcn)(int x, int y);

const char *errmsg;

/* open the library */

handle = dlopen("libsthc.so", RTLD_NOW);

if(handle == NULL)

{

fprintf(stderr, "Failed to load libsthc.so: %s/n", dlerror());

return 1;

}

dlerror();

*(void **)(&fcn) = dlsym(handle, "add"); //ok

//fcn = dlsym(handle, "add"); //not ok in c++

if((errmsg = dlerror()) != NULL)

{

printf("%s/n", errmsg);

return 1;

}

printf("%d/n", fcn(1, 5));

dlclose(handle);

return 0;

}

#makefile

cppdltest:cppdltest.o

g++ cppdltest.o -ldl -lsthc -o cppdltest

cppdltest.o:cppdltest.cpp

g++ -c cppdltest.cpp -o cppdltest.o

all:cppdltest

clean:

rm -f *.o cppdltest

2. c++ 语言写动态库:

/*

* libsthcpp.h

* Declarations for function cppadd

*/

#include "stdio.h"

#include "stdlib.h"

#include "stdarg.h"

#ifdef __cplusplus

extern "C"

{

#endif

int cppadd(int x, int y);

#ifdef __cplusplus

}

#endif

/*

* libsthcpp.cpp

* Implementation of function cppadd declared in libsthcpp.h

* in c++ language

*/

#include "libsthcpp.h"

int cppadd(int x, int y)

{

return x + y;

}

#makefile

libsthcpp.so:libsthcpp.o

g++ -g -shared -Wl libsthcpp.o -lc -o libsthcpp.so

libsthcpp.o:libsthcpp.cc libsthcpp.h

g++ -g -fPIC -c libsthcpp.cc -o libsthcpp.o

all:libsthcpp.so

clean:

rm -f *.o *.so

make 完成后,会生成一个动态库,即 libsthcpp.so 。为了使其他程序也可以使用该动态库,需要将库文件 libsthcpp.so 拷贝到 /usr/lib 目录下 ( 由于权限的问题,一般要以 root 的身分进行拷贝 ) ,为了使其他程序也可以使用该动态库,需要将头文件 libsthcpp.h 拷贝到 /usr/include 目录下 ( 由于权限的问题 , 一般要以 root 的身分进行拷贝 )

2.1 c 语言静态方式调用动态库 libsthcpp.so

/*

* ctest.c

* Testing program for libsthcpp.so library

* in c languange

* by 玄机逸士

*/

#include "libsthcpp.h"

int main(void)

{

printf("%d/n", cppadd(1, 2));

return 0;

}

#makefile

ctest:ctest.o

gcc ctest.o -lsthcpp -o ctest

ctest.o:ctest.c

gcc -c ctest.c -o ctest.o

all:ctest

clean:

rm -f *.o ctest

2.2 c 语言动态方式调用动态库 libsthcpp.so

/*cdltest.c*/

#include "stdio.h"

#include "stdlib.h"

#include "dlfcn.h"

int main(void)

{

void *handle;

int (*fcn)(int x, int y);

const char *errmsg;

/* open the library */

handle = dlopen("libsthcpp.so", RTLD_NOW);

if(handle == NULL)

{

fprintf(stderr, "Failed to load libsthc.so: %s/n", dlerror());

return 1;

}

dlerror();

//*(void **)(&fcn) = dlsym(handle, "cppadd"); //ok in c and c++

fcn = dlsym(handle, "cppadd"); //ok in c, but not in c++

if((errmsg = dlerror()) != NULL)

{

printf("%s/n", errmsg);

return 1;

}

printf("%d/n", fcn(1, 5));

dlclose(handle);

return 0;

}

#makefile

cdltest:cdltest.o

gcc cdltest.o -ldl -lsthcpp -o cdltest

cdltest.o:cdltest.c

gcc -c cdltest.c -o cdltest.o

all:cdltest

clean:

rm -f *.o cdltest

2.3 c++ 语言静态方式调用动态库 libsthcpp.so

/*

* cpptest.cpp

* Testing program for libsthc.so library written in c language

* in c++ languange

* by 玄机逸士

*/

#include "libsthcpp.h"

#include "iostream.h"

int main(void)

{

cout << cppadd(1, 2) << endl;

return 0;

}

#makefile

cpptest:cpptest.o

g++ cpptest.o -lsthcpp -o cpptest

cpptest.o:cpptest.cpp

g++ -c cpptest.cpp -Wno-deprecated -o cpptest.o

all:cpptest

clean:

rm -f *.o cpptest

2.4 c++ 语言动态方式调用动态库 libsthcpp.so

/*cppdltest.cpp*/

#include "stdio.h"

#include "stdlib.h"

#include "dlfcn.h"

int main(void)

{

void *handle;

int (*fcn)(int x, int y);

const char *errmsg;

/* open the library */

handle = dlopen("libsthcpp.so", RTLD_NOW);

if(handle == NULL)

{

fprintf(stderr, "Failed to load libsthc.so: %s/n", dlerror());

return 1;

}

dlerror();

*(void **)(&fcn) = dlsym(handle, "cppadd"); //ok in c and c++

//fcn = dlsym(handle, "cppadd"); //ok in c, but not in c++

if((errmsg = dlerror()) != NULL)

{

printf("%s/n", errmsg);

return 1;

}

printf("%d/n", fcn(1, 5));

dlclose(handle);

return 0;

}

#makefile

cppdltest:cppdltest.o

g++ cppdltest.o -ldl -lsthcpp -o cppdltest

cppdltest.o:cppdltest.cpp

g++ -c cppdltest.cpp -o cppdltest.o

all:cppdltest

clean:

rm -f *.o cppdltest

分享到:
评论

相关推荐

    对比Windows和Linux两系统的动态库

    从程序编写、编 译、调用以及对操作系统依赖等方面综合分析比较了这两种调用方式的不同之处,根据实际程序移植经验,给出了将VC++编制的Windows动态库移植到 Linux下的方法以及需要注意的问题,同时并给出了程序示例...

    LINUX系统中动态链接库的创建与使用

    译、调用以及对操作系统依赖等方面综合分析比较了这两种调用方式的不同之处,根据实际程序移植经验,给出了将VC++编制的Windows动态库移植到Linux下的方法以及需要注意的问题,同时并给出了程序示例片断,实际在程序...

    c# - winform调用fortran/c++动态链接库的实现

    在这种情况下,可以使用C++或Fortran编写的库,然后通过C#调用该库来实现需要的功能。 具有优化的计算需求:C++和Fortran语言在计算密集型任务方面通常具有更好的性能。因此,如果需要进行大规模的数值计算或者需要...

    UNIX操作系统教程 张红光

    第1章绪论.1 1.1操作系统概述1 1.1.1建立操作系统的目标1 1.1.2操作系统是用户与计算机的接口1 1.1.3操作系统是资源管理器2 1.2UNIX系统的主要特性3 1.3UNIX系统的发展史4 1.4开源软件与UNIX的推广发展6 1.4.1开源...

    《计算机操作系统》期末复习指导

    对考试很有帮助的.......... ...UNIX或Linux操作系统中文件系统的主要特点 (1)操作系统文件的目录组织是一个树形结构,从根结点到叶子称为文件的全路径名,文件可以由其全路径名唯一确定...

    操作系统(内存管理)

    free:该函数获得指向由 malloc 分配的内存片段的指针,并将其释放,以便以后的程序或操作系统使用(实际上,一些 malloc 实现只能将内存归还给程序,而无法将内存归还给操作系统)。 物理内存和虚拟内存 要理解...

    Linux多线程服务端编程:使用muduo C++网络库

    《Linux多线程服务端编程:使用muduo C++网络库》主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread。...

    Linux1.0核心游记

    Linux1.0核心游记 &lt;br&gt;第一部分 基础知识(Basic knowledge ).....................................................................11 软件部分(Software part)........................................

    毕设作品 采用Qt框架编写(基于WIFI车间设备监测与控制系统的研究)的主程序.zip

    它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...

    网络编程教程,很好的一本写linux网络编程书,这是我上传的源码

     1.3.3 和进程相关的系统调用  本章小结 第二章 进程间通信和同步  2.1 信号的处理  2.1.1 Linux中支持的信号  2.1.2 信号的捕获和处理  2.1.3 系统调用和信号的相互作用  2.1.4 pause和...

    sandbox:简单Linux seccomp规则,无需编写任何代码

    它与SystemCallFilter= 非常相似,但具有一些优点: 它没有一些systemd限制: execve,exit,exit_group,getrlimit,rt_sigreturn,sigreturn系统调用以及用于查询时间和睡眠的系统调用被隐式列入白名单......

    嵌入式课件

    第11章Linux操作系统基础 11.1嵌入式Linux的开发环境 11.1.1交叉开发概述 11.1.2桌面Linux的开发工具链 11.1.3嵌入式Linux的交叉开发工具链 11.2 桌面Linux的安装 11.2.1双操作系统环境 11.2.2 Cygwin模拟环境 ...

    基于Qt编写的智能管家系统客户端,实现语音识别,按钮音效,摄像头采集。.zip

    它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...

    基于Qt编写的音乐播放器,界面由QML编写,网络和文件由C++实现,能够搜索和播放在线歌曲。.zip

    它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...

    基于qt编写的五子棋和围棋游戏.zip

    它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...

    Linux 平台基于 Qt + MYSQL 的聊天程序.zip

    它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...

    这是一个基于QT,使用c++编写的2D超级玛丽游戏.zip

    它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...

    ChineseChess 中国象棋,使用QT基于C++编写,实现了完整的人机对战.zip

    它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...

    传智播客扫地僧视频讲义源码

    09_C动态库升级成框架案例_方法2把回调函数缓存到动态库_编写 10_C动态库升级成框架案例_方法2把回调函数混存到动态库_测试 11_C++基础课程day06-day08_知识体系梳理 文档和源码 第三部分 C++进阶部分目录 01_上一...

Global site tag (gtag.js) - Google Analytics