栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux 动态库编译链接

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/741198.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号