makefile for creating (.so) file from existing files

淺唱寂寞╮ 提交于 2019-11-28 01:50:22

问题


I have 4 files: 1.c, 1.h, 2.c, 2.h. I need a makefile, which will create a dynamic library (.so) from those 4 files. I have tried to write a makefile like this:

library.so : 1.c 1.h 2.c 2.h

but it did not work. It would be great, if someone helps me, thanks.


回答1:


Something like

 CC=gcc
 CFLAGS= -Wall -g -O -fPIC
 RM= rm -f
 .PHONY: all clean

 all: library.so
 clean:
      $(RM) *.o *.so

 library.so: 1.o 2.o
      $(LINK.c) -shared $< -o $@

 1.o: 1.c 1.h 2.h

 2.o: 2.c 1.h 2.h

But this is untested! I am assuming Linux with GNU make, and a directory containing only the source code of your library (with the above Makefile), which might be bad practice -you might want a test case- (you could have a special Makefile rule for %.pic.o depending on %.c, etc...)

Hints: use make -p to understand the builtin rules. Then make --trace or (with remake) remake -x to understand a bit more what make is doing.

Read also Drepper's paper: How to Write Shared Libraries, documentation of GNU make, Program Library HowTo, this answer, ...




回答2:


The simplest way is:

CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: 1.o 2.o
    $(LINK.cc) -shared $^ $(LOADLIBS) $(LDLIBS) -o $@

Slightly more advanced:

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

DEBUGFLAGS   = -O0 -D _DEBUG
RELEASEFLAGS = -O2 -D NDEBUG -combine -fwhole-program

TARGET  = example.so
SOURCES = $(wildcard *.c)
HEADERS = $(wildcard *.h)
OBJECTS = $(SOURCES:.c=.o)


all: $(TARGET)

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



回答3:


CC = gcc                                # C compiler
CFLAGS = -fPIC -Wall -Wextra -g         # C flags
LDFLAGS = -shared                       # linking flags
RM = rm -f                              # rm command
TARGET_LIB = sh_main.so                 # target lib

SRCS = add.c sub.c main.c               # source file
DEPS = header.h                         # header file
OBJS = $(SRCS:.c=.o)                    # object file

.PHONY: all
all: ${TARGET_LIB}

$(TARGET_LIB): $(OBJS)
        $(CC) ${LDFLAGS} -o $@ $^       # -o $@ says, put the output of the compilation in the file named on the left side of the :

$(SRCS:.c=.d):%.d:%.c
        $(CC) $(CFLAGS) -MM $< >$@      # the $< is the first item in the dependencies list, and the CFLAGS macro is defined as above
include $(SRCS:.c=.d)

.PHONY: clean
clean:
        -${RM} ${TARGET_LIB} ${OBJS} $(SRCS:.c=.d)

After the shared library created successfully. We need to install it.
Become the root user.
Copy the shared library into standard directory "/usr/lib".
Run ldcofig command.

Recompile your .c file with shared library. root@Admin:~/C/SharedLibrary# gcc -c main.c root@Admin:~/C/SharedLibrary# gcc -o main main.o sh_main.so

root@Admin:~/C/SharedLibrary# ldd main

Note: In my case.
main.c: main C file
sh_main.so: shared library.



来源:https://stackoverflow.com/questions/26689759/makefile-for-creating-so-file-from-existing-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!