[Ilugc] Regarding c file execution :

  • From: sriramadhas@xxxxxxxxx (Sri Ramadoss Mahalingam)
  • Date: Thu Jul 8 14:21:01 2004

HowTo build simple C programs on Linux

1. Introduction

This document will step you though compiling and running simple C
programs under the Linux operating system. It is meant as a guide for
beginners who may know how to program in C but don't know how to
build a C program on Linux. This document uses simple C programs to
illustrate how to build the programs under Linux. It covers writing a
basic C program but it is not meant as a guide to teach the language.
We will also step through the basics of writing a Makefile for your
project.

This document also assumes that you know how to create/edit files on
Linux and that you have the GNU C compiler installed. An easy way to
tell if you have a C compiler installed is by issuing the
command 'which gcc'. We are also assuming that the compile is
executed from the command line. There are far too many GUI/IDE type
environments to cover otherwise.

2. A Simple program

2.1 Writing the source

The source of our first program is below:

code: #include <stdio.h>

main()
{
printf("Linuxquestions.org\n");
}




Save the program above and call it simplelq.c

Here is the breakdown of the program above.
The first line #include <stdio.h> is a preprocessor directive. This
basically tells the compiler that we are using the functions that are
in the stdio library. The stdio library contains all the basic
functions needed for basic input and output for our program.

The second line main() is a required function for every C program.
main() is the starting point for the program. Like all functions the
body begins with a { (open curly brace) and ends with a } (close
curly brace).

The body of our main function printf ("Linuxquestions.org\n"); is a
function call to the printf function. printf stands for print
formatted and has complex rules for printing text, numbers to
specific formats. Here we are just displaying the
test "Linuxquestions.org" to the screen. The \n at the end of string
is a newline. It tells printf to end the line and start any
additional text on the next line. All function calls in C must end in
a ;

2.2 Compiling the Source

The compile is done from the command line.


code: $ gcc -o simplelq simplelq.c
$




gcc is the GNU C compiler. The -o option tells it what to name the
output file and the simplelq.c is the source file.

The output from the compiler will be a binary file called simplelq

2.3 Running the executable

In order to run our sample executable we will need to apply the
execute permission to the file. The we will execute it after.

code: $ chmod 744 simplelq
$ ./simplelq
Linuxquestions.org




The output of our sample program produced the
text "Linuxquestions.org" to the console (or screen). Try to add some
more text to the sample program, recompile and watch the output.




Sridhar R <sridharinfinity@xxxxxxxxx> wrote:On Wed, 7 Jul 2004 00:07:55 -0700 
(PDT), kumari mani
wrote:

sir ,When i execute a C program
(which is compiling&running in Win) in RH9.0

cc compiler is indicating an error "header file not
found"

please help me

1) how to include a header in c

Just include it as you do with others. See K&R.

2) what is the directory in which all the header files
are placed.

in *nix, the headers are placed in /usr/include .. but many programs
install their headers in special places like /usr/include/python2.3,
etc . Use the gcc -I option to specify extra header file paths.

3) how do i get a help(sample function usage) from
RH9.0

Just do a `man functionname`. If you're looking for a specific
library, see the library docs.



-- 
Sridhar - http://www.cs.annauniv.edu/~rsridhar
Blog: http://www.livejournal.com/users/sridharinfinity
_______________________________________________
To unsubscribe, email ilugc-request@xxxxxxxxxxxxx with 
"unsubscribe 
address"
in the subject or body of the message. 
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc


                
---------------------------------
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.ae.iitm.ac.in/pipermail/ilugc/attachments/20040708/d0a51f94/attachment.htm

Other related posts: