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
分享到:
相关推荐
从程序编写、编 译、调用以及对操作系统依赖等方面综合分析比较了这两种调用方式的不同之处,根据实际程序移植经验,给出了将VC++编制的Windows动态库移植到 Linux下的方法以及需要注意的问题,同时并给出了程序示例...
译、调用以及对操作系统依赖等方面综合分析比较了这两种调用方式的不同之处,根据实际程序移植经验,给出了将VC++编制的Windows动态库移植到Linux下的方法以及需要注意的问题,同时并给出了程序示例片断,实际在程序...
在这种情况下,可以使用C++或Fortran编写的库,然后通过C#调用该库来实现需要的功能。 具有优化的计算需求:C++和Fortran语言在计算密集型任务方面通常具有更好的性能。因此,如果需要进行大规模的数值计算或者需要...
第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++网络库》主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread。...
Linux1.0核心游记 <br>第一部分 基础知识(Basic knowledge ).....................................................................11 软件部分(Software part)........................................
它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...
1.3.3 和进程相关的系统调用 本章小结 第二章 进程间通信和同步 2.1 信号的处理 2.1.1 Linux中支持的信号 2.1.2 信号的捕获和处理 2.1.3 系统调用和信号的相互作用 2.1.4 pause和...
它与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模拟环境 ...
它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...
它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...
它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...
它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...
它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...
它支持多种操作系统,包括但不限于Windows、macOS、Linux、Android和iOS。通过Qt,开发者可以使用同一套源代码,在不同平台上编译并生成原生外观与体验的应用程序,极大地提高了开发效率和产品一致性。 图形用户...
09_C动态库升级成框架案例_方法2把回调函数缓存到动态库_编写 10_C动态库升级成框架案例_方法2把回调函数混存到动态库_测试 11_C++基础课程day06-day08_知识体系梳理 文档和源码 第三部分 C++进阶部分目录 01_上一...