The command
g++ main.cpp -o main
doesn't tell the linker where to find the compiled code for
human_functions
. Although you've included
human.h
in your main.cpp, that only includes the class definitions to main.cpp, it doesn't say where the actual code is for the class member functions. Depending on how you want to compile your program, you can either compile human_functions.cpp separately, and then link when you compile main.cpp, or compile both human_functions.cpp and main.cpp at the same time.
$ g++ -Wall -Wextra -g -c human_functions.cpp
$ g++ -Wall -Wextra -g main.cpp human_functions.o -o main
$ g++ -Wall -Wextra -g main.cpp human_functions.cpp -o main
$ g++ -Wall -Wextra -g -c main.cpp
$ g++ -Wall -Wextra -g -c human_functions.cpp
$ g++ -g main.o human_functions.o -o main
Note that I've added warning flags to the compiler invocation. I almost always use these two flags, and they will flag most of the useful warnings for you. If you get any warnings, you should take a look at what the compiler thinks might be an issue and almost always, its good practice to fix the code to compile cleanly.
The -g flag is use to add debugging information to the object code. This will aid greatly when the time comes to debug your code.
Lastly, consider using
make
to compile your code, if you're not using an IDE. A simple Makefile for this project might look like
CXXFLAGS = -Wall -Wextra -g
main: main.o human_functions.o
$(CXX) -g main.o human_functions.o -o main
You can now say
make main
, and make will create the .o files and link them together. As a bonus, make knows if a .cpp file is newer than the .o file, so will only compile those modules that need it. This is a Good Thing if you have many modules, and compiling might take many minutes.
Be aware that the third line of the file must start with a tab!