简体   繁体   English

Makefile用于编译c ++程序

[英]Makefile for compiling c++ program

I'm using g++ on linux and writing multiple cpp programs. 我在linux上使用g ++并编写多个cpp程序。 For ex. 对于前者 I have ex1.cpp, ex2.cpp, bot.cpp. 我有ex1.cpp,ex2.cpp,bot.cpp。

All these cpp programs are inside the same folder. 所有这些cpp程序都在同一个文件夹中。 I want to use make to compile individual programs as in "make ex1" should compile ex1.cpp and not the other cpp files. 我想使用make来编译单个程序,因为“make ex1”应该编译ex1.cpp而不是其他cpp文件。 "make bot" should only compile bot.cpp “make bot”应该只编译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. 我不想为我在文件夹中创建的每个cpp程序在MakeFile中编写目标和依赖项,我不想使用* .cpp一次编译所有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 . 只需在命令行中运行make ex1 ,它将使用命令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). 如果您想动态包含库/共享对象或添加其他标志,那么您应该创建一个make文件或遵循Lukasz建议的过程(虽然太复杂了)。

You don't need to write a makefile. 您不需要编写makefile。 make ex1 should already do what you want. make ex1应该已经做你想要的了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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