makefile gcc自定义动态库链接问题与.so文件

时间:2016-02-09 20:41:49

标签: c gcc makefile dynamic-linking dynamic-library

伙计们我很难尝试将自定义.so文件与另一个代码文件正确链接。

who创建.so文件的Makefile在哪里:

#the compiler
CC = gcc

#the standart ompilation flags of the project
CFLAGS = -O3 -Wall -Wno-unused-variable -Wno-unused-but-set-variable -Wno-implicit-function-declaration

#path to the folder's root, where the holy not build framework is. Relate to this Makefile
PREPATH = ../

#Path to the SDL, SDL_image and Lua includes and libs
SDL2INCLUDES = -I $(PREPATH)SDL2/include
SDL2LIBS = $(PREPATH)SDL2/lib/libSDL2-2.0.so.0 $(PREPATH)SDL2/lib/libSDL2_test.a $(PREPATH)SDL2/lib/libSDL2-2.0.so.0.2.1 $(PREPATH)SDL2/lib/libSDL2main.a $(PREPATH)SDL2/lib/libSDL2.a $(PREPATH)SDL2/lib/libSDL2.so
SDLIMAGE2INCLUDES = -I $(PREPATH)SDL2/SDL_image
SDLIMAGE2LIBS = $(PREPATH)SDL2/lib/libSDL2_image-2.0.so.0 $(PREPATH)SDL2/lib/libSDL2_image.so $(PREPATH)SDL2/lib/libSDL2_image-2.0.so.0.0.0 $(PREPATH)SDL2/lib/libSDL2_image.a
LUAINCLUDES = -I $(PREPATH)lua/
LUALIB = -L $(PREPATH)lua/ -llua -lm

#Where to put the compiled program
COMPILEPATH = $(PREPATH)BINARIES/



#Build options

build: NLF.o
    cp ./*.o $(COMPILEPATH)
    $(CC) -shared $(CFLAGS) $(SDL2INCLUDES) $(SDL2LIBS) $(SDLIMAGE2INCLUDES) $(SDLIMAGE2LIBS) $(LUAINCLUDES) $(LUALIB) $(COMPILEPATH)*.o -o $(COMPILEPATH)libNLF.so

NLF.o: NLF_osservice.o NLF_Error.o NLF_event.o NLF.h.gch
    $(CC) $(CFLAGS) $(SDL2INCLUDES) $(SDLIMAGE2INCLUDES) $(LUAINCLUDES) -fPIC -c NLF.c

NLF.h.gch: NLF.h
    $(CC) $(CFLAGS) $(SDL2INCLUDES) $(SDLIMAGE2INCLUDES) $(LUAINCLUDES) NLF.h

NLF_osservice.o: NLF_osservice.h.gch
    $(CC) $(CFLAGS) $(SDL2INCLUDES) $(SDLIMAGE2INCLUDES) $(LUAINCLUDES) $(NLFINCLUDES) -fPIC -c NLF_osservice.c
NLF_osservice.h.gch:
    $(CC) $(CFLAGS) $(SDL2INCLUDES) $(SDLIMAGE2INCLUDES) $(LUAINCLUDES) $(NLFINCLUDES) NLF_osservice.h

NLF_Error.o: NLF_error.h.gch
    $(CC) $(CFLAGS) $(SDL2INCLUDES) $(SDLIMAGE2INCLUDES) $(LUAINCLUDES) $(NLFINCLUDES) -fPIC -c NLF_error.c
NLF_error.h.gch:
    $(CC) $(CFLAGS) $(SDL2INCLUDES) $(SDLIMAGE2INCLUDES) $(LUAINCLUDES) $(NLFINCLUDES) NLF_error.h

NLF_event.o: NLF_event.h.gch
    $(CC) $(CFLAGS) $(SDL2INCLUDES) $(SDLIMAGE2INCLUDES) $(LUAINCLUDES) $(NLFINCLUDES) -fPIC -c NLF_event.c
NLF_event.h.gch:
    $(CC) $(CFLAGS) $(SDL2INCLUDES) $(SDLIMAGE2INCLUDES) $(LUAINCLUDES) $(NLFINCLUDES) NLF_event.h  



#cleaning options

clean-build:
    rm -f -v $(COMPILEPATH)*.o

clean-hard clean-all:
    rm -f -v ./*.o ./*.h.gch
    rm -f -v $(COMPILEPATH)*.o $(COMPILEPATH)*.so

clean-soft clean-all-but-so:
    rm -f -v ./*.o ./*.h.gch

它有点长,但它并不难^^" 这个正常编译并创建" libNLF.so"文件在" BINARIES"夹。 为了确保运行命令" nm -g libNLF.so | grep NLF",它给了我其他的东西,这一行:

  

0000000000001100 T NLF_init

现在让我们看看有问题的Makefile:

CC = gcc
CFLAGS = -O3
PREPATH = ../

SDL2INCLUDES = -I $(PREPATH)SDL2/include
SDL2LIBS = -L $(PREPATH)SDL2/lib/libSDL2-2.0.so.0 -L $(PREPATH)SDL2/lib/libSDL2_test.a -L $(PREPATH)SDL2/lib/libSDL2-2.0.so.0.2.1 -L $(PREPATH)SDL2/lib/libSDL2main.a -L $(PREPATH)SDL2/lib/libSDL2.a -L $(PREPATH)SDL2/lib/libSDL2.so
SDLIMAGE2INCLUDES = -I $(PREPATH)SDL2/SDL_image
SDLIMAGE2LIBS = -L $(PREPATH)SDL2/lib/libSDL2_image-2.0.so.0 -L $(PREPATH)SDL2/lib/libSDL2_image.so -L $(PREPATH)SDL2/lib/libSDL2_image-2.0.so.0.0.0 -L $(PREPATH)SDL2/lib/libSDL2_image.a
#LUAINCLUDES = -I $(PREPATH)lua/
#LUALIB = -L $(PREPATH)lua/ -llua -lm

NLFINCLUDES = -I $(PREPATH)NLF_SCR/
NLFLIB = -L ../BINARIES/libNLF.so


build:
    $(CC) $(CFLAGS) auto_genareted_code.c $(NLFINCLUDES) $(SDL2INCLUDES) $(NLFLIB) $(SDL2LIBS) -o auto_genareted_code

GCC给了我:

  

auto_genareted_code.c :(。text.startup + 0x7):未定义引用`NLF_init'   来自libNLF.so的所有其他功能都处于相同的情况。

有人知道发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

添加到John Bollinger的解决方案中,记得在编译器命令结束时放置库链接,因为顺序对于库链接很重要。

http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

  

在您编写此选项的命令中,它会有所不同;       链接器搜索并处理库中的库和目标文件       他们是指定的。       因此,foo.o -lz bar.o' searches library z'在文件foo.o之后但是       在bar.o之前如果bar.o引用`z'中的函数,那些函数       可能没有加载。