GNU make simple usage example

Requirement: Write a Makefile to compile and link the two .cpp files to generate the executable file main. The cpp file can be found at the following link:

https://blog.csdn.net/Reading8/article/details/132630694icon-default.png?t=N7T8https://blog.csdn.net/Reading8/article/details/132630694 a>There are two ways to compile and link this example to generate an executable file.

One is the command line method

(base) ganning@ganning:~/Xproject/makefilesteel$ g++ main.cpp myfunctions.cpp -o main
(base) ganning@ganning:~/Xproject/makefilesteel$ ls
main  main.cpp  myfunctions.cpp  myfunctions.h
(base) ganning@ganning:~/Xproject/makefilesteel$ ./main
Hello world!

Another way to write a Makefile

main: main.o myfunctions.o
	g++ main.o myfunctions.o -o main


main.o: main.cpp
	g++ -c main.cpp

myfunctions.o: myfunctions.cpp myfunctions.h
	g++ -c myfunctions.cpp
	
clean:
	rm *.o 

Note that in < g++ -c main.cpp > -c means not to generate an executable file, <rm *.o> means to delete all .o suffix files. You can use make clean to call this operation. You can see the Makefile The basic writing specification is

target: materials
    Operation

Result verification

(base) ganning@ganning:~/Xproject/makefilesteel$ make
g++ -c main.cpp
g++ -c myfunctions.cpp
g++ main.o myfunctions.o -o main
(base) ganning@ganning:~/Xproject/makefilesteel$ ls
main  main.cpp  main.o  Makefile  myfunctions.cpp  myfunctions.h  myfunctions.o
(base) ganning@ganning:~/Xproject/makefilesteel$ ./main
Hello world!
(base) ganning@ganning:~/Xproject/makefilesteel$ make clean
rm *.o 
(base) ganning@ganning:~/Xproject/makefilesteel$ ls
main  main.cpp  Makefile  myfunctions.cpp  myfunctions.h

Guess you like

Origin blog.csdn.net/Reading8/article/details/132631057
Recommended