创建三个文件:
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文件的程序名称的表。
AR | Archive-maintaining program; default `ar'. |
AS | Program for compiling assembly files; default `as'. |
CC | Program for compiling C programs; default `cc'. |
CO | Program for checking out files from RCS; default `co'. |
CXX | Program for compiling C++ programs; default `g++'. |
CPP | Program for running the C preprocessor, with results to standard output; default `$(CC) -E'. |
FC | Program for compiling or preprocessing Fortran and Ratfor programs; default `f77'. |
GET | Program for extracting a file from SCCS; default `get'. |
LEX | Program to use to turn Lex grammars into source code; default `lex'. |
YACC | Program to use to turn Yacc grammars into source code; default `yacc'. |
LINT | Program to use to run lint on source code; default `lint'. |
M2C | Program to use to compile Modula-2 source code; default `m2c'. |
PC | Program for compiling Pascal programs; default `pc'. |
MAKEINFO | Program to convert a Texinfo source file into an Info file; default `makeinfo'. |
TEX | Program to make TeX dvi files from TeX source; default `tex'. |
TEXI2DVI | Program to make TeX dvi files from Texinfo source; default `texi2dvi'. |
WEAVE | Program to translate Web into TeX; default `weave'. |
CWEAVE | Program to translate C Web into TeX; default `cweave'. |
TANGLE | Program to translate Web into Pascal; default `tangle'. |
CTANGLE | Program to translate C Web into C; default `ctangle'. |
RM | Command to remove a file; default `rm -f'. |
这里是一个变量,其值是上述程序的额外的参数表。所有这些的默认值是空字符串,除非另有说明
ARFLAGS | Flags to give the archive-maintaining program; default `rv'. |
ASFLAGS | Extra flags to give to the assembler (when explicitly invoked on a `.s' or `.S' file). |
CFLAGS | Extra flags to give to the C compiler. |
CXXFLAGS | Extra flags to give to the C compiler. |
COFLAGS | Extra flags to give to the RCS co program. |
CPPFLAGS | Extra flags to give to the C preprocessor and programs that use it (the C and Fortran compilers). |
FFLAGS | Extra flags to give to the Fortran compiler. |
GFLAGS | Extra flags to give to the SCCS get program. |
LDFLAGS | Extra flags to give to compilers when they are supposed to invoke the linker, `ld'. |
LFLAGS | Extra flags to give to Lex. |
YFLAGS | Extra flags to give to Yacc. |
PFLAGS | Extra flags to give to the Pascal compiler. |
RFLAGS | Extra flags to give to the Fortran compiler for Ratfor programs. |
LINTFLAGS | Extra flags to give to lint. |
参考这个博主的: 参考原动态链接