简体   繁体   English

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

[英]makefile gcc custom dynamic lib linking issue with .so file

Guys I'm having a very hard time trying to make a custom .so file be correctly linked with the a another code file. 伙计们,我很难使一个自定义.so文件与另一个代码文件正确链接。

Where is the Makefile of the who creates the .so file: 谁创建.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

it's a bit long, but it's not hard ^^" This one compiles normally and create the "libNLF.so" file in the "BINARIES" folder. To be sure a run the command "nm -g libNLF.so | 它有点长,但并不难^^“这个文件可以正常编译并在” BINARIES“文件夹中创建” libNLF.so“文件。请确保运行命令” nm -g libNLF.so | grep NLF", and it gives me between other things, this line: grep NLF”,它使我在其他事情之间得到这一行:

0000000000001100 T NLF_init 0000000000001100 T NLF_init

Now lets to the problematic Makefile: 现在进入有问题的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

and the GCC gives me: 海湾合作委员会给了我:

auto_genareted_code.c:(.text.startup+0x7): undefined reference to `NLF_init' and all other funtions from libNLF.so are in the same situation. auto_genareted_code.c :(。text.startup + 0x7):未定义对`NLF_init'的引用以及来自libNLF.so的所有其他功能都处于相同的情况。

does some one knows what the hack in going on? 有人知道发生了什么事吗?

Adding to John Bollinger's solution, remember to put libraries linking at the end of compiler command, as order is important for library linking. 添加到John Bollinger的解决方案中时,请记住将库链接放在编译器命令的末尾,因为顺序对于库链接很重要。

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

It makes a difference where in the command you write this option; 在命令中写入此选项的位置会有所不同。 the linker searches and processes libraries and object files in the order they are specified. 链接器按照指定的顺序搜索和处理库和目标文件。 Thus, foo.o -lz bar.o' searches library z' after file foo.o but before bar.o. 因此, foo.o -lz bar.o' searches library '在文件foo.o之后但在bar.o之前foo.o -lz bar.o' searches library z'。 If bar.o refers to functions in `z', those functions may not be loaded. 如果bar.o引用了“ z”中的函数,则可能不会加载这些函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM