cygwin怎么编译c语言,cygwin下编译.c程序和调用.a静态库用法

本博客主要总结在cywgin下编译一个写好的.c程序(注意,是c程序,不是c++程序)。

1.1用notepad软件写一个hello.h和hello.c程序,如代码如下所示:

hello.h头文件代码

#ifndefHELLO_H

#defineHELLO_H

#ifdef__cplusplus

extern"C"{

#endif

externvoidhello(constchar*name);

#ifdef__cplusplus

}

#endif

#endif

hello.c源文件代码

#include

voidhello(constchar*name)

{

printf("Hello%s!\n",name);

}

mainHello.c源文件代码

#include"hello.h"

intmain()

{

hello("successfulcall.astaticlibrary!");

return0;

}

1.2在cygwin下,用gcc编译,敲入如下代码,生成hello.o文件

gcc-chello.c

6658823919807a968e688906a303822a.png

65453b23af54a9f309483a009e75cf28.png

1.3然后继续在cygwin编译器中,将生成的.o目标文件打包成.a静态库文件,敲入如下代码:

arrcslibhello.ahello.o

b48c22f14650d4afaa3e40c12640ff56.png

f24b9bd87697cfa6adfd51b8e7dfe7d3.png

1.4调用libhello.a静态库中函数,编译mainHello.c文件,生成hello.exe可执行程序,在cygwin敲入如下代码:

gcc-ohellomainHello.c-L.-lhello

cd754e69aa68b3ed2ca903161a5dc896.png

36fa007260392dc1e9beef2b39327f35.png

1.5在cygwin下,调用.exe程序,敲入如下代码:

./hello

bbed02eddb9a278a74242f92836d8352.png

入上图所示,当输出如下内容时,表示带哦用.a静态库成功。

Hellosuccessfulcall.astaticlibrary!!

参考内容:

https://blog.csdn.net/qq_20480611/article/details/45827451

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Windows 系统下,使用 Golang 调用 C 语言开源库 libxslt 可以通过以下步骤完成: 1. 安装 libxslt 在 Windows 上,可以通过 Cygwin 或 MinGW 等工具链来安装 libxslt 库。 2. 编写 C 语言代码 编写 C 语言代码,实现 libxslt 库的功能,并将其编译为动态链接库(.dll)文件。 例如,以下是一个使用 libxslt 库将 XML 文件转换为 HTML 文件的 C 语言代码: ```c #include <stdio.h> #include <string.h> #include <libxslt/xslt.h> #include <libxslt/transform.h> int transform(char *xml, char *xsl, char *output) { xmlDocPtr doc, res; xsltStylesheetPtr sty; const char *params[1] = { NULL }; int ret; xmlInitParser(); LIBXML_TEST_VERSION doc = xmlReadMemory(xml, strlen(xml), "noname.xml", NULL, 0); sty = xsltParseStylesheetFile((const xmlChar *)xsl); res = xsltApplyStylesheet(sty, doc, params); xmlSaveFormatFile(output, res, 1); xsltFreeStylesheet(sty); xmlFreeDoc(doc); xmlFreeDoc(res); xmlCleanupParser(); return ret; } ``` 将上述代码编译为动态链接库,可使用以下命令: ``` gcc -shared -o libxslt.dll -I/path/to/libxslt/include -L/path/to/libxslt/lib -lxslt -lz -lm xslt.c ``` 其中,`/path/to/libxslt` 为 libxslt 库的安装目录。 3. 使用 CGO 调用 C 函数 在 Golang 中,使用 CGO 可以调用 C 函数。在调用 C 函数前,需要将 C 函数声明为外部函数。 例如,以下是一个使用 CGO 调用上述 C 函数的 Golang 代码: ```go package main // #cgo LDFLAGS: -L./ -lxslt // int transform(char *xml, char *xsl, char *output); import "C" import "fmt" func main() { xml := "<xml>...</xml>" xsl := "<xsl>...</xsl>" output := "output.html" ret := C.transform(C.CString(xml), C.CString(xsl), C.CString(output)) fmt.Println(ret) } ``` 在编译 Golang 代码时,需要指定 libxslt 库的链接标志 `-L` 和 `lxslt`。 以上就是在 Windows 系统下,使用 Golang 调用 C 语言开源库 libxslt 的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值