本博客主要总结在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
1.3然后继续在cygwin编译器中,将生成的.o目标文件打包成.a静态库文件,敲入如下代码:
arrcslibhello.ahello.o
1.4调用libhello.a静态库中函数,编译mainHello.c文件,生成hello.exe可执行程序,在cygwin敲入如下代码:
gcc-ohellomainHello.c-L.-lhello
1.5在cygwin下,调用.exe程序,敲入如下代码:
./hello
入上图所示,当输出如下内容时,表示带哦用.a静态库成功。
Hellosuccessfulcall.astaticlibrary!!
参考内容:
https://blog.csdn.net/qq_20480611/article/details/45827451