CC

来源:互联网 发布:清理注册表 软件 编辑:程序博客网 时间:2024/04/20 16:06

There are lots and lots of options for cc, which are all inthe manual page. Here are a few of the most important ones, with examples of how to usethem.

-o filename

The output name of the file. If you do not use this option, cc will produce an executable called a.out.[4]

%cc foobar.c executable is a.out
%cc -o foobar foobar.c executable is foobar

-c

Just compile the file, do not link it. Useful for toy programs where you just want tocheck the syntax, or if you are using a Makefile.

%cc -c foobar.c

This will produce an object file (not an executable) calledfoobar.o. This can be linked together with other object filesinto an executable.

-g

Create a debug version of the executable. This makes the compiler put information intothe executable about which line of which source file corresponds to which function call.A debugger can use this information to show the source code as you step through theprogram, which is very useful; thedisadvantage is that all this extra information makes the program much bigger. Normally,you compile with -g while you are developing a program andthen compile a “release version” without -g whenyou are satisfied it works properly.

%cc -g foobar.c

This will produce a debug version of the program.[5]

-O

Create an optimized version of the executable. The compiler performs various clevertricks to try to produce an executable that runs faster than normal. You can add a numberafter the -O to specify a higher level of optimization, butthis often exposes bugs in the compiler's optimizer. For instance, the version of cc that comes with the 2.1.0 release of FreeBSD is known to producebad code with the -O2 option in some circumstances.

Optimization is usually only turned on when compiling a release version.

%cc -O -o foobar foobar.c

This will produce an optimized version of foobar.

The following three flags will force cc to check that yourcode complies to the relevant international standard, often referred to as the ANSI standard, though strictly speaking it is anISO standard.

-Wall

Enable all the warnings which the authors of cc believe areworthwhile. Despite the name, it will not enable all the warnings cc is capable of.

-ansi

Turn off most, but not all, of the non-ANSI C features provided by cc. Despitethe name, it does not guarantee strictly that your code will comply to the standard.

-pedantic

Turn off allcc's non-ANSI Cfeatures.

Without these flags, cc will allow you to use some of itsnon-standard extensions to the standard. Some of these are very useful, but will not workwith other compilers--in fact, one of the main aims of the standard is to allow people towrite code that will work with any compiler on any system. This is known asportable code.

Generally, you should try to make your code as portable as possible, as otherwise youmay have to completely rewrite the program later to get it to work somewhere else--andwho knows what you may be using in a few years time?

%cc -Wall -ansi -pedantic -o foobar foobar.c

This will produce an executable foobar after checking foobar.c for standard compliance.

-llibrary

Specify a function library to be used at link time.

The most common example of this is when compiling a program that uses some of themathematical functions in C. Unlike most other platforms, these are in a separate libraryfrom the standard C one and you have to tell the compiler to add it.

The rule is that if the library is called libsomething.a, you give ccthe argument -lsomething.For example, the math library is libm.a, so you give cc the argument -lm. A common“gotcha” with the math library is that it has to be the last library on thecommand line.

%cc -o foobar foobar.c -lm

This will link the math library functions into foobar.

If you are compiling C++ code, you need to add -lg++, or-lstdc++ if you are using FreeBSD 2.2 or later, to thecommand line argument to link the C++ library functions. Alternatively, you can run c++ instead of cc, which does this for you.c++ can also be invoked as g++ onFreeBSD.

%cc -o foobar foobar.cc -lg++ For FreeBSD 2.1.6 and earlier
%cc -o foobar foobar.cc -lstdc++ For FreeBSD 2.2 and later
%c++ -o foobar foobar.cc

原创粉丝点击