Youngsup Kim
Youngsup Kim

Reputation: 2235

How to link static lib in gcc make?

A static library is linked fine in the command line, but not through makefile. Compiling part accessing include files seems ok, but ld process must be wrong in the makefile. Thanks for your help in advance!

Here is my command line:

gcc -o quadEq.exe quadEq.c -I../include -L../lib -lnowic

Here is my Makefile:

CFLAGS = -Wall -g -c
INCLUDE = -I../include
LDFLAGS = -L../lib
LDLIBS = -lnowic
SOURCES = quadEq.c
OBJECTS = $(SOURCES:.c=.o)

EXE = quadEq

all: $(SOURCES) $(EXE)

$(EXE): $(OBJECTS)
    gcc $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $@ 

.c.o:
    gcc $(CFLAGS) $(INCLUDE) $< -o $@ 

Here is my screen capture that ran Makefile and the command line.

enter image description here

Upvotes: 0

Views: 787

Answers (2)

Youngsup Kim
Youngsup Kim

Reputation: 2235

Based on Aggieboy's suggestions, I rewrote the makefile and made it work.;

CC = gcc
CFLAGS = -x c -Wall -g 
INCPATH = -I../include
LIBPATH = -L../lib
LLIBS = -lnowic

%: %.c
    $(CC) -o $@ $(CFLAGS) $(INCPATH) $< $(LIBPATH) $(LLIBS) 

By the way, this accepts a filename to make from the command line argument. Thank you Aggieboy again!

Upvotes: 0

Suedocode
Suedocode

Reputation: 2534

Static libraries care in which order you link them. If libA.a depends on libB.a, then you have to do -lB -lA.

If a libA.a symbol depends on libB.a that depends on a libA.a symbol, you have to cyclically link: -lB -lA -lB. I've seen some cycles get to about 3 or 4 loops, but generally 2 is enough in my experience.

This is different from dynamic library linking which not only doesn't care what order you link them, but you don't need to also link dependent libraries since the .so specifies them.

Upvotes: 1

Related Questions