欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

linux动态库编译链接

时间:2023-05-17

创建三个文件:

testa.c testb.c testc.c so_test.h

使用这三个文件编译动态库libtest.so

testa.c

#include "so_test.h"void test_a(){ printf("this is in test_a...n");}

testb.c

#include "so_test.h"void test_b(){ printf("this is test_b .、n");}

testc.c

#include "so_test.h"void test_c(){ printf("this is test_c n");}

so_test.h

#ifndef _SO_TEST_H#define _SO_TEST_H#include "stdio.h"void test_a();void test_b();void test_c();#endif

test.c 用来测试链接动态库

#include "so_test.h"int main(int argc, char *argv[]){ test_a(); test_b(); test_c(); return 0;}

Makefile

#编译动态库 libtest.so #$@ 目标文件#$^ 以来文件集合#$< 第一个依赖文件#-修饰 报错后不会停止推出,继续向下执行#@修饰 隐藏信息TARGET = libtest.so #目标文件CC = gcc #CC是默认编译命令OBJS = testa.o testb.o testc.o #以来文件CFLAGS = -O2 -Wall -shared -fPIC -D_GNU_SOURCE -DNR_LINUX #编译选项.PHONY:allall:$(TARGET) $(TARGET):$(OBJS) $(CC) -shared -o $@ $^ %.o:%.c $(CC) -c $< $(CFLAGS).PHONY:cleanclean: echo "start clean" -@rm $(OBJS) $(TARGET) test test.o echo "clean end".PHONY:testtest:test.o $(CC) -o $@ $^ -L xxx/test_make/test_so -ltest%.o:%.c $(CC) -c $< $(CFLAGS)

直接执行:

make    编译出 libtest.so文件

执行 make test 编译出  测试文件

 执行 test报错

./test: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory

 -L 制定了动态库的路径

-l (L的小写) 制定了动态库的名字,编译出来的动态库文件名是 libtest.so ,指定时需要去掉lib 和.so后缀,只需要 test就可以;

但是上述错误提示找不到也编译同步:

需要在启动脚本中 或者 etc/profile 或者测试终端窗口中定义LD_LIBRARY_PATH

LD_LIBRARY_PATH 指定动态库链接的路径

如下:

1、终端中测试: 执行ok

 2、脚本中:

start.sh

 chmod +x start.sh 之后执行 效果一直

所以动态库执行的时候才会在给定的目录中找这个文件编译的时候没有报错; 

比较常见的变量用作内置规则:makefile文件的程序名称的表。

ARArchive-maintaining program; default `ar'.ASProgram for compiling assembly files; default `as'.CCProgram for compiling C programs; default `cc'.COProgram for checking out files from RCS; default `co'.CXXProgram for compiling C++ programs; default `g++'.CPPProgram for running the C preprocessor, with results to standard output; default `$(CC) -E'.FCProgram for compiling or preprocessing Fortran and Ratfor programs; default `f77'.GETProgram for extracting a file from SCCS; default `get'.LEXProgram to use to turn Lex grammars into source code; default `lex'.YACCProgram to use to turn Yacc grammars into source code; default `yacc'.LINTProgram to use to run lint on source code; default `lint'.M2CProgram to use to compile Modula-2 source code; default `m2c'.PCProgram for compiling Pascal programs; default `pc'.MAKEINFOProgram to convert a Texinfo source file into an Info file; default `makeinfo'.TEXProgram to make TeX dvi files from TeX source; default `tex'.TEXI2DVIProgram to make TeX dvi files from Texinfo source; default `texi2dvi'.WEAVEProgram to translate Web into TeX; default `weave'.CWEAVEProgram to translate C Web into TeX; default `cweave'.TANGLEProgram to translate Web into Pascal; default `tangle'.CTANGLEProgram to translate C Web into C; default `ctangle'.RMCommand to remove a file; default `rm -f'.

这里是一个变量,其值是上述程序的额外的参数表。所有这些的默认值是空字符串,除非另有说明

ARFLAGSFlags to give the archive-maintaining program; default `rv'.ASFLAGSExtra flags to give to the assembler (when explicitly invoked on a `.s' or `.S' file).CFLAGSExtra flags to give to the C compiler.CXXFLAGSExtra flags to give to the C compiler.COFLAGSExtra flags to give to the RCS co program.CPPFLAGSExtra flags to give to the C preprocessor and programs that use it (the C and Fortran compilers).FFLAGSExtra flags to give to the Fortran compiler.GFLAGSExtra flags to give to the SCCS get program.LDFLAGSExtra flags to give to compilers when they are supposed to invoke the linker, `ld'.LFLAGSExtra flags to give to Lex.YFLAGSExtra flags to give to Yacc.PFLAGSExtra flags to give to the Pascal compiler.RFLAGSExtra flags to give to the Fortran compiler for Ratfor programs.LINTFLAGSExtra flags to give to lint.

参考这个博主的: 参考原动态链接

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。