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 $@
Share:
12,166

Related videos on Youtube

pythonic
Author by

pythonic

Updated on September 04, 2022

Comments

  • pythonic
    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
    Kerrek SB almost 12 years
    Do you not need to make explicit reference to CXXFLAGS in the invocation? Also, why g++ and not $(CXX)?
  • pythonic
    pythonic almost 12 years
    This is what I was looking for. Thanks.
  • matth
    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
    matth almost 12 years
    @user1018562 - please note that I used CXXFLAGS because I used C++ as my test language. You may need to use CFLAGS if you use C as your programming language.