0

I have a C++ project with this file structure:

enter image description here

include files:

enter image description here

src files:

enter image description here 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?

casper
  • 35
  • 9
  • 2
    Remember that paths are *relative*. There is no `agenda.cpp` file in the same directory as `makefile`. There is on the other hand a `src/agenda.cpp` file. The same problem with all your files. I think you need to spend a little more time with [the `make` documentation](https://www.gnu.org/software/make/manual/make.html). And when you do that I also recommend you learn more about *implicit rules*, which means you don't have to list the commands to build all object files. – Some programmer dude Nov 23 '18 at 11:37
  • You mean like wherever makefile is, it is a starting point ? and before each .cpp and .hpp files I should add the path like src/file_name.cpp ?@Someprogrammerdude – casper Nov 23 '18 at 11:42
  • Sure, just the file structure is the requirement for this assignment idk @Someprogrammerdude – casper Nov 23 '18 at 11:46
  • You could also use `vpath %.hpp src` and `vpath %.cpp src` – Botje Nov 23 '18 at 11:56
  • @Botje where i should type that ? – casper Nov 23 '18 at 11:58
  • Put it at the top of your Makefile. (assuming GNU make here) – Botje Nov 23 '18 at 11:59
  • Unrelated, you could cut down on the repetition by making use of the built-in [.cpp->.o](https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html#Catalogue-of-Rules) rule – Botje Nov 23 '18 at 12:01
  • vpath %.hpp src and vpath %.cpp src didn't work @Botje – casper Nov 23 '18 at 12:15
  • 1
    vpath is an advanced Makefile use and often done wrong, don't start with that. For the header files pass "-I include" to g++ and for the cpp files use the full path, e.g. src/User.cpp. You should also make an agenda.o and add a separat linker stage. Once that works look into pattern rules: %.o: %.cpp for example. – Goswin von Brederlow Nov 23 '18 at 15:29

1 Answers1

0

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
casper
  • 35
  • 9