Creating a simple Makefile to build a shared library

前端 未结 5 1444
天涯浪人
天涯浪人 2021-01-30 21:37

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         


        
相关标签:
5条回答
  • 2021-01-30 21:46

    Compile with -shared:

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

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

    0 讨论(0)
  • 2021-01-30 21:46

    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 
    
    0 讨论(0)
  • 2021-01-30 21:57

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

    0 讨论(0)
  • 2021-01-30 22:08

    The solution was to modify the XXFLAGS as follows:

    FLAGS        = # -std=gnu99 -Iinclude
    CFLAGS       = -fPIC -g #-pedantic -Wall -Wextra -ggdb3
    LDFLAGS      = -shared
    
    0 讨论(0)
  • 2021-01-30 22:11

    Example for C++ files. 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) $(CFLAGS) $(OBJECTS) -o $@ $(LDFLAGS)
    
    0 讨论(0)
提交回复
热议问题