Ubuntu LDFLAGS - required

I have a C project that will not link correctly, and I suspect this because the -s flag needs to be passed to the gcc ld program. Because of this flag, gcc sees any linked library specified as an option in front of * .c files as unnecessary and will not link them.

PREFIX?=/usr/local CFLAGS=-D_LARGEFILE64_SOURCE=1 -g -Wall -I${PREFIX}/apr/include/apr-1 -I${PREFIX}/apr/include/apr-util-1 LDFLAGS=-lapr-1 -pthread -laprutil-1 all: devpkg devpkg: bstrlib.o db.o shell.o commands.o install: all install -d $(DESTDIR)/$(PREFIX)/bin/ install devpkg $(DESTDIR)/$(PREFIX)/bin/ clean: rm -f *.o rm -f devpkg rm -rf *.dSYM 

When I run this makefile, I get the following.

 cc -D_LARGEFILE64_SOURCE=1 -g -Wall -I/usr/local/apr/include/apr-1 -I/usr/local/apr/include/apr-util-1 -c -o bstrlib.o bstrlib.c cc -D_LARGEFILE64_SOURCE=1 -g -Wall -I/usr/local/apr/include/apr-1 -I/usr/local/apr/include/apr-util-1 -c -o db.o db.c cc -D_LARGEFILE64_SOURCE=1 -g -Wall -I/usr/local/apr/include/apr-1 -I/usr/local/apr/include/apr-util-1 -c -o shell.o shell.c cc -D_LARGEFILE64_SOURCE=1 -g -Wall -I/usr/local/apr/include/apr-1 -I/usr/local/apr/include/apr-util-1 -c -o commands.o commands.c cc -D_LARGEFILE64_SOURCE=1 -g -Wall -I/usr/local/apr/include/apr-1 -I/usr/local/apr/include/apr-util-1 -lapr-1 -pthread -laprutil-1 devpkg.c bstrlib.o db.o shell.o commands.o -o devpkg /tmp/ccZcAm9b.o: In function `main': /home/zach/Desktop/devpkgzed/devpkg.c:14: undefined reference to `apr_pool_initialize' /home/zach/Desktop/devpkgzed/devpkg.c:15: undefined reference to `apr_pool_create_ex' /home/zach/Desktop/devpkgzed/devpkg.c:29: undefined reference to `apr_getopt_init' /home/zach/Desktop/devpkgzed/devpkg.c:31: undefined reference to `apr_getopt' 

My problem is that I really do not understand how make approaches these commands through installed CFLAGS. How can I get linker options to follow the compilation part, and not vice versa, what causes this problem?

+4
source share
1 answer

Make has built-in rules for compiling source files and linking executable files and libraries. The commands you list are created using these rules.

The reason why you are unable to do this is that when linking the library, you should specify after the object files, because the linker performs one pass through its arguments and will discard any characters that were not allowed at the time of their viewing. To fix this, put your libraries in the LDLIBS variable, and not in the LDFLAGS variable (i.e. just replace LDFLAGS with LDLIBS ). The LDFLAGS variable is for non-library options for the linker, such as -L or -shared , etc.

+6
source

All Articles