LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-18-2004, 03:46 PM   #1
pengui
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Rep: Reputation: 0
Unix system calls with c


I am a beginner and have RH 8 installed. We have UNIX Lab and i have to do c programs with the help of unix system calls.... caan u plz help me,,,
Is gcc enough to compile a c program ... plz help??
 
Old 09-18-2004, 05:13 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,408

Rep: Reputation: 108Reputation: 108
Yes, it is. When you have your code written, let's say in myprog.c you can compile it using
gcc -o progname myprog.c
The executable created will be 'progname'.

Do you have more specific questions? I'm not sure if I understand what you mean.
 
Old 09-19-2004, 12:42 AM   #3
pengui
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Original Poster
Rep: Reputation: 0
I mean i had to generate some commands like cat,grep with the help of unix system calls.
I have to include the header files fcntl.h,unistd.h.. Is all these header files availabe in Linux.. and where i have to type the code? Can i compile it in any dir?
 
Old 09-19-2004, 12:47 AM   #4
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
What do you mean by generate??
I think you probably mean executing them from C. Am I right?
or are you trying to write programs that duplicate their functions? I suspect you'll not be posting this if that's the case.

Quote:
Originally posted by pengui
I mean i had to generate some commands like cat,grep with the help of unix system calls.
I have to include the header files fcntl.h,unistd.h.. Is all these header files availabe in Linux.. and where i have to type the code? Can i compile it in any dir?
 
Old 09-19-2004, 12:51 AM   #5
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
I probably misunderstood your question.
Yes, all these headers are available.
 
Old 09-19-2004, 02:42 AM   #6
gr33ndata
Member
 
Registered: Aug 2003
Location: DMZ
Distribution: Ubuntu
Posts: 144

Rep: Reputation: 15
Sounds that you wanna execute those commands from within a C prog. So you may use commands like "system" and the "exec" family. do man system and man exec for more info. However for simplicity to run ls for example do write system("ls");
 
Old 09-19-2004, 11:47 PM   #7
shrey_j
Member
 
Registered: Jul 2004
Location: Delhi
Distribution: FC3, RH 9.0
Posts: 39

Rep: Reputation: 15
Hi pengui,

Possibly all the people above may have solved your problem, still I am posting this because even I was at this stage someday.

See gcc is a compiler in Linux, which helps compile C files, no matter where these files exists (in any directory), only thing is that you should be able to run gcc from that. [type gcc and if the output on screen is "no files specified", it is perfect]. HEader files are kept in /usr/include/ directory, and there are a large no of them including the above ones.

As for your program , here is a sample snippet:

//test.c
#include<stdio.h>
#include<stdlib.h>
int main()
{
system(" ls -l");
return 0;
}

you can replace whatever command in " " in system call. like cat, grep etc. [but please take care not to use ' " ' anywhere or they will create problem. use \" if you need to have ' " ' within your command.

File name of above prog is file.c so: type
gcc file.c -o file; which will create a output file 'file' in the same directory as you compiled in. execute it by ' ./file '. ./ is required to tell bash to execute from the local directory.

if it does not work and some problem of header files exists: use
gcc file.c -o file -I/usr/include/ where you can replace /usr/include/ with the path where gcc can find its header files [if you know where they are.

Hope it helps.
 
Old 09-20-2004, 01:43 AM   #8
suowei1979
LQ Newbie
 
Registered: Sep 2004
Location: BeiJing,China
Distribution: Red Hat
Posts: 25

Rep: Reputation: 15
 
Old 09-20-2004, 05:32 AM   #9
pengui
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by shrey_j
Hi pengui,

Possibly all the people above may have solved your problem, still I am posting this because even I was at this stage someday.

See gcc is a compiler in Linux, which helps compile C files, no matter where these files exists (in any directory), only thing is that you should be able to run gcc from that. [type gcc and if the output on screen is "no files specified", it is perfect]. HEader files are kept in /usr/include/ directory, and there are a large no of them including the above ones.

As for your program , here is a sample snippet:

//test.c
#include<stdio.h>
#include<stdlib.h>
int main()
{
system(" ls -l");
return 0;
}

you can replace whatever command in " " in system call. like cat, grep etc. [but please take care not to use ' " ' anywhere or they will create problem. use \" if you need to have ' " ' within your command.

File name of above prog is file.c so: type
gcc file.c -o file; which will create a output file 'file' in the same directory as you compiled in. execute it by ' ./file '. ./ is required to tell bash to execute from the local directory.

if it does not work and some problem of header files exists: use
gcc file.c -o file -I/usr/include/ where you can replace /usr/include/ with the path where gcc can find its header files [if you know where they are.

Hope it helps.
Than you shrey_j for your deatiled reply...
 
  


Reply

Tags
gcc


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System calls question alltime Programming 1 11-29-2005 08:31 PM
Some system calls Spooky Programming 1 11-24-2004 10:17 AM
How to Write Own Shell in Unix using System Calls? indian Programming 2 08-05-2004 04:06 AM
System Calls file Hady Programming 1 12-13-2003 12:14 PM
java and system calls aizkorri Programming 6 06-20-2003 01:54 AM


All times are GMT -5. The time now is 07:43 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration