Search code examples
gcccompiler-construction

Tips on using GCC as a new user


I am really new to GCC and I don't know how to use it. I already have a copy of a pre-compiled gcc binaries I've downloaded from one of the mirror sites in the gcc website. Now, I don't know where to go from here. Please give me some tips on how I can proceed.

I am sorry for the rather vague question..

What I want are tips on how to use GCC. I've programmed in C in the past using the TC compiler. Thanks!

I really appreciate all of your suggestions. Thanks again.. :)


Solution

  • Baby steps to start with.

    Create the file you want to compile (hi.c) in your favorite editor, like:

    #include <stdio.h>
    int main (void) {
        printf ("Hi there\n");
        return 0;
    }
    

    Then from the command prompt, execute:

    gcc -o hi hi.c
    

    That will give you an executable you can then run.

    Beyond that, it really depends on how much C (or C++ or other GCC language) you know. If you're a beginner at C, rather than just the GCC toolchain, get yourself a good beginner's book on the language and start reading. Most importantly, do the exercises, play with the code and so forth.


    Based on your update that you're comfortable with C itself (the Borland line), you'll probably only be interested in the immediate GCC differences.

    GCC is a command-line compiler. There are IDEs that use it but GCC itself is not an IDE. That means you'll probably be doing command-line compilation.

    The basic forms of this are:

    # creates an executable "exe" from your source file "src.c"
    gcc -o exe src.c
    # creates an executable "exe" from your source files "src.c" and "extra.c"
    gcc -o exe src.c extra.c
    # creates object files from your source files
    gcc -c -o src.o src.c
    gcc -c -o extra.o extra.c
    # creates an executable file "exe" from your object files
    gcc -o exe src.o extra.o
    

    Once you get sick of doing that, you'll want to learn how to use make, a way of automating the build process with a file containing rules (dependencies and actions to take), such as:

    all: exe
    
    clean:
        rm -rf exe src.o extra.o
    
    rebuild: clean all
    
    exe: src.o extra.o
        gcc -o exe src.o extra.o
    
    src.o: src.c
        gcc -o src.o src.c
    
    extra.o: extra.c
        gcc -o extra.o extra.c
    

    I don't do justice to the power of make here, it's far more expressive than it looks.