Simple makefile for compiling a shared object library file
12,166
The simplest makefile that I can think of that does what you want:
CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: x.o y.o
g++ -shared $^ -o $@
In the alternative, you may want to use more of make's built-in rules and variables:
CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: x.o y.o
$(LINK.cc) -shared $^ $(LOADLIBES) $(LDLIBS) -o $@
Related videos on Youtube
Author by
pythonic
Updated on September 04, 2022Comments
-
pythonic over 1 year
I need a very simple makefile to compile a shared object library file (*.so). I also need to know how to pass optimization parameters like -O2 and -O3. I tried to search for simple examples using google, but all examples are twisted.
I don't need to create any version like *.so.1.0 but only a simple *.so file. My project would have multiple files, so I need an example which compiles multiple files.
-
Kerrek SB almost 12 yearsDo you not need to make explicit reference to
CXXFLAGS
in the invocation? Also, whyg++
and not$(CXX)
? -
pythonic almost 12 yearsThis is what I was looking for. Thanks.
-
matth almost 12 years@KerrekSB 1) No. CXXFLAGS is being used in the implicit
%.o: %.cc
rule. 2) To make it simple and easy-to-understand. But I have added the alternative that you suggest. -
matth almost 12 years@user1018562 - please note that I used
CXXFLAGS
because I used C++ as my test language. You may need to useCFLAGS
if you use C as your programming language.