# this is a comment
OBJS = student.o teacher.o class.o
CC = g++
DEBUG = -g
CFLAGS = -Wall –c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
myproj.exe : $(OBJS)
$(CC) $(LFLAGS) $(OBJS) –o myproj.exe
student.o : student.h student.cpp
$(CC) $(CFLAGS) student.cpp
teacher.o : teacher.h teacher.cpp
$(CC) $(CFLAGS) teacher.cpp
class.o : class.h student.h teacher.h class.cpp
$(CC) $(CFLAGS) class.cpp
can someonte tell me why does CFLAGS and LFALGS have the $(DEBUG) behind?
So you don't have to write it every time you compile or link.
Lets take these two lines from your file:
student.o : student.h student.cpp
$(CC) $(CFLAGS) student.cpp
If you didn't have $(DEBUG) in $(CFLAGS) you would have to write
student.o : student.h student.cpp
$(CC) $(CFLAGS) $(DEBUG) student.cpp
And do it for every rule and command where you want to use $(DEBUG).
Related
I am trying to reorganize my project to put all generated object file in a particular "obj" folder, when I just handle c files I used below makefile and it works fine(I put src1.c in src1 dir,src2.c in src2 dir and src3.c in src3 dir).
vpath %.c src
vpath %.c src2
vpath %.c src3
D_OBJ = obj
CC = gcc
SRC_C = src1.c src2.c src3.c
OBJ_C = $(addprefix $(D_OBJ)/,$(patsubst %.c,%.o,$(notdir $(SRC_C))))
DEP = $(addprefix $(D_OBJ)/, $(patsubst %.c,%.d,$(notdir $(SRC_C))))
.PHONY: all
all:$(OBJ_C)
$(D_OBJ)/%.o:%.c
$(CC) -c -Wall $< -o $# -MMD -MF $(D_OBJ)\\$*.d -MP
-include $(DEP)
currently, I have asm and S files in src1,src2 and src3 called file1.asm file2.asm file3.asm and sfile1.s,sfile2.s and sfile3.s.
how can I put all of these files generated .o into obj folder?
I tried below but doesn't work
vpath %.c src
vpath %.c src2
vpath %.c src3
vpath %.s src
vpath %.s src2
vpath %.s src3
vpath %.asm src
vpath %.asm src2
vpath %.asm src3
D_OBJ = obj
CC = gcc
SRC_C = src1.c src2.c src3.c
SRC_ASM = file1.asm file2.asm file3.asm
SRC_S = sfile1.s sfile2.s sfile3.s
OBJ_C = $(addprefix $(D_OBJ)/,$(patsubst %.c,%.o,$(notdir $(SRC_C))))
OBJ_S = $(addprefix $(D_OBJ)/,$(patsubst %.s,%.o,$(notdir $(SRC_S))))
OBJ_ASM = $(addprefix $(D_OBJ)/,$(patsubst %.asm,%.o,$(notdir $(SRC_ASM))))
DEP = $(addprefix $(D_OBJ)/, $(patsubst %.c,%.d,$(notdir $(SRC_C))))
DEP += $(addprefix $(D_OBJ)/, $(patsubst %.s,%.d,$(notdir $(SRC_S))))
DEP += $(addprefix $(D_OBJ)/, $(patsubst %.asm,%.d,$(notdir $(SRC_ASM))))
.PHONY: all
all:$(OBJ_C) $(OBJ_ASM) $(OBJ_S)
$(D_OBJ)/%.o:%.c
$(CC) -c -Wall $< -o $# -MMD -MF $(D_OBJ)\\$*.d -MP
$(D_OBJ)/%.o:%.asm
$(CC) -c -Wall $< -o $# -MMD -MF $(D_OBJ)\\$*.d -MP
$(D_OBJ)/%.o:%.s
$(CC) -c -Wall $< -o $# -MMD -MF $(D_OBJ)\\$*.d -MP
-include $(DEP)
got error :make: *** No rule to make target obj/file1.o', needed byall'. Stop.
Thank you in advance!
Makefile:
CC = gcc
CFLAGS = -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_NO_XSLT=1 -DXMLSEC_NO_XKMS=1 -I/usr/include/libxml2 -DXMLSEC_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_CRYPTO=\"openssl\" -DUNIX_SOCKETS -DXML_SECURITY -DDEBUG
LDFLAGS= -lcrypto -I/usr/include/libxml2 -lxml2 -I/usr/local/include/xmlsec1 -lxmlsec1
$(CC) $(CFLAGS) $(LDFLAGS) src/aadhaar.c src/uid_auth.c -o AuthClient
I'm getting this error: error: commands commence before first target
You seem to miss the target definition:
AuthClient : src/aadhaar.c src/uid_auth.c
$(CC) $(CFLAGS) $(LDFLAGS) src/aadhaar.c src/uid_auth.c -o $#
I have the following Makefile. Whenever I run make, I get the following error.
ifort: error #10236: File not found: 'mkl_matrix_multiply.o'
I have been trying to figure this out for a while now with no luck.
C = icc
FC = ifort
LD = ifort
OPT = -Ofast -vec_report6 -simd -xhost -debug -traceback -ftrapuv
OP = -Ofast -vec_report6 -simd -xhost
LINK = -L$(MKLROOT)/lib/intel64 $(MKLROOT)/lib/intel64/libmkl_blas95_ilp64.a -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -lpthread -lm
INCLUDE = -openmp -i8 -I$(MKLROOT)/include/intel64/ilp64 -I$(MKLROOT)/include
mkl_matrix_multiply.exe: mkl_matrix_multiply.o timing.o
$(LD) -o mkl_matrix_multiply.exe mkl_matrix_multiply.o timing.o
mkl_matrix_multiply.o: mkl_matrix_multiply.f90
$(FC) $(INCLUDE) $(LINK) mkl_matrix_multiply.f90
timing.o: timing.c
$(CC) $(OP) -c timing.c
dummy.o: dummy.c
$(CC) $(OP) -c dummy.c
clean:
rm -f *.o matrix_multiply.exe
Any help would be greatly appreciated.
Seems like you are missing -c in mkl_matrix_multiply.o rule.
Modify your makefile as
C = icc
FC = ifort
LD = ifort
OPT = -Ofast -vec_report6 -simd -xhost -debug -traceback -ftrapuv
OP = -Ofast -vec_report6 -simd -xhost
LINK = -L$(MKLROOT)/lib/intel64 $(MKLROOT)/lib/intel64/libmkl_blas95_ilp64.a -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -lpthread -lm
INCLUDE = -openmp -i8 -I$(MKLROOT)/include/intel64/ilp64 -I$(MKLROOT)/include
mkl_matrix_multiply.exe: mkl_matrix_multiply.o timing.o
$(LD) -o mkl_matrix_multiply.exe mkl_matrix_multiply.o timing.o
mkl_matrix_multiply.o: mkl_matrix_multiply.f90
$(FC) -c $(INCLUDE) $(LINK) mkl_matrix_multiply.f90
timing.o: timing.c
$(CC) $(OP) -c timing.c
dummy.o: dummy.c
$(CC) $(OP) -c dummy.c
clean:
rm -f *.o matrix_multiply.exe
I am adapting my Makefile to look into 4 directories, rather than 2 (it had one for source files and one for header files, but I've added a new folder for common source and include). I have something like follows:
CC = g++
FLAGS = -g -c
BUILDDIR = build
INCLUDEDIR = -Icode/inc -I../common/code/inc -I/usr/include/libxml2
SOURCEDIR = code/src ../common/code/src
SOURCES = $(wildcard $(SOURCEDIR)/*.cpp)
OBJECTS = $(patsubst $(SOURCEDIR)/%.cpp,$(BUILDDIR)/%.o,$(SOURCES))
EXECUTABLE = Exec
all: $(BUILDDIR)/$(EXECUTABLE)
$(BUILDDIR)/$(EXECUTABLE): $(OBJECTS)
$(CC) $^ -o $# -lpthread -lxml2
$(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp
$(CC) $(FLAGS) $< $(INCLUDEDIR) -o $# -Wno-write-strings
I tried to add one entry to INCLUDEDIR as follows:
-I../common/code/inc
And added ../common/code/src to SOURCEDIR:
SOURCEDIR = code/src ../common/code/src
This is not currently working and I am wondering how to fix it please. I am getting the error:
Makefile:27: target `code/src' doesn't match the target pattern
but I cannot find how to fix it so far. Any help would be appreciated.
EDIT: After following MadScientist response below, I am getting the following output:
g++ -c -o code/src/Client.o code/src/Client.cpp
code/src/Client.cpp:1:20: fatal error: Client.h: No such file or directory
compilation terminated.
make: *** [code/src/Client.o] Error 1
Updated Makefile:
SOURCEDIR = code/src ../common/code/src
SOURCES = $(wildcard $(addsuffix /*.cpp,$(SOURCEDIR)))
OBJECTS = $(SOURCES:%.cpp=%.o)
$(BUILDDIR)/$(EXECUTABLE): $(OBJECTS)
$(CC) $^ -o $# -lpthread -lxml2
$(BUILDDIR)/%.o : ../common/code/src/%.cpp
$(CC) $(FLAGS) $< $(INCLUDEDIR) -o $# -Wno-write-strings
$(BUILDDIR)/%.o : code/src/%.cpp
$(CC) $(FLAGS) $< $(INCLUDEDIR) -o $# -Wno-write-strings
PS:
I was able to fix it using the following:
SOURCEDIR = code/src ../common/code/src
SOURCES = $(wildcard $(addsuffix /*.cpp,$(SOURCEDIR)))
TEMP_OBJ = $(SOURCES:%.cpp=%.o)
NOT_DIR = $(notdir $(TEMP_OBJ))
OBJECTS = $(addprefix $(BUILDDIR)/, $(NOT_DIR))
Sure, because now your static pattern rule expands to:
$(OBJECTS): build/%.o : code/src ../common/code/src/%.cpp
which is illegal syntax. If you avoid using static pattern rules, and instead use pattern rules, then it will just work. Replace your single static pattern rule with two pattern rules:
$(BUILDDIR)/%.o : code/src/%.cpp
$(CC) $(FLAGS) $< $(INCLUDEDIR) -o $# -Wno-write-strings
$(BUILDDIR)/%.o : ../common/code/src/%.cpp
$(CC) $(FLAGS) $< $(INCLUDEDIR) -o $# -Wno-write-strings
EDIT: you also need to change other uses of SOURCEDIR:
SOURCES = $(wildcard $(addsuffix /*.cpp,$(SOURCEDIR))
OBJECTS = $(patsubst %.cpp,$(BUILDDIR)/%.o,$(notdir $(SOURCES)))
i'm trying to use GCC (linux) with a makefile to compile my project.
I get the following error :
% make map
make: *** No rule to make target `map'. Stop.
Does any one have any idea what could that be ?
This is the makefile:
CC = gcc
DEBUG_FLAG = -g
COMP_FLAG = -std=c99 -Wall -Werror -pedantic-errors -DNDEBUG
LIB = -L. -lmtm
MAP_OBJS = map.o
USER_OBJS = user.o
NETWORK_OBJS = network.o
ISOCIAL_OBJS = network.o user.o Isocial.o
PARSING_OBJS = user.o network.o map.o Isocial.o mtm_isocial.o
PARSING = parsing
MAP_TEST = map_example_test
ISOCIAL_TEST = Isocial_test
NETWORK_TEST = network_test
USER_TEST = user_test
TEST_PATH = ./tests/
$(PARSING) : $(PARSING_OBJS)
$(CC) $(DEBUG_FLAG) $(PARSING_OBJS) $(LIB) -o $#
$(MAP_TEST) : $(MAP_OBJS)
$(CC) $(DEBUG_FLAG) $(MAP_OBJS) $(LIB) -o $#
$(ISOCIAL_TEST) : $(ISOCIAL_OBJS)
$(CC) $(DEBUG_FLAG) $(ISOCIAL_OBJS) $(LIB) -o $#
$(USER_TEST) : &(USER_OBJS)
$(CC) $(DEBUG_FLAG) $(USER_OBJS) $(LIB) -o $#
$(NETWORK_TEST) : $(NETWORK_OBJS)
$(CC) $(DEBUG_FLAG) $(MAP_OBJS) $(LIB) -o $#
map.o: map.c map.h mtm_ex2.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c
isocial.o: Isocial.c user.h mtm_ex2.h set.h network.h list.h Isocial.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c
parsing.o: parsing.c Isocial.h parsing.h mtm_ex2.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c
network.o: network.c network.h set.h mtm_ex2.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c
user.o:user.c user.h set.h mtm_ex2.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c
user_test.o: user_test.c ../test_utilities.h ../user.h ../mtm_ex2.h \
../set.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $(TEST_PATH)$*.c
Isocial_test.o: Isocial_test.c ../test_utilities.h ../Isocial.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c $(TEST_PATH)$*.c
map_example_test.o: map_example_test.c ../test_utilities.h ../map.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c $(TEST_PATH)$*.c
network_test.o: network_test.c ../test_utilities.h ../network.h ../set.h \
../mtm_ex2.h
$(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c $(TEST_PATH)$*.c
EXEC = $(PARSING) $(MAP_TEST) $(ISOCIAL_TEST $(USER_TEST) $(NETWORK_TEST)
tests : $(MAP_TEST) $(ISOCIAL_TEST) $(USER_TEST) $(NETWORK_TEST)
isocial : $(PARSING)
clean:
rm -f $(PARSING_OBJS) $(EXEC)
The error message is correct: I see a target for map.o, but there's no target for map, which would look like
map: map.o
something something something
There are targets for programs named parsing, map_example_test, network_test, and Isocial_test; perhaps you want to build one of those?
If you just typed make, it would build the program parsing, as that's the first target in the file.