jeea
jeea

Reputation: 163

Compiling c++ from command line using nmake or another tool like batch script

I have multiple c++ files in a directory, but at a moment, I need to compile only one of them. I am using Windows 10 with mingw-w64.

Currently I am individually writing command like g++ abc.cpp -o abc plus some flags, but I need to automate this, for example, writing only some command like compile abc.cpp.

I am looking through nmake, but there doesn't seem any simple tutorial and I am stuck for some time on this.

Is there any easy tool for this job, or how can it be done in nmake. I am using VSCode

Edit

In comment of KamilCuk I come to know about batch script which can also be used

Upvotes: 1

Views: 614

Answers (1)

123p10
123p10

Reputation: 166

The best, simplest method for this is to use a Makefile. I believe mingw has make capabilities.

All you have to do is simply create a file called "Makefile" with no extensions.

In the Makefile add the following lines

CXX = g++
CXXFLAGS = PUT YOUR FLAGS HERE
abc: abc.cpp
     $(CXX) $(CXXFLAGS) -o abc

Then when you want to compile abc, simply run make abc in the command line.

Edit: Support for custom files

CXX = g++
CXXFLAGS = PUT YOUR FLAGS HERE
main: $(FILE).cpp
     $(CXX) $(CXXFLAGS) -o $(FILE)

This new makefile, reads in the filename from the command line. You can simply run: make FILE=abc and it will build abc.cpp and send it to -o abc

Upvotes: 3

Related Questions