Page 30 - C-Language
P. 30

See more about compiling


        Compile using GCC



        GCC (GNU Compiler Collection) is a widely used C compiler. To use it, open a terminal, use the
        command line to navigate to the source file's location and then run:


         gcc hello.c -o hello


        If no errors are found in the the source code (hello.c), the compiler will create a binary file, the
        name of which is given by the argument to the -o command line option (hello). This is the final
        executable file.


        We can also use the warning options -Wall -Wextra -Werror, that help to identify problems that can
        cause the program to fail or produce unexpected results. They are not necessary for this simple
        program but this is way of adding them:


         gcc -Wall -Wextra -Werror -o hello hello.c


        Using the clang compiler



        To compile the program using clang you can use:


         clang -Wall -Wextra -Werror -o hello hello.c


        By design, the clang command line options are similar to those of GCC.


        Using the Microsoft C compiler from the command line


        If using the Microsoft cl.exe compiler on a Windows system which supports Visual Studio and if all
        environment variables are set, this C example may be compiled using the following command
        which will produce an executable hello.exe within the directory the command is executed in (There
        are warning options such as /W3 for cl, roughly analogous to -Wall etc for GCC or clang).



         cl hello.c


        Executing the program



        Once compiled, the binary file may then be executed by typing ./hello in the terminal. Upon
        execution, the compiled program will print Hello, World, followed by a newline, to the command
        prompt.


        Original "Hello, World!" in K&R C


        The following is the original "Hello, World!" program from the book The C Programming Language


        https://riptutorial.com/                                                                                6
   25   26   27   28   29   30   31   32   33   34   35