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.
Implicit rules tell make how to use customary techniques so that you do not have to specify them in detail when you want to use them. For example, there is an implicit rule for C compilation. File names determine which implicit rules are run. For example, C compilation typically takes a . c file and makes a .o file.
Implicit rules tend to be those that are accepted as cultural standards for proper relationship conduct (e.g., monogamy and secrets kept private).
The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.
CFLAGS and CXXFLAGS are either the name of environment variables or of Makefile variables that can be set to specify additional switches to be passed to a compiler in the process of building computer software.
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
There supposed to be an implicit rule for prog or I am missing something?
There is no implicit rule. make
cannot know how to build prog
because it doesn’t know that prog
is supposed to be an executable. make
only uses the file name as a pattern to deduce the build rule. prog
is a generic file name without extension so make
doesn’t know how to treat it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With