zoukankan      html  css  js  c++  java
  • 将C语言文件生成静态库lib

    1,创建三个文件Test.c , Test.h,main.c

    内容分别如下:

    Test.h

    1 #ifndef _TEST_H_
    2 #define _TEST_H_
    3 
    4 int get_result(int firstNum,int secondNum);
    5 
    6 #endif //test.h

    Test.c

    1 #include "Test.h"
    2 
    3 int get_result(int firstNum,int secondNum)
    4 {
    5     return firstNum+secondNum;
    6 }

    main.c

     1 #include <stdio.h>
     2 #include "Test.h"
     3 
     4 int main()
     5 {
     6     int rlt; 
     7     rlt = get_result(23,7);
     8     printf("The result is: rlt = %d
    ",rlt);
     9     
    10     return 0;
    11 }

    其中Test.h,Test.c用于生成静态库,main.c用于测试

    2,编译.o文件

    无论静态库,还是动态库,都是由.o文件创建的。因此,我们必须将源程序Test.c通过gcc先编译成.o文件。

    gcc -c Test.c

    确认是否生成了Test.o文件

    3,生成静态库

    在linux环境下输入ar cr libmyTest.a Test.o

    通过ls命令可以看到在当前目录中生成了静态库文件libmyTest.a文件

    4,测试

    输入gcc -o main main.c -L. -lmyTest

    输出结果如下图:

  • 相关阅读:
    nyoj163 Phone List
    hdu1251统计难题
    hdu1754 I Hate It
    nyoj123 士兵杀敌(四)
    poj3468 A Simple Problem with Integers
    zoj1610 Count the Colors
    nyoj144 小珂的苦恼
    nyoj93 汉诺塔(三)
    poj2182 Lost Cows
    ASP.NET2.0中的Callback机制
  • 原文地址:https://www.cnblogs.com/Pan-Z/p/6405748.html
Copyright © 2011-2022 走看看