I have a C++ project with this file structure:
include files:
src files:
In order to compile it and run I'm trying to create simple Makefile. After going through some tutorials, that what i got so far:
vpath %.hpp include
vpath %.cpp src
agenda: agenda.cpp User.o Date.o Meeting.o Storage.o AgendaService.o AgendaUI.o
g++ agenda.cpp User.o Date.o Meeting.o Storage.o AgendaService.o AgendaUI.o -o agenda
User.o:User.hpp User.cpp
g++ -c -std=c++11 User.cpp
Date.o:Date.hpp Date.cpp
g++ -c -std=c++11 Date.cpp
Meeting.o:Meeting.hpp Meeting.cpp
g++ -c -std=c++11 Meeting.cpp
Storage.o:Storage.hpp Storage.cpp
g++ -c -std=c++11 Storage.cpp
AgendaService.o:AgendaService.hpp AgendaService.cpp
g++ -c -std=c++11 AgendaService.cpp
AgendaUI.o:AgendaUI.hpp AgendaUI.cpp
g++ -c -std=c++11 AgendaUI.cpp
clean:
rm User.o Date.o Meeting.o Storage.o AgendaService.o AgendaUI.o
And by the way, the main function here is agenda.cpp
file. So by executing the make
command I'm getting this error :
make: *** No rule to make target 'agenda.cpp', needed by 'agenda'. Stop.
My guess is it can't find the path to agenda.cpp otherwise it wouldn't ask to make a rule. Anyway not sure, hope someone could explain.
EDIT.0:
I have edited makefile by adding vpath, but still get the error(new) :
g++ -c -std=c++11 User.cpp
g++: error: User.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
make: *** [makefile:9: User.o] Error 1
Seems like this time it found agenda.cpp
and User.hpp
but can't find User.cpp
. Really would appreciate any clue, was working on it for a long time.
EDIT.1:
#VPATH = src:include
#CPPFLAGS = -I include
#vpath %.hpp include
#vpath %.cpp src
bin/agenda: build/User.o build/Date.o build/Meeting.o build/Storage.o build/AgendaService.o build/AgendaUI.o
@mkdir -p bin
g++ -std=c++11 -w -I./include $^ -o $@
build/%.o: src/%.cpp
@mkdir -p build
g++ -std=c++11 -w -I./include -c -o $@ $<
clean:
@rm -rf build
@rm -rf bin
After spending some time on my Makefile, that is the final answer,it compiles fine all *.cpp files, stores obj file in build folder,no problem,except agenda.cpp(main-file),i didn't get my executable file. But got this error:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [makefile:8: bin/agenda] Error 1
How Could i fix this?
Alright i made this Makefile
and it compiles and runs just fine, just post it here if someone needs that as a reference.
The study path that i used is:
GNU tutorials
(.text+0x20): undefined reference to `main' and undefined reference to function
C Linking Error: undefined reference to 'main'
CC := g++
FLAGS := -std=c++11 -w
BIN_DIR := bin
INC_DIR := include
SRC_DIR := src
INCLUDE := -I./$(INC_DIR)
BUILD_DIR := build
$(BIN_DIR)/agenda: $(BUILD_DIR)/User.o $(BUILD_DIR)/Date.o $(BUILD_DIR)/Meeting.o $(BUILD_DIR)/Storage.o $(BUILD_DIR)/AgendaService.o $(BUILD_DIR)/AgendaUI.o $(BUILD_DIR)/agenda.o
@mkdir -p $(BIN_DIR)
$(CC) $(FLAGS) $(INCLUDE) $^ -o $@
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
@mkdir -p $(BUILD_DIR)
$(CC) $(FLAGS) $(INCLUDE) -c -o $@ $<
clean:
@rm -rf build
@rm -rf bin
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.