Reputation: 14970
My folder structure is as follows.
--->MyProgram
| a.c
| b.c
| c.c
| libzint/
The libzint folder contains a makefile of its own and it creates libzint.a in the folder MyProgram. When I call make in MyProgram folder. I want the libzint.a to be inserted into the executable printer.
If I do
$ nm printer | grep "libzint_function"
it doesn't show anything. What am I doing wrong?
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)
If I try to include libzint_funciton' in one of the
*.cfiles in
MyProgram` I get the following errors.
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
Upvotes: 0
Views: 219
Reputation: 140138
(first part of the answer assumed that OP successfully linked his executable, which wasn't the case):
that happens if you're not using the symbol in any of your .c
files.
When passing a .a
file, the linker selects the internal object files which are referenced in the object files. Other symbols are discarded.
So unless you're calling libzint_function
explicitly, the linker won't include the symbol.
You can force symbol inclusion without calling any functions from the lib with various techniques (explained here)
The actual problem:
But in your case, the executable is created but is invalid because of the unresolved symbols you're getting (saw that after you edited the question).
That's because the libzint.a
file refers to symbols from libpng
. You have to install that library (if not already installed) and link against it (after libzint.a
of course):
@$(CC) $(OBJECTS) -o $(OUTPUTNAME) $(LDFLAGS) libzint.a -lpng
Upvotes: 2