mac下gcc学习(一)

来源:互联网 发布:js输入框输入触发事件 编辑:程序博客网 时间:2024/05/16 18:42

一、生成静态库

新建一个hello.c,内容如下:

#include <stdio.h>int hello(){        printf("hello world\n") ;        return 0 ;}

以及其头文件hello.h

#ifndef _HELLO_H#define _HELLO_Hint hello() ;#endif
用于测试的主函数文件test.c

#include "hello.h"int main(){        hello();        return 0 ;}
编译源文件hello.c到目标文件hello.o

gcc -c hello.c -o hello.o


生成包含hello模块的静态库libhello.a

libtool -static -o libhello.a hello.o    --MAC OS

ar crsv libhello.a testhello             --linux

将库与源文件链接成可执行文件 testhello

gcc test.c libhello.a -o testhello


最后测试一下








0 0
原创粉丝点击