cygwin下编译.c程序和调用.a静态库用法

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

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

hello.h头文件代码

#ifndef HELLO_H
#define HELLO_H
 
 
#ifdef __cplusplus
extern "C" {
#endif
extern void hello(const char* name);
#ifdef __cplusplus
}
#endif
 
 
#endif


hello.c源文件代码

#include <stdio.h>
 
 
void hello(const char* name)
{
    printf("Hello%s!\n", name);
}

mainHello.c源文件代码

#include "hello.h"
 
 
int main()
{
    hello("successful call .a static library!");
    return 0;
}

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

gcc -c hello.c




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

ar rcs libhello.a hello.o





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

gcc -o hello mainHello.c -L. -lhello





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

./hello




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

Hellosuccessful call .a static library!!



参考内容:

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

猜你喜欢

转载自blog.csdn.net/naibozhuan3744/article/details/80447798
今日推荐