码迷,mamicode.com
首页 > 系统相关 > 详细

编译Linux使用的.a库文件

时间:2017-05-23 19:44:36      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:class   gravity   nbsp   std   nts   生成   stdio.h   tracking   enter   

编译Linux使用的.a库文件

 

首先是须要编译成.a的源文件

 

hello.h

#ifndef __INCLUDE_HELLO_H__
#define __INCLUDE_HELLO_H__

void hello(const char *name);


#endif // end __INCLUDE_HELLO_H__

hello.c

#include "stdio.h"


void hello(const char *name)
{
    printf("Hello world %s\n", name);
}


和一个在linux平台上做測试的main.c

#include "hello.h"


int main()
{
    hello("everyone");
    
    return 0;
}


在Linux上面做測试。编译成.a文件,主要利用.o文件和ar命令


1、首先生成.o文件:

gcc -c hello.c

这样源码的文件夹下就会产生一个hello.o

技术分享


2、利用ar命令,从.o文件里创建.a文件

ar cr libhello.a hello.o

这样就能够生成.a文件了。注意,所要生成的.a文件的名字前三位最好是lib,否则在链接的时候。就可能导致找不到这个库

技术分享


3、在linux下測试使用

编译main.c。并让hello.a链接到main中

gcc main.c -L. -lhello -o main(注意这里-L后面有个.)

这样在当面文件夹以下就出现了可执行程序main。直接执行就是我们索要的结果了

技术分享

技术分享

编译Linux使用的.a库文件

标签:class   gravity   nbsp   std   nts   生成   stdio.h   tracking   enter   

原文地址:http://www.cnblogs.com/cynchanpin/p/6895545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!