# lazarus **Repository Path**: wydev/lazarus ## Basic Information - **Project Name**: lazarus - **Description**: No description available - **Primary Language**: Pascal - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-08-26 - **Last Updated**: 2024-08-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # lazarus ## Lazarus使用C语言编写的函数库 ### 静态链接方式 1. 使用C语言编写函数库(Windows平台下的例子) ~~~ // mylib.c #include int add(int a, int b) { return a + b; } ~~~ 2. 使用GCC进行编译 ~~~ gcc -c mylib.c -o mylib.o ~~~ 3. 使用ar转换为静态库 ~~~ ar rcs libmylib.a mylib.o ~~~ 4. 使用Lazarus创建一个例程 ~~~ unit fmMain; {$mode objfpc}{$H+} {$linklib 'libmylib.a'} // 链接静态库 interface uses CTypes, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private public end; function add(a, b: integer):integer; cdecl; external;// 声明外部函数 var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); var result: Integer; begin result := add(5, 3); // 调用 C 函数 Caption := IntToStr(result); end; end. ~~~