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.
Related
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.
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)?
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.
In makefile below I'm trying to compile one directory, but make against my will is trying to do it in wrong order. It's starting from compiling main.o and later it goes straight to compiling console. I have no idea why it's not trying to solve all dependences before it. I just get:
user#ubuntu:~/Dokumenty/Sysopy/cw1/zad3b$ make
gcc -c -Wall -I ./src/include src/main.c
gcc -o macierze main.o
main.o: In function `main':
main.c:(.text+0x28): undefined reference to `wczytaj'
main.c:(.text+0x31): undefined reference to `wczytaj'
main.c:(.text+0x7e): undefined reference to `suma'
...
collect2: ld returned 1 exit status make: *** [console] Błąd 1
Here's a makefile:
############ makra #############
CC = gcc
CFLAGS = -Wall -I ./src/include
LFLAGS =
VPATH = ./src/operacje: ./src/we_wy: ./src/reszta
########### pliki ##############
SRC_CONSOLE = wczytaj_konsola.c wypisz_konsola.c
SRC_FILE = wczytaj_plik.c wypisz_plik.c pliki.c
SRC_FUNCTION = suma.c roznica.c iloczyn.c macierz.c
HEADERS = suma.h roznica.h iloczyn.h wypisz.h wczytaj.h macierz.h
OBJ_SONSOLE = $(SRC_CONSOLE:.c=.o)
OBJ_FILE = $(SRC_FILE:.c=.o)
OBJ_FUNCTION = $(SRC_FUNCTIONS:.c=.o)
console: $(OBJ_CONSOLE) $(OBJ_FUNCTION) main.o
$(CC) $(LFLAGS) -o macierze $^
file: $(OBJ_FILE) $(OBJ_FUNCTION) main.o
$(CC) $(LFLAGS) -o macierze $^
console_logs: $(OBJ_CONSOLE) $(OBJ_FUNCTION) main.o
$(CC) $(LFLAGS) -o macierze $^
file_logs: $(OBJ_FILE) $(OBJ_FUNCTION) main.o
$(CC) $(LFLAGS) -o macierze $^
############# objekty ##############
main.o:
$(CC) -c $(CFLAGS) src/main.c
$(OBJ_CONSOLE): $(SRC_CONSOLE)
$(CC) -c $(CFLAGS) $^
$(OBJ_FILE): $(SRC_FILE)
$(CC) -c $(CFLAGS) $^
$(OBJ_FUNCTION): $(SRC_FUNCTION)
$(CC) -c $(CFLAGS) $^
%logs : LFLAGS += -D KOMUNIKATY
file% : LFLAGS += -D PLIKI
./PHONY: clean
clean:
rm -f *.o
rm -f ./src/include/*.gch
rm -f macierze
you write : OBJ_FUNCTION = $(SRC_FUNCTIONS:.c=.o)
instead of : OBJ_FUNCTION = $(SRC_FUNCTION:.c=.o)
and
OBJ_SONSOLE = $(SRC_CONSOLE:.c=.o)
instead of OBJ_CONSOLE = $(SRC_CONSOLE:.c=.o)
I'm trying to do a simple sprintf like this:
snprintf(dest, maxlen_dest, "%'lu", along);
Note the quote: ' in the string.
This means (according to the manual):
' For decimal conversion (i, d, u, f, F, g, G) the output is to be
grouped with thousands’ grouping characters if the locale
information indicates any. Note that many versions of gcc(1) cannot
parse this option and will issue a warning. SUSv2 does not include
%'F.
So when I compile, I get this warning:
../common/utils.c:29: warning: ISO C does not support the ''' printf flag
../common/utils.c:29: warning: ISO C does not support the ''' printf flag
So, my question is: is there a function that "supports the ''' printf flag" I could use?
I want my output to be like:
51 254 234
1 424 000
...
...
...
Here's my Makefile (in case there's some options I should modify):
CC = gcc
# -g = all debug options
CFLAGS = -g -I../common -W -Wall -pedantic -std=c99
OBJECTS = o/main.o o/opts.o o/verif_liens_bidirectionnels.o o/ecriture.o o/plugin_utils.o o/plugin_algo_groupes.o ../common/o/cell.o ../common/o/utils.o ../common/o/verif.o ../common/o/lecture.o
gen : $(OBJECTS)
$(CC) -o gen $(OBJECTS) $(CFLAGS) -lm -lgd -lz
o/main.o : main.c
$(CC) -o $# -c main.c $(CFLAGS)
o/opts.o : opts.c opts.h
$(CC) -o $# -c opts.c $(CFLAGS)
o/verif_liens_bidirectionnels.o : verif_liens_bidirectionnels.c verif_liens_bidirectionnels.h
$(CC) -o $# -c verif_liens_bidirectionnels.c $(CFLAGS)
o/ecriture.o : ecriture.c ecriture.h
$(CC) -o $# -c ecriture.c $(CFLAGS)
o/plugin_utils.o : plugin_utils.c plugin_utils.h
$(CC) -o $# -c plugin_utils.c $(CFLAGS)
o/plugin_algo_groupes.o : plugin_algo_groupes.c plugin_algo_groupes.h
$(CC) -o $# -c plugin_algo_groupes.c $(CFLAGS)
../common/o/cell.o : ../common/cell.c ../common/cell.h
$(CC) -o $# -c ../common/cell.c $(CFLAGS)
../common/o/utils.o : ../common/utils.c ../common/utils.h
$(CC) -o $# -c ../common/utils.c $(CFLAGS)
../common/o/verif.o : ../common/verif.c ../common/verif.h
$(CC) -o $# -c ../common/verif.c $(CFLAGS)
../common/o/lecture.o : ../common/lecture.c ../common/lecture.h
$(CC) -o $# -c ../common/lecture.c $(CFLAGS)
clean :
rm $(OBJECTS)
You could add
../common/o/utils.o : CFLAGS := $(filter-out -pedantic,$(CFLAGS))
to the makefile, which should suppress the warning for the specific file.
If you don't want to rely on non-standard libc extensions, you'll have to implement the functionality yourself...