简体   繁体   中英

Makefile for compiling c++ program

I'm using g++ on linux and writing multiple cpp programs. For ex. I have ex1.cpp, ex2.cpp, bot.cpp.

All these cpp programs are inside the same folder. I want to use make to compile individual programs as in "make ex1" should compile ex1.cpp and not the other cpp files. "make bot" should only compile bot.cpp

The compilation command I intend to execute is :

g++ -o bot bot.cpp

I don't want to write target and dependency in MakeFile for every cpp program I create in the folder and I don't want to compile all cpp's at one go using *.cpp.

Is it possible first of all to achieve this ? Is yes please suggest a solution

A simple solution to your question :

Just run make ex1 in the command line and it will compile using the command g++ ex1.cpp -o ex1 .

If you want to dynamically include libraries/shared objects or add additional flags then you should create a make file or follow the procedure suggested by Lukasz (Too complicated though).

You don't need to write a makefile. make ex1 should already do what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM