Is there a trick involving makefiles or IDEs?
This "trick" called linking - loading all compiled modules and libraries together into executable file. In your case you can do that manually:
g++ -c a.cpp -o a.o // compiling a.cpp and producing object file a.o
g++ -c main.cpp -o main.o // compiling main.cpp and producing object file main.o
g++ main.o a.o -o myprog // linking all object files together with system libraries and producing executable myprog
For different compilers commands may look different, but process is the same. Of course you do not want to type all of that again and again so you would want to automate that. That what IDE or makefile does for you, there are no tricks or magic involved.