I am working my way through a make tutorial. Very simple test projects I am trying to build has only 3 files: ./src/main.cpp ./src/implementation.cpp and ./include/header.hpp This is the make file.
VPATH = src include
CPPFLAGS = -I include
main: main.o implementation.o
main.o: header.hpp
implementation.o: header.hpp
When called without any arguments make builds only object files, but doesn't link the executable file. There supposed to be an implicit rule for prog or I am missing something? I really need someone to point me into right direction.
Thanks.
I made the first target name the same as the prefix of the source file. Now make calls cc to link object files.
g++ -I include -c -o main.o src/main.cpp
g++ -I include -c -o implementation.o src/implementation.cpp
cc main.o implementation.o -o main
For some reason linking with g++ works, but linking with cc doesn't.
I could specify the rule explicitly, but want to learn how to use implicit rules.
According to the make manual, you can use the implicit linking rule with multiple objects if one of these matches the executable name, eg:
VPATH = src include
CPPFLAGS = -I include
main: implementation.o
main.o: header.hpp
implementation.o: header.hpp
This will build an executable named main from both main.o and implementation.o.
Note however that the builtin implicit rule uses the C compiler for linking, which will not link against the C++ std library by default, you will need to add the flag -lstdc++
to LDLIBS explicitly