Makefile - Only Makes One of Two Libs - c

I am new to makefile and am trying to compile several files.
First is called s-chat and the others are RWT (reader-writer) and DP (dining philosopher). They both use my list library, but s-chat needs it to compile with the -m32 flags.
This is what I tried to do: I called one libMonitor.a using lib-adders.o, lib_movers.o, lib_removers.o. The other one is liblist_32 using lib_adders_32.o, lib_removers_32.o, lib_movers_32.o. For some reason, only the first of these two libraries is made, and the other says it cannot find its first dependency.
PTHREADS = /student/cmpt332/pthreads
RTT = /student/cmpt332/rtt
CC = gcc
CFLAGS = -g
CPPFLAGS = -std=c90 -Wall -pedantic
.PHONEY: all clean
ARCH = $(shell uname -sm | tr -d ' ')
ifeq ($(ARCH),SunOS)
ARCH = $(PLATFORM)
PROCESSOR = "$(shell uname -p)"
ifeq ($(PROCESSOR),i386)
ARCH = i86pc
endif
endif
all: s-chat reader-writer-test dining-philosophers-test
RWT_OBJS = reader-writer_$(ARCH).o reader-writer-monitor_$(ARCH).o Monitor_$(ARCH).o libMonitor_$(ARCH).a
reader-writer-test: CPPFLAGS += -I$(PTHREADS)
reader-writer-test: LDFLAGS += -L$(PTHREADS)/lib/$(ARCH)
reader-writer-test: LDLIBS += -lpthreads
reader-writer-test: $(RWT_OBJS)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $#
reader-writer_$(ARCH).o: reader-writer.c reader_writer_monitor.h Monitor.h
reader-writer-monitor_$(ARCH).o: reader-writer-monitor.c reader_writer_monitor.h Monitor.h
DP_OBJS = dining-philosophers_$(ARCH).o dining-philosophers-monitor_$(ARCH).o Monitor_$(ARCH).o libMonitor_$(ARCH).a
dining-philosophers-test: CPPFLAGS += -I$(PTHREADS)
dining-philosophers-test: LDFLAGS += -L$(PTHREADS)/lib/$(ARCH)
dining-philosophers-test: LDLIBS += -lpthreads
dining-philosophers-test: $(DP_OBJS)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $#
dining-philosophers_$(ARCH).o: dining-philosophers.c dining_philosophers_monitor.h Monitor.h
dining-philosophers-monitor_$(ARCH).o: dining-philosophers-monitor.c dining_philosophers_monitor.h Monitor.h
Monitor_$(ARCH).o: Monitor.c Monitor.h
list_adders.o: list_adders.c list.h
list_movers.o: list_movers.c list.h
list_removers.o: list_removers.c list.h
libMonitor_$(ARCH).a: list_adders.o list_movers.o list_removers.o
ar rcs $# $^
list_adders_32.o: CFLAGS += -m32
list_adders_32.o: LDFLAGS += -m32
list_adders_32.o: list_adders.c list.h
list_movers_32.o: CFLAGS += -m32
list_movers_32.o: LDFLAGS += -m32
list_movers_32.o: list_movers.c list.h
list_removers_32.o: CFLAGS += -m32
list_removers_32.o: LDFLAGS += -m32
list_removers_32.o: list_removers.c list.h
liblist_32_$(ARCH).a: list_adders_32.o list_movers_32.o list_removers_32.o
ar rcs $# $^
s-chat: CPPFLAGS += -I$(RTT)/include
s-chat: CFLAGS += -m32
s-chat: LDFLAGS += -L$(RTT)/lib/$(ARCH) -m32
s-chat: LDLIBS += -lRtt -lRttUtils
s-chat: s-chat.o liblist_32_$(ARCH).a
s-chat.o: s-chat.c
%_$(ARCH).o %_32_$(ARCH).o %.o %_32.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
%: %.o
$(CC) $(LDFLAGS) $(LDLIBS) $^ -o $#
clean:
rm -rf *.o *.a reader-writer-test dining-philosophers-test s-chat
This is the error I get:
ar rcs libMonitor_Linuxx86_64.a list_adders.o list_movers.o list_removers.o
ar: list_adders.o: No such file or directory
Makefile:66: recipe for target 'libMonitor_Linuxx86_64.a' failed
make: *** [libMonitor_Linuxx86_64.a] Error 1
And if I make s-chat last, libMonitor is made, but I get the exact same error with liblist_32 and it says it can't find list_adders_32.o
Anyone know what's going on and how to fix this?
Thanks in advance!!

