VoyForums
[ Show ]
Support VoyForums
[ Shrink ]
VoyForums Announcement: Programming and providing support for this service has been a labor of love since 1997. We are one of the few services online who values our users' privacy, and have never sold your information. We have even fought hard to defend your privacy in legal cases; however, we've done it with almost no financial support -- paying out of pocket to continue providing the service. Due to the issues imposed on us by advertisers, we also stopped hosting most ads on the forums many years ago. We hope you appreciate our efforts.

Show your support by donating any amount. (Note: We are still technically a for-profit company, so your contribution is not tax-deductible.) PayPal Acct: Feedback:

Donate to VoyForums (PayPal):

Login ] [ Contact Forum Admin ] [ Main index ] [ Post a new message ] [ Search | Check update time | Archives: 1[2] ]


[ Next Thread | Previous Thread | Next Message | Previous Message ]

Date Posted: 02:06:15 07/17/07 Tue
Author: Anandan
Subject: gcc (To compile C Source code) with basic options (switches)
In reply to: Anandan 's message, "C Programming Development Environment on Unix" on 00:23:55 07/17/07 Tue

Let the C program be in the file: test1.c. The code is:

#include
void main(void)
{
printf("Hello World!\n");
}

To compile this program:
$gcc test1.c

There may be a warning depending on he gcc version. Ignore it now and the output would be in a file called a.out

To run the program
$./a.out

That will display the line:
Hello World!

To put the output into specified file use -o option as given below while compiling the program:

gcc -o test1 test1.c

The generated file would be test1 and to run the same:
$./test1
Hello World!

Warnings:
Warnings are controlled by -W option to gcc. One can enable all of the most common warnings with the -Wall command:
$gcc -Wall -o test1 test1.c
test1.c:4: warning: return type of main is not 'int'

The fixed program would be
//FileName:test1.c
#include
#include

int main(void)
{
printf("Hello World!\n");
return EXIT_SUCCESS;
}
Another useful option is -Werror switch, which causes gcc to treat all warnings as errors. This is particularly useful when using automated compilation, such as with the GNU make tool. When -Werror is used, gcc will not finish the compilation if any warning is detected. Therefore, you don't want to include this in release versions of software because other users' compilers may gnerated warnings on different things. However, when working with large projects with which gcc or make may generate several thousand lines of output, having the compilation aborted in this manner can be beneficial.
$gcc -Wall -Werror -o test1 test1.c
For more complex programs, the -Wall switch can be an extremely useful tool for tracking down and preventing bugs.

Optimizations with gcc
======================
The opimizer is part of the compiler that is capable of examining your code (or the assembler code generated by the compiler
), identifying those areas that are suboptimal, and rewriting them using code that does the same thing in less space or with
better performance. In gcc, one can enable optimizations by using one of the -0 options. If you simply use -0, this is take
n aslevel one (or -01); -0 is the same as -01. You can go up to level three (or -03).
So, to use basic optimizations, one might use a command line such as the following:
$gcc -Wall -01 -o myprogram myprogram.c
Hence the optimizations are: -0, -01 (same as -0), -02 and -03. higher the number, the more aggressive gcc becomes with opti
mizations. More aggressive optimizations mean that your code runs faster.
More aggressive gcc takes mre time for the program to compile. Therefore, some prefer to compile without optimizations durin
g day-to-day development, but enable optimizations when the time to release and finish the program nears.
Debugging can be difficult when the optimization is enabled. Hence avoid optimizations as much as possible when debugging yo
ur programs. The option -02 proves the best compromise between optimization strength, compile time and code size.

To see how much time a program takes to execute may be observed using time command as given below:

//File name:test2.c
#include
int main(void)
{
int counter, ending, temp, five;
for(counter = 0; counter < 2*100000000*9/18 + 5131; counter += (5-3)/2)
{
temp = counter/15302;
ending = counter;
five = 5;
}
printf("five = %d; ending = %d\n", five, ending);
return 0;
}

First compile without optimizations:
$gcc -Wall -o test2 test2.c
Run the executable:
$time ./test2
five = 5; ending = 100005130

real 0m15.146s
user 0m14.960s
sys 0m0.000s

Now try gcc with basic optimizations enabled:
$gcc -Wall -01 -o test2 test2.c
And examine the results of execution this time:
$time ./test2
five = 5; ending = 100005130

real 0m2.220s
user 0m2.200s
sys 0m0.000s

[ Next Thread | Previous Thread | Next Message | Previous Message ]


Replies:

  • gcc basics part2 -- Anandan, 04:17:28 07/17/07 Tue
    [ Contact Forum Admin ]


    Forum timezone: GMT-8
    VF Version: 3.00b, ConfDB:
    Before posting please read our privacy policy.
    VoyForums(tm) is a Free Service from Voyager Info-Systems.
    Copyright © 1998-2019 Voyager Info-Systems. All Rights Reserved.