linux 动态库编译链接

创建三个文件:

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:all

all:$(TARGET) 

$(TARGET):$(OBJS)
        $(CC) -shared -o $@ $^ 

%.o:%.c
        $(CC) -c $< $(CFLAGS)

.PHONY:clean
clean:
        echo "start clean"
        -@rm $(OBJS) $(TARGET) test test.o
        echo "clean end"

.PHONY:test

test: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.

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值