Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this makefile make objectfiles?

Tags:

c++

c

makefile

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??

like image 289
sonagi Avatar asked Jun 01 '26 11:06

sonagi


1 Answers

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).

like image 128
firda Avatar answered Jun 03 '26 00:06

firda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!