ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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??
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.
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?
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?
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");
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.
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.
Materials for learning C programing for unix system calls
Hey,
I'm second year B tech student, in our curriculum we have UNIX programming alb where we have to write system calls using C/ C++ programming. I was wondering if you could help me in recommending some materials for writing system calls using C program ?
Please help Urgent !
I can write C programs, I just need the specific thing for writing system calls ( basically writing programs for UNIX environment )
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.