https://www.gnu.org/software/make/manual/make.html#Pattern-Intro
If a pattern rule has multiple targets, make knows that the rule’s recipe is responsible for making all of the targets. The recipe is executed only once to make all the targets.
%_$(ARCH).o %_32_$(ARCH).o %.o %_32.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
Should be something like
%_$(ARCH).o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
%_32_$(ARCH).o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
%.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
%_32.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
But since it seems like all of your sources are in the same directory anyway, you could just delete these rules completely because make already has a built-in rule for %.o: %.c.

Related

make: *** No rule to make target '-std=c99'

I've been reading so many make guide but I still cant make it work.
I'm trying to create a makefile that creates different programs, each program has its own source file (.c) but they all use the same header file (.h) nad all the files are in the same directory.
For now i have this:
CC = gcc
CFLAGS = -std=c99 -g -c -Wall
LFLAGS = -std=c99 -g -c -Wall
OBJS = nivel1.o nivel2.o nivel3.o nivel4.o nivel5.o nivel6.o my_shell.o
OUT = nivel1,nivel2,nivel3,nivel4,nivel5,nivel6,my_shell
OBJS0 = nivel1.o
SOURCE0 = nivel1.c
HEADER0 = my_shell.h
OUT0 = nivel1
OBJS1 = nivel2.o
SOURCE1 = nivel2.c
HEADER1 = my_shell.h
OUT1 = nivel2
OBJS2 = nivel3.o
SOURCE2 = nivel3.c
HEADER2 = my_shell.h
OUT2 = nivel3
OBJS3 = nivel4.o
SOURCE3 = nivel4.c
HEADER3 = my_shell.h
OUT3 = nivel4
OBJS4 = nivel5.o
SOURCE4 = nivel5.c
HEADER4 = my_shell.h
OUT4 = nivel5
OBJS5 = nivel6.o
SOURCE5 = nivel6.c
HEADER5 = my_shell.h
OUT5 = nivel6
OBJS6 = my_shell.o
SOURCE6 = my_shell.c
HEADER6 = my_shell.h
OUT6 = my_shell
all: nivel1 nivel2 nivel3 nivel4 nivel5 nivel6 my_shell
nivel1: $(OBJS0) $(LFLAGS)
$(CC) -g $(OBJS0) -o $(OUT0)
nivel2: $(OBJS1) $(LFLAGS)
$(CC) -g $(OBJS1) -o $(OUT1)
nivel3: $(OBJS2) $(LFLAGS)
$(CC) -g $(OBJS2) -o $(OUT2)
nivel4: $(OBJS3) $(LFLAGS)
$(CC) -g $(OBJS3) -o $(OUT3)
nivel5: $(OBJS4) $(LFLAGS)
$(CC) -g $(OBJS4) -o $(OUT4)
nivel6: $(OBJS5) $(LFLAGS)
$(CC) -g $(OBJS5) -o $(OUT5)
my_shell: $(OBJS6) $(LFLAGS)
$(CC) -g $(OBJS6) -o $(OUT6)
nivel1.o: nivel1.c
$(CC) $(FLAGS) nivel1.c -std=c99
nivel2.o: nivel2.c
$(CC) $(FLAGS) nivel2.c -std=c99
nivel3.o: nivel3.c
$(CC) $(FLAGS) nivel3.c -std=c99
nivel4.o: nivel4.c
$(CC) $(FLAGS) nivel4.c -std=c99
nivel5.o: nivel5.c
$(CC) $(FLAGS) nivel5.c -std=c99
nivel6.o: nivel6.c
$(CC) $(FLAGS) nivel6.c -std=c99
my_shell.o: my_shell.c
$(CC) $(FLAGS) my_shell.c -std=c99
clean:
rm -f $(OBJS) $(OUT)
I've tried changing many things but nothing gets it to work,
any help would be appreciated!
PD: I need it to be std c99.
nivel1: $(OBJS0) $(LFLAGS)
$(CC) -g $(OBJS0) -o $(OUT0)
is wrong. That is why make complains. You specified a dependance on $(LFLAGS) but that is not a file or a target, that is just a flag definition used at compile-time. You probably want:
nivel1: $(OBJS0)
$(CC) $(LFLAGS) -g $(OBJS0) -o $(OUT0)
---EDIT---(suggested by Kaylum)
LFLAGS has a wrong value. Since this command is a linking phase, neither -std=c99, -c nor -Wall are usable.

Makefile adding libraries from others directory

