tags:

views:

88

answers:

4

I am really new to GCC and I don't 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 where to go from here... Please give me some tips on the different path to 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.. :)

+4  A: 

Use -Wall all the time.

Don't ignore warnings; fix them as they crop up.

James McNellis
+3  A: 

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.

paxdiablo
I prefer "Hello World". especially when taking "baby" steps :p
Midhat
A common newbie trip-up is that you will probably need to do `./hi` and not just `hi` to run it, depending on `PATH` settings.
detly
thanks... i would love to use an ide for that after i learned that... so what IDEs are available out there for free which i could use with GCC? Thanks!
ultrajohn
eclipse ftw.ALso kate. Its simple, not overwhelming like eclipse, and lets you open a file browser, text editor and console all in teh same window so you can edit, compile and run your code; and switch between multiple files
Midhat
I'd suggest Eclipse under Linux and Code::Blocks under Windows (and Linux). Eclipse/MinGW under Windows is not for the faint of heart :-)
paxdiablo
A: 

Makefiles are very helpful. You should definitely check out how to use them.

helixed
Woah not so fast.I guess you stay away from them for a while until you are quite comfortable with gcc
Midhat
A basic Makefile isn't that much more complicated than compiling on the command line with gcc, and it can save a lot of retyping while you're learning.
helixed
+1  A: 

Unless you have no interest in portability, make sure you learn which features of GCC are GNU extensions to the standard. Granted, GCC is available on most machines, but it would usually be silly to restrict your code so it only compiles with GCC.

To that end, as well as the ubiquitous -Wall, I usually use -std=c99 (or -std=c89) with -pedantic. I like to work with -Wmissing-prototypes -Wstrict-prototypes to ensure that no functions are undeclared. Where the code is really clean, I will add -Wextra (more warnings than -Wall) and -Werror (treat warnings as errors). This makes sure that the code stays really clean - the compilation fails when there's a warning.

Jonathan Leffler