Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make: implicit rule to link c++ project

Tags:

c++

makefile

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.

like image 547
pic11 Avatar asked Mar 29 '11 14:03

pic11


People also ask

What is implicit rule in makefile?

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.

What is an implicit rule?

Implicit rules tend to be those that are accepted as cultural standards for proper relationship conduct (e.g., monogamy and secrets kept private).

What is $@ in makefile?

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.

What is Cflags in makefile?

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.


2 Answers

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

like image 51
Hasturkun Avatar answered Sep 19 '22 19:09

Hasturkun


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.

like image 28
Konrad Rudolph Avatar answered Sep 18 '22 19:09

Konrad Rudolph