开发者

Tips on using GCC as a new user [closed]

开发者 https://www.devze.com 2023-01-03 06:30 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 6 years ago.

Improve this question

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 al开发者_Go百科l of your suggestions. Thanks again.. :)


Use -Wall all the time.

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消