码迷,mamicode.com
首页 > 其他好文 > 详细

        自己制作静态链接库并使用

时间:2015-11-22 23:37:29      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:include   源代码   return   制作   

                                               目的隐藏源代码只提供给.a 与.h文件

/*
aston.c
*/
#include <stdio.h>
void func1( void )
{
 printf("func1 in aston.c\n");
}
int  func2(int a,int b)
{
 printf("func2 in aston.c\n");
 return a+b;
}
//aston.h
void func1( void );

int  func2(int a,int b);

//Makefile
all:
 gcc  aston.c -o aston.o -c
 ar   -rc  libaston.a  aston.o 

//text.c

#include "aston.h"
#include <stdio.h>

int main(void)
{
 func1();
 int a = func2(4,5);
 printf("a = %d.\n",a );
}

aston@ubuntu:~/CCC/hello/4.6.11.archiveuse$ make
gcc  aston.c -o aston.o -c
ar   -rc  libaston.a  aston.o

aston@ubuntu:~/CCC/hello/4.6.11.archiveuse$ gcc test.c -o test
/tmp/ccsEZ3M1.o: In function `main‘:
test.c:(.text+0xa): undefined reference to `func1‘
test.c:(.text+0x1e): undefined reference to `func2‘
collect2: error: ld returned 1 exit status

aston@ubuntu:~/CCC/hello/4.6.11.archiveuse$ gcc test.c -o test -laston
/usr/bin/ld: cannot find -laston
collect2: error: ld returned 1 exit status

aston@ubuntu:~/CCC/hello/4.6.11.archiveuse$ gcc test.c -o test -laston -L.  //_L.去链接当前目录
aston@ubuntu:~/CCC/hello/4.6.11.archiveuse$ ls
aston.c  aston.h  aston.o  libaston.a  Makefile  test  test.c

aston@ubuntu:~/CCC/hello/4.6.11.archiveuse$ ./test
func1 in aston.c
func2 in aston.c
a = 9.

 

        自己制作静态链接库并使用

标签:include   源代码   return   制作   

原文地址:http://10254316.blog.51cto.com/10244316/1715647

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