Homunculus Reticulli
Homunculus Reticulli

Reputation: 68366

Creating a simple Makefile to build a shared library

I am trying to create a very basic hand crafted Makefile to create a shared library to illustrate a point.

This is what I have so far:

SHELL = /bin/sh
CC    = gcc
FLAGS        = -std=gnu99 -Iinclude
CFLAGS       = -fPIC -pedantic -Wall -Wextra -march=native -ggdb3
DEBUGFLAGS   = -O0 -D _DEBUG
RELEASEFLAGS = -O2 -D NDEBUG -combine -fwhole-program

TARGET  = example.so
SOURCES = $(shell echo src/*.c)
HEADERS = $(shell echo include/*.h)
OBJECTS = $(SOURCES:.c=.o)

PREFIX = $(DESTDIR)/usr/local
BINDIR = $(PREFIX)/bin

all: $(TARGET)

$(TARGET): $(OBJECTS)
    $(CC) $(FLAGS) $(CFLAGS) $(DEBUGFLAGS) -o $(TARGET) $(OBJECTS)

When I run make, it attempts to build an application - and ld fails because it can't resolve main().

Problem seems to be with CFLAGS - I have specified -fPIC but that is not working - what am I doing wrong?

Edit

I added the -shared flag as suggested, when I run make, I got this error:

gcc -std=gnu99 -Iinclude -fPIC -shared -pedantic -Wall -Wextra -march=native -ggdb3 -O0 -D _DEBUG -o example.so src/example.o
/usr/bin/ld: src/example.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
src/example.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [example.so] Error 1

Which seems to be suggesting to revert back to -fPIC only.

BTW, my new CFLAGS setting is:

CFLAGS       = -fPIC -shared -pedantic -Wall -Wextra -march=native -ggdb3

I am running gcc v4.4.3 on Ubuntu 10.0.4.

Upvotes: 44

Views: 122706

Answers (5)

rgb122
rgb122

Reputation: 146

Example for C++ files. I've also included a clean target.

.PHONY : clean

CPPFLAGS= -fPIC -g
LDFLAGS= -shared

SOURCES = $(shell echo *.cpp)
HEADERS = $(shell echo *.h)
OBJECTS=$(SOURCES:.cpp=.o)

FIKSENGINE_LIBDIR=../../../../lib
FIKSENGINE_INCDIR=../../../../include

TARGET=$(FIKSENGINE_LIBDIR)/tinyxml.so

all: $(TARGET)

clean:
    rm -f $(OBJECTS) $(TARGET)

$(TARGET) : $(OBJECTS)
    $(CC) $(CPPFLAGS) $(OBJECTS) -o $@ $(LDFLAGS)

Upvotes: 11

ThorSummoner
ThorSummoner

Reputation: 18079

this is my goto makefile rule for so files:

%.so: %.o ; $(LINK.c) $(LDFLAGS) -shared $^ -o $@

can be used like so

CFLAGS+=-fPIC

libmyfoo.so:  # create from libmyfoo.o

# or 

libmyfoo.so: myfoo.o  # create from myfoo.o 

Upvotes: 1

Homunculus Reticulli
Homunculus Reticulli

Reputation: 68366

The solution was to modify the XXFLAGS as follows:

FLAGS        = # -std=gnu99 -Iinclude
CFLAGS       = -fPIC -g #-pedantic -Wall -Wextra -ggdb3
LDFLAGS      = -shared

Upvotes: 38

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143061

Since you try to build so file, you probably need -shared.

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 476920

Compile with -shared:

gcc -o libfoo.so module1.o module2.o -shared

(This also works on MingW under Windows to produce DLLs.)

Upvotes: 18

Related Questions