I want to create a Makefile for a big C project, with files divided into directories. What I wrote is
CC = gcc
AR = ar
ARFLAGS = rvs
CFLAGS += -std=c99 -Wall -g
INCDIR = ./include
LIBDIR = ./lib
SRCDIR = ./src
BINDIR = ./bin
OBJDIR = ./obj
TESTDIR = ./testfile
INCLUDES = -I.
LDFLAGS = -L/lib
LIBFUNCTIONS = /-lfunctions
LIBTH = -lpthread
LIBUTILITY = /-lutility
OPTFLAGS = -O3 -DNDEBUG
TARGETS = $(BINDIR)/test_functions \
.PHONY: all clean cleanall test1 test2
.SUFFIXES: .c .h
$(BINDIR)/%: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OPTFLAGS) -o $# $<
$(OBJDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OPTFLAGS) -c -o $# $<
all : $(TARGETS)
$(BINDIR)/test_functions: $(OBJDIR)/test_functions.o $(LIBDIR)/libutility.a $(LIBDIR)/libfunctions.a
$(CC) $(CFLAGS) $(INCLUDES) $(FLAGS) $(LDFLAGS) -o $# $< $(LIBUTILITY) $(LIBFUNCTIONS)
$(OBJDIR)/test_functions.o: $(SRCDIR)/test_functions.c $(INCDIR)/utility.h $(LIBDIR)/libutility.a $(INCDIR)/functions.h $(LIBDIR)/libfunctions.a
$(CC) $(CCFLAGS) $(INCLUDES) $(FLAGS) -c -o $# $<
$(LIBDIR)/libutility.a: $(OBJDIR)/utility.o $(INCDIR)/utility.h
$(AR) $(ARFLAGS) $# $<
$(LIBDIR)/libfunctions.a: $(OBJDIR)/functions.o $(INCDIR)/functions.h $(INCDIR)/utility.h
$(AR) $(ARFLAGS) $# $<
clean :
rm -f $(TARGETS)
cleanall : clean
\rm -f *.o *~
the problem is that when I try to compile the project using the makefile, the result is
gcc -std=c99 -Wall -g -I. -L/lib -o bin/test_functions obj/test_functions.o -lutility -lfunctions
/usr/bin/ld: impossibile trovare -lutility
/usr/bin/ld: impossibile trovare -lfunctions
collect2: error: ld returned 1 exit status
make: *** [Makefile:54: bin/test_functions] Error 1
it seems that the compiler cannot find the libraries. what can I do to make it work properly?
You have this:
LIBDIR = ./lib
then in your link line you have this:
LDFLAGS = -L/lib
Notice the difference?
Why don't you just say:
LDFLAGS = -L$(LIBDIR)
to avoid repeating yourself (and getting it wrong)?

GCC symbol lookup error

I try to launch my executable but i got this error:
.dudac/stage/monkey/lib/hello.duda: undefined symbol: DDS_sequence_BoardGlobalParameters_BoardGPSoftState__alloc
Whereas link and compile work fine
Here is my makefile:
# ====================================
# Date : Tue 21, Feb 2017 at 10:15
NAME = hello
CC = /opt/windriver/wrlinux-small/7.0-xxxxxx-3543dr/sysroots/x86_64-wrlinuxsdk-linux/usr/bin/arm-wrs-linux-gnueabi/arm-wrs-linux-gnueabi-gcc
CFLAGS = -g -Wall -DDEBUG -L/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/RTS/armv7at2_vfp_neon.WRlinux7_gcc/lib -ldcpssac -ldcpsisocpp2
#-g -Wall -DDEBUG -fPIC
LDFLAGS =
DEFS =
INCDIR = -I/home/T0181049/.dudac/stage/monkey//include/ -I/home/T0181049/.dudac/stage/monkey//src/include -I/home/T0181049/.dudac/stage/monkey//plugins/duda/src -I/home/T0181049/.dudac/stage/monkey//plugins/duda/ -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/include -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/include/dcps/C/SAC -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/include/sys -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSplice/6.7.1p1/HDE/x86_64.linux/include -I/opt/PrismTech/Vortex_v2/Device/VortexOpenSplice/6.7.1p1/HDE/x86_64.linux/include/sys -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/c++ -I/opt/windriver/wrlinux-small/7.0-xxxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/c++/4.9.1 -I/opt/windriver/wrlinux-small/7.0-xxxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/c++/4.9.1/arm-windriverv7atneon-linux-gnueabi -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/c++/4.9.1/backward -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/armv7at2-vfp-neon-wrs-linux-gnueabi/usr/include/dtc -I/opt/windriver/wrlinux-small/7.0-xxxxxx-3543dr/sysroots/x86_64-wrlinuxsdk-linux/usr/lib/arm-wrs-linux-gnueabi/gcc/arm-wrs-linux-gnueabi/4.9.1/include -I/opt/windriver/wrlinux-small/7.0-xxxxx-3543dr/sysroots/x86_64-wrlinuxsdk-linux/usr/lib/arm-wrs-linux-gnueabi/gcc/arm-wrs-linux-gnueabi/4.9.1/include-fixed
OBJECTS = main.o
_PATH = $(patsubst /%, %, $(CURDIR))
_CC = #/bin/echo -e " [\033[33mCC\033[0m] $#"; $(CC)
_DD = #/bin/echo -e " [\033[32mDD\033[0m] $#"; $(CC)
_CC_QUIET = #/bin/echo -n; $(CC)
all: $(NAME).duda
$(NAME).duda: $(OBJECTS)
$(_DD) $(CFLAGS) $(DEFS) -shared -o $# $^ -lc $(LDFLAGS)
.c.o:
$(_CC) -c $(CFLAGS) $(DEFS) $(INCDIR) -fPIC $<
$(_CC_QUIET) -MM -MP $(CFLAGS) $(DEFS) $(INCDIR) $*.c -o $*.d > /dev/null &2>&1
clean:
rm -rf *.o *.d *~ $(NAME).duda
How do i resolve that ?
Thanks
During linking you have specified libraries that are not in the standard path
Solutions
link statically
Use LD_LIBRARY_PATH
Use LD_PRELOAD
Use rpath
I try to use rpath just like this but got the same error:
CFLAGS = -g -Wall -DDEBUG -L/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/lib -Wl,-rpath,/opt/PrismTech/Vortex_v2/Device/VortexOpenSpliceRTE/6.7.1p2/HDE/armv7at2_vfp_neon.WRlinux7_gcc/lib -ldcpssac -ldcpsisocpp2

