1

So, I have been driving myself crazy trying to get this makefile to work. I am trying to take three .c source files and 2.h header files and work them together using a single makefile to set them up with dynamic libraries.

Here is the code:

SHELL=/bin/bash
OBJDIR = obj/
OBJS = $(OBJDIR)parser.o $(OBJDIR)shutil.o $(OBJDIR)sshell.o
EXCOBJS = $(OBJDIR)sshell.o
HDRDIR = ./include/
HEADER_FILES = $(HDRDIR)shell.h $(HDRDIR)parser.h
SRCDIR = ./src/
LIBDIR = ./lib/
LIBS = $(LIBDIR)libparser.so $(LIBDIR)libshell.so
SODIR = $(shell pwd)/temp/lib/ 
LIBIMP = -lparser -lshell
EXECUTABLE = simpleshell
CFLAGS = -Wall
SOFLAGS = -shared -Wl,-soname,
CC = gcc
NOH =
# End of configuration options

#What needs to be built to make all files and dependencies
all: $(EXECUTABLE)

#Create the main executable
$(EXECUTABLE): $(EXCOBJS) $(LIBS)
    $(CC) $(CFLAGS) -Wl,-rpath,$(SODIR) -L$(SODIR) $(LIBIMP) ./$(EXCOBJS) -o $(EXECUTABLE)

$(EXCOBJS): $(HEADER_FILES)

$(LIBDIR)%.so: $(OBJS)
    $(CC) $(SOFLAGS)$(@F).1 -o $(LIBDIR)$(@F).1.0 $(OBJDIR)$(strip $(patsubst shell%, 
    shutil%, $(patsubst %.so, %.o, $(patsubst lib%, %, $(@F)))))
    cp $(LIBDIR)$(@F).1.0 $(SODIR)
    ln -sf $(SODIR)$(@F).1.0 $(SODIR)$(@F).1
    ln -sf $(SODIR)$(@F).1.0 $(SODIR)$(@F).1


$(OBJDIR)sshell.o: $(SRCDIR)sshell.c
    $(shell if [ ! -d "$(OBJDIR)" ]; then mkdir ./obj; fi)
    $(shell if [ ! -d "$(LIBDIR)" ]; then mkdir ./lib; fi)
    $(shell if [ ! -d "$(SODIR)" ]; then mkdir ./temp; mkdir ./temp/lib; fi)
    $(CC) $(CFLAGS) -I$(HDRDIR) -c $< -o ./$@

$(OBJDIR)%.o: $(SRCDIR)%.c
    $(CC) $(CFLAGS) -fPIC -I$(HDRDIR) -c $< -o ./$@

clean:
    -rm -f $(EXECUTABLE) $(OBJDIR)*.o $(LIBDIR)*.so* $(SODIR)libparser.so*     
$(SODIR)libshell.so*
    -rm -rf ./temp

run: $(EXECUTABLE)
    ./$(EXECUTABLE)

tarball:
    -rm -f $(EXECUTABLE) $(OBJDIR)*.o $(LIBDIR)*.so* $(SODIR)libparser.so*     
$(SODIR)libshell.so*
    (cd .. ; tar czf Lanini_Chris_a4_libraries.tar.z shell )

# End of Makefile

And here is the error I keep getting:

sshell.c:(.text+0x1b): undefined reference to `signal_c_init'
sshell.c:(.text+0x13f): undefined reference to `stripcrlf'
sshell.c:(.text+0x14e): undefined reference to `parse'
collect2: error: ld returned 1 exit status
make: *** [simpleshell] Error 1
rm obj/parser.o obj/shutil.o

My sanity significantly thanks you for any help.

Starlncr34
  • 31
  • 1
  • 6
  • What are the compilation units where these functions are defined? Are they from your project or from a third-party library or libraries? – user3159253 Apr 15 '14 at 23:41
  • 4
    Have you tried adding `$(LIBIMP)` at the end rather than interspersed with other arguments in the rule for `$(EXECUTABLE) `? –  Apr 15 '14 at 23:46
  • The $(LIBIMP) move did it. You are my hero, thanks! – Starlncr34 Apr 15 '14 at 23:53

0 Answers0