Child pages
  • Question 2 - Create a simple Makefile
Skip to end of metadata
Go to start of metadata

First, create a C/C++ program by using any Windows/Linux editor (like Notepad for Windows or nano and vi for Linux).

For instance, we are about to create a C program called hello_world.c

/* C Example */
#include <stdio.h>

int main (int argc, char** argv)
   printf( "Hello world\n");
   return 0;

Then, create Makefile as follows, don't forget use tab before any Linux command. Also, don't forget to change the extension of the file within the Makefile depending on the language you are using (in this case, hello_world program is based on C language so the extension is .c:

all: HelloWorld
HelloWorld: hello_world.o
	gcc hello_world.o -o hello_world
hello_world.o: hello_world.c
	gcc -c hello_world.c
	rm hello_world.o

Save your file as Makefile without file extension

Once you have hello_world.c and Makefile created, just type make and then type ls to see the generated files. To run, simply type ./hello_world

[cpsuser@cpshostname]$ make
gcc -c hello_world.c
gcc hello_world.o -o hello_world
[cpsuser@cpshostname]$ ls
hello_world  hello_world.c  hello_world.o  Makefile
[cpsuser@cpshostname CE_Testing]$ ./hello_world
Hello world

  • No labels