Wednesday, April 20, 2016

Writing a C program, Compiling It, MakeFiles

In order to write a C-program you need a text editor. CentOs comes with both VIM and Gedit. I like Gedit better so I will show the instructions for it.

To create a C program, first navigate to a directory in a terminal that you wish to save your program in. Type in the command line:

  • gedit test_program.c
This will open gedit with a blank C-program.
Write your C-program, a "Hello World" program is a good place to start:

int main(int argc, char *argv[])
{
    printf("Hello world.");

    return 0;
}

Save your program.


The next step is to compile your program using 'make' in the command line. In order for 'make' to be used, we first need to create a Makefile called 'Makefile.'  You can use gedit again to create this file and it should be saved in the same directory as your test_program.c. Copy these lines into Makefile:

CFLAGS=-Wall -g

clean:
   rm -f ex1

Save your file.

Go back to your terminal and navigate to the directory where your program and Makefile are saved.

  • Type make test_program to compile the code
  • Type ./testprogram to see the results
The results:
"Hello World"


is written directly in the terminal.

To learn a whole lot more see:





No comments:

Post a Comment