Makefile error in C

CC = gcc
OPTS = -W -O3
SRCDIR=./src
OBJDIR=./obj
INCDIR=./inc
BINDIR=./bin
SRCS=$(SRCDIR)/Functions.c \
SRCS=$(SRCDIR)/Function1.c \
SRCS=$(SRCDIR)/Function2.c \
SRCS=$(SRCDIR)/Function3.c \
INCLUDE = $(addprefix -I,$(INCDIR))
OBJS=${SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o}
CFLAGS = $(OPTS) $(INCLUDE) $(DEBUG)
TARGET = $(BINDIR)/ Functions
all: $(TARGET)
$(TARGET): $(OBJS)
${CC} ${CFLAGS} -o $# $(OBJS)
$(OBJS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
$(CC) $(CFLAGS) -c $< -o $#
Functions.c has a main inside that calls Function1(); .....Function3();
I'm trying to add the debug flag based on #ifndef
How to add a debug flag and set the Debug compiler directive inside this Makefile? I have #ifndef NDEBUG...#endif from Functions.h
TARGET = $(BINDIR)/ Functions
should be ?
TARGET = $(BINDIR)/Functions
CC=gcc
OPTS=-W -O3
DEBUG=
RM=rm
SRCDIR=src
OBJDIR=obj
INCDIR=inc
BINDIR=bin
SRCS=$(SRCDIR)/*.c
INCLUDE=$(addprefix -I, $(INCDIR))
OBJS=obj/Functions.o obj/Function1.o obj/Function2.o obj/Function3.o
CFLAGS = $(OPTS) $(INCLUDE) $(DEBUG)
TARGET = $(BINDIR)/Functions
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $# $(OBJS)
$(OBJS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
$(CC) $(CFLAGS) -c $< -o $#
clean:
$(RM) -f $(TARGET) $(OBJDIR)/*.o
I couldn't get your OBJS expansion working, but the adove works on my end.

Any Missing code in my Make file

i wrote a makefile with some commands.after running command make got a errors undefined reference to sqrtand undefined reference to exp
my make file :
CFILES = smLe.c iniTra.c le.c res.c uti.c smClass.c ini.c clas.c
OBJECTS = smLe.o iniTra.o le.o res.o uti.o smClass.o ini.o clas.o
OBJECTS1 = smLe.o iniTra.o le.o res.o uti.o
OBJECTS2 = smClass.o ini.o clas.o
CFLAGS = -O -lm
CC = gcc
.c .o:
$(CC) $(CFLAGS) $(OBJECTS) -c $<
p1: $(OBJECTS1)
$(CC) $(CFLAGS) $(OBJECTS1) -o $#
p2: $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS2) -o $#
smLe.o: iniTra.h le.h res.h
iniTra.o: iniTra.h
le.o: iniTra.h uti.h le.h
res.o: iniTra.h le.h res.h
smClass.o: ini.h clas.h
ini.o: ini.h clas.h
clas.o: ini.h clas.h
Is it any thing missing in my make file.why this error occur in this make file code?please help me ..thanks
-lm is an LDLIB, you pass it to the linker
CFLAGS = -O
LDLIBS = -lm
CC = gcc
.c .o:
$(CC) $(CFLAGS) $(OBJECTS) -c $<
p1: $(OBJECTS1)
$(CC) $(OBJECTS1) -o $# $(LDLIBS)
Libraries have to appear after object files that call what is in them on the command line.

Resources