# Makefile template for a shared library in C | |
# https://www.topbug.net/blog/2019/10/28/makefile-template-for-a-shared-library-in-c-with-explanations/ | |
CC = gcc # C compiler | |
CFLAGS = -fPIC -Wall -Wextra -O2 -g # C flags | |
LDFLAGS = -shared # linking flags | |
RM = rm -f # rm command | |
TARGET_LIB = libtarget.so # target lib | |
SRCS = main.c src1.c src2.c # source files | |
OBJS = $(SRCS:.c=.o) | |
.PHONY: all | |
all: ${TARGET_LIB} | |
$(TARGET_LIB): $(OBJS) | |
$(CC) ${LDFLAGS} -o $@ $^ | |
$(SRCS:.c=.d):%.d:%.c | |
$(CC) $(CFLAGS) -MM $< >$@ | |
include $(SRCS:.c=.d) | |
.PHONY: clean | |
clean: | |
-${RM} ${TARGET_LIB} ${OBJS} $(SRCS:.c=.d) |
This comment has been minimized.
This comment has been minimized.
From man gcc:
tl;dr - yes it can. |
This comment has been minimized.
This comment has been minimized.
Can you please explain its working. I mean I can see that you have generated .o files and linked em together to make the .so. I want to know like step by step execution of this makefile. (I'm new to makefiles and bash, so you might want to elaborate $^, $< etc). |
This comment has been minimized.
This comment has been minimized.
There are tons information on google about Makefile. Please google by yourself. @saqibahmed515 |
This comment has been minimized.
This comment has been minimized.
could you tell me how can i build static library with your Makefile? and how can i specify a special directory for .d and .o files. |
This comment has been minimized.
This comment has been minimized.
Better using |
This comment has been minimized.
This comment has been minimized.
much appreciated! needed this in a pinch. |
This comment has been minimized.
This comment has been minimized.
What about header file ? |
This comment has been minimized.
This comment has been minimized.
@nainesh1311 You don't need to add them to the Makefile, because we should only send source files directly to the compiler and the compiler will find header files automatically. |
This comment has been minimized.
This comment has been minimized.
Fix And...
|
This comment has been minimized.
This comment has been minimized.
@dr-begemot
which is exactly what your "correction" tries to add (with header files also taken care of). |
This comment has been minimized.
This comment has been minimized.
@xuhdev You wrote: $(TARGET_LIB): $(OBJS) I really want to understand) |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
How to force your way accept include path -I? |
This comment has been minimized.
This comment has been minimized.
@dr-begemot I've written a blog post explaining this file: https://www.topbug.net/blog/2019/10/28/makefile-template-for-a-shared-library-in-c-with-explanations/ HTH |
This comment has been minimized.
This comment has been minimized.
can any one tell me how to add shared library in my main project make file |
This comment has been minimized.
This comment has been minimized.
-l for example: |
This comment has been minimized.
This comment has been minimized.
thanks for response but one think i am not understanding actually i install embedded eclipse for windows when i create shared library output fill generated lib.so file as for my knowladge lib.so file generate base on linux system . why its happen like that if i successfully generated library then where i add the this line
in make
|
This comment has been minimized.
does -O2 and -g can co exist?