0

Similar to this post: Compile multiple C source fles into a unique object file

I have to compile multiple C files into a single dynamic shared library. Here is what I have in my Makefile:

_OBJS = file1.o file2.o

CFLAGS += -Wall -I../include 

SHARED_LIB =libdynamic.so

.PHONY: all
all:  (SHARED_LIB)

$(SHARED_LIB): 
        $(CC) -fPIC -c file1.c file2.c $(CFLAGS) 
        $(CC) -shared -o $(SHARED_LIB) -L$/lib -ldl $(_OBJS)

However the generated shared library doesn't have the functions belonging to file2.c. How can I get it working?

Thanks.

tweet
  • 51
  • 2
  • 11

1 Answers1

1

You're not creating the object files (*.o).

Try:

$(SHARED_LIB):
    $(CC) -fPIC -c file1.c -o file1.o $(CFLAGS)
    $(CC) -fPIC -c file2.c -o file2.o $(CFLAGS)
    $(CC) -shared -o $(SHARED_LIB) -L$/lib -ldl $(_OBJS)

Also, you are missing a '$' after the all: target.

jdc
  • 680
  • 1
  • 8
  • 11
  • Well, you have to add the separator. :P Makefiles are very picky - the lines after `$(SHARED_LIB):` MUST have a hard tab as the first character (cannot use spaces). – jdc Jun 04 '18 at 20:24
  • Unfortunately, I get the follwoing error when I followed your step:arm-linux-gnueabihf-gcc: fatal error: cannot specify -o with -c, -S or -E with multiple files – tweet Jun 04 '18 at 20:26
  • Thanks for the Makefile tip. Yes, I added the tab. Was too quick to report error ;) – tweet Jun 04 '18 at 20:28
  • Based on what you are trying to do and the error you are getting, see https://stackoverflow.com/questions/16177790/g-fatal-error-cannot-specify-o-with-c-s-or-e-with-multiple-files/16177962. Maybe this will help you see what you are missing. – jdc Jun 05 '18 at 00:02