First things first. Create the following text file and save as 'hello.c'.
#include <stdio.h>
int main(void){
printf("Hello, World.\n");
}For a simple program like the Hello World example above, the GNU Compiler Collection (gcc) is fairly simple to use. If the above program is saved as hello.c, you would execute $ gcc hello.c. This will produce an executable file labeled a.out. To run the file, execute $ ./a.out.
To change the name of the name of the executable that is created, use the -o option. Thus to create an executable called 'hello', we would type $ gcc hello.c -o hello. To run this program, we would now use $ ./hello instead of $ ./a.out.
Additionally, we will add a few options that will effectively make the compiler be stricter on our code. These are-Werror, -Wall, and -Wpedantic. From the man page:
-Werror- Make all warnings into errors.-Wpedantic- Issue all the warnings demanded by strict ISO C...-Wall- This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning)
Using all these options, we would compile 'hello.c' by executing $ gcc -Werror -Wall -Wpedantic -o hello hello.c.
Compiling C files can quickly get more complex, especially as multiple files are added. Makefiles contain a set of rules that tell the compiler how all the pieces fit together and how you want the compilation executed. With a properly built Makefile, you simply need to run the command "make".
For the Hello World program above, a corresponding Makefile might look like the following:
hellomake: hello.c
gcc -Wall -Werror -Wpedantic -o hello hello.c
Basic declaration: type arrayName [arraySize];.
1-1
1-2
1-3
1-4
1-5
1-6
1-7
1-8
1-9
1-l0
Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd ed.). Englewood Cliffs, NJ: Prentice Hall
This page was last updated: Tue 31 Oct 2017 05:55:10 PM EDT