首页 > 系统服务 > 详细

linux环境下库文件的编译

时间:2020-03-21 13:35:58      阅读:106      评论:0      收藏:0      [点我收藏+]

一:无库情况下

#include 
void hello()

        printf("This is st .\n");
}

二:生成静态库

 shutao  ~  mkdir libtest
mkdir: cannot create directory ‘libtest’: File exists
 shutao  ~  rm -r libtest
 shutao  ~  mkdir libtest
 shutao  ~  cd libtest/
 shutao  ~  libtest  vim stlib.c
 shutao  ~  libtest  vim stlib.h
 shutao  ~  libtest  vim test.c
 shutao  ~  libtest  gcc -Wall -g -c -o stlib.o stlib.c
stlib.c:1:10: error: #include expects "FILENAME" or <FILENAME>
 #include
          ^
stlib.c: In function ‘hello’:
stlib.c:4:10: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
          printf("This is my st.\n");
          ^~~~~~
stlib.c:4:10: warning: incompatible implicit declaration of built-in function ‘printf’
stlib.c:4:10: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
 shutao  ~  libtest vim stlib.c
 shutao  ~  libtest  gcc -Wall -g -c -o stlib.o stlib.c
 shutao  ~  libtest  ar rcs stlib.a stlib.o
 shutao  ~  libtest  gcc -Wall -g -c test.c -o test.o
 shutao  ~  libtest  ls
stlib.a  stlib.c  stlib.h  stlib.o  test.c  test.o
 shutao  ~  libtest  mv stlib.a libstlib.a
 shutao  ~  libtest  ls
libstlib.a  stlib.c  stlib.h  stlib.o  test.c  test.o
 shutao  ~  libtest   gcc -g -o test test.o -L. -lstlib
 shutao  ~  libtest  ls
libstlib.a  stlib.c  stlib.h  stlib.o  test  test.c  test.o
 shutao  ~  libtest  ./test
This is st.
 到此,静态库完成。

其中  stlib.c为

#include <stdio.h>
void hello()

        printf("This is st.\n");
}

  stlib.h为

extern void hello();

test.c为

#include "stlib.h"
int  main()
{
        hello();
        return 0;
}

三:动态库

具体操作如下

 技术分享图片

 

 其中  so_test.h为

extern void test_st();

test_st.c 为

#include<stdio.h>
#include "so_test.h"
void test_st()
{
                printf("This is in test_st..\n");
}

test.c为

#include "so_test.h"
int main()
{
                test_st();
                  return 0;
}

到此,简易静态、动态库的编写完成。

linux环境下库文件的编译

原文:https://www.cnblogs.com/superst/p/12538676.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!