Gus
Gus

Reputation: 215

Makefile for Linux from Xcode-written C++ program

I've written a simple c++ program on Xcode, all contained within ONE FILE called huffmanGenerator.cpp. The program reads input from a file on the user's computer, and writes output to a file saved to their computer.

The instructor has asked us to create a makefile so that our programs compile and run with g++ OR gcc in Linux; however she never showed us how to do so, and when the class asked for help, her answer was we could figure it out.

I found many links online, but they're all very confusing as this is all new to me, and most of them fail to answer even the most basic questions like what kind of file should the makefile be? Is it a .txt? Should I just save one in word?

Please help do what the instructor won't, enlighten me. Thanks!

Upvotes: 0

Views: 1466

Answers (2)

Gray
Gray

Reputation: 7140

what kind of file should the makefile be?

It should be a plaintext file called Makefile or makefile. The reason the name matters is because when you run the make command, it looks for a file with this name by default for directions on how to compile your code. You can also name it whatever you want as long as you specify the name when you run it (make -f filename).

Is it a .txt?

No, it has no extension. Extensions don't mean that much in *nix.

Should I just save one in word? (Assume you mean Microsoft Word.)

No, definitely not. Whitespace (tabs/spaces/new lines) have meaning in these files, so you should use an editor that won't add formatting to the file. Something like pico/vi/etc.

Here is an example of a makefile, that I think does what you are asking.

# You can change your compiler to gcc / g++ here.
CC=g++
# Add whatever flags you want to use here.
CFLAGS=-c -Wall

all:
    $(CC) $(CFLAGS) huffmanGenerator.cpp -o huffmanGenerator
#Use something like this to run `make clean` which deletes your object files, so you can do a fresh compile.    
#clean:
#   rm -rf *o huffmanGenerator

As a side note, you would be served well not to blame your professor for not spelling out everything for you. When you graduate, you will often be given tasks that have no other directions than a set of requirements and a deadline. You will need to figure it out. You could have easily made this make file by visiting http://mrbook.org/tutorials/make/ (search google for 'makefile tutorial').

Upvotes: 1

jsp
jsp

Reputation: 1283

The makefile should be called Makefile. It is just a text file.

You need a text editor. There are many to choose from, vim, emacs, nano, pico, ..., etc.

Open a command line and run, say

$ pico Makefile

Then you would enter the contents of the Makefile

all:
    g++ -o huffmanGenerator huffmanGenerator.cpp

Save and exit and run make

$ make

Upvotes: 0

Related Questions