sonagi
sonagi

Reputation: 39

How does this makefile make objectfiles?

I hava a just simple question. I made a just simple Makefile like this

OBJ = main.o Str.o Out.o
CC = g++
TAR = main

$(TAR):$(OBJ) Header.h
    $(CC) -o $(TAR) $(OBJ)

clean:
    rm -f $(OBJ) $(TAR)

I have just Str.cpp, Out.cpp, main.cpp in my current directory. Even if I miss the process which converts source file into objectfile (like g++ -c Str.cpp), it works well.

I'm curious why this Makefile works. Does it make objectfiles automatically??

Upvotes: 3

Views: 74

Answers (1)

firda
firda

Reputation: 3338

10.2 Catalogue of Built-In Rules

Compiling C++ programs

n.o is made automatically from n.cc, n.cpp, or n.C with a recipe of the form ‘$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’. We encourage you to use the suffix ‘.cc’ for C++ source files instead of ‘.C’.

The answer is yes, it creates object files in the same directory for each source file (and then links them all with your rule to final executable).

Upvotes: 3

Related Questions