30°

Pascal 调用C语言动态链接库

Pascal 调用 C语言 开发的 动态链接库, 环境: Fedora 31, gcc 9.2.1, fpc 3.0.4

源码

C语言懂点链接库源码

//simplemath.c
int max_int(int x,int y){
    if (x>y){
        return x;
    }else{
        return y;
    }
}

int plus_int(int x,int y){ return x+y; } void swap_int(int* a,int* b){ int c = *a; *a = *b; *b = c; }

pascal的调用代码

//main.pas

Program main;

Uses SysUtils;

Function max_int(a,b:integer): integer; stdcall; external 'libsm'; Function plus_int(x,y:integer): integer; stdcall; external 'libsm'; Procedure swap_int(Var x:integer;Var y:integer); stdcall; external 'libsm';

Var a,b: integer; Begin a := 10; b := 20;

writeln(max_int(a,b)); writeln(plus_int(a,b)); swap_int(a,b); writeln(a,',',b); End.

makefile

mainp.elf:main.pas libsm.so
	ptop main.pas  main.pas
	export LD_LIBRARY_PATH=.
	fpc -Fl. main.pas -omainp.elf
libsm.so:simplemath.c
	gcc --shared -o libsm.so -fPIC simplemath.c
clean:
	rm -f *.elf
	rm -f *.o
	rm -f *.a
	rm -f *.so

本文由【有来有趣】发布于开源中国,原文链接:https://my.oschina.net/happytor/blog/3159545

全部评论: 0

    我有话说: