[英]linking a static library into an executable
我的文件夹结构如下。
--->MyProgram
| a.c
| b.c
| c.c
| libzint/
libzint文件夹包含其自己的makefile,并在MyProgram文件夹中创建libzint.a。 当我在MyProgram文件夹中调用make时。 我希望将libzint.a插入可执行打印机。
如果我做
$ nm printer | grep "libzint_function"
它什么也没显示。 我究竟做错了什么?
PATHTOMAKEVARIABLES=../
include $(PATHTOMAKEVARIABLES)/Makefile.variable
PREFIX := /usr
LIBDIR := $(PREFIX)/lib
LIBDIR2 := .
PRJROOT:=.
INCDIRS := -I. -I./inc/ -I../lib/inc -I./libzint
ECFLAGS :=
CFLAGS := -DPRN -D_GNU_SOURCE -O2 $(INCDIRS) $(ECFLAGS)
LDFLAGS := -lrt -lm -lpthread -lclog -fpack-struct -L$(LIBDIR) -L$(LIBDIR2)
OBJPATH := ./Debug
OUTPUTNAME := printer
SOURCES := $(PRJROOT)/src/a.c \
$(PRJROOT)/src/b.c \
$(PRJROOT)/src/c.c
OBJECTS=$(SOURCES:.c=.o)
all: $(OUTPUTNAME)
@echo "Build complete"
$(OUTPUTNAME) : clean $(OBJECTS) $(LIBZINT)
@$(CC) $(OBJECTS) -o $(OUTPUTNAME) $(LDFLAGS) libzint.a
LIBZINT:
$(MAKE) -C libzint
clean:
@-$(RM) $(OBJECTS)
@-$(RM) $(PRJROOT)/$(OUTPUTNAME)
如果我尝试files in
MyProgram libzint_funciton' in one of the
* .c files in
libzint_funciton' in one of the
包含libzint_funciton' in one of the
出现以下错误。
libzint.a(png.o): In function `writepng_error_handler':
png.c:(.text+0x40): undefined reference to `png_get_error_ptr'
libzint.a(png.o): In function `png_pixel_plot':
png.c:(.text+0x500): undefined reference to `png_create_write_struct'
png.c:(.text+0x54c): undefined reference to `png_create_info_struct'
png.c:(.text+0x570): undefined reference to `png_destroy_write_struct'
png.c:(.text+0x5cc): undefined reference to `png_destroy_write_struct'
png.c:(.text+0x610): undefined reference to `png_init_io'
png.c:(.text+0x620): undefined reference to `png_set_compression_level'
png.c:(.text+0x670): undefined reference to `png_set_IHDR'
png.c:(.text+0x684): undefined reference to `png_write_info'
png.c:(.text+0x690): undefined reference to `png_set_packing'
png.c:(.text+0x7f4): undefined reference to `png_write_row'
png.c:(.text+0x94c): undefined reference to `png_write_row'
png.c:(.text+0xab0): undefined reference to `png_write_row'
png.c:(.text+0xc08): undefined reference to `png_write_row'
png.c:(.text+0xc38): undefined reference to `png_write_end'
png.c:(.text+0xc64): undefined reference to `png_destroy_write_struct'
collect2: error: ld returned 1 exit status
(答案的第一部分假设OP成功链接了他的可执行文件,情况并非如此):
如果您未在任何.c
文件中使用该符号,则会发生这种情况。
传递.a
文件时,链接程序选择在目标文件中引用的内部目标文件。 其他符号将被丢弃。
因此,除非您显式调用libzint_function
,否则链接程序将不包含该符号。
您可以使用各种技术来强制包含符号,而无需从lib中调用任何函数( 在此处说明)
实际问题:
但是在您的情况下,可执行文件已创建,但由于您得到的符号未解决而无效(在编辑问题后会看到该符号)。
这是因为libzint.a
文件引用了libpng
符号。 您必须安装该库(如果尚未安装)并链接到它(当然在 libzint.a
之后):
@$(CC) $(OBJECTS) -o $(OUTPUTNAME) $(LDFLAGS) libzint.a -lpng
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.