Calling c program from another c program


 
Thread Tools Search this Thread
Top Forums Programming Calling c program from another c program
# 1  
Old 01-27-2011
CPU & Memory Calling c program from another c program

Hi All,

Probably this is a repeated question. My knowledge in this is limited and i got confused on all those materials i got in google search.
We use #include <> to include a predefined library like stdio.h
i saw somewhere that #include "" includes a man made module(another C program). IS this possible?

Let me make this simple
I have two files, say parent.c and child.c
child.c contains function definitions which is called from parent.c

My code:
parent.c
Code:
#include <stdio.h>

int main()
{
        int ret_option;
        ret_option = menu();
        return 0;
}

child.c

Code:
#include<stdio.h>
int menu();
int menu()
{

        int option;
        printf("\tWelcome to Address Search Engine\n");
        printf("-----------------------------------------\n");
        printf("1. Add\n2. Edit\n3. Delete\n4. Search\n0 for exit\n");
        printf("Enter your Selection : ");
        scanf("%d",&option);
        return (option);
}

What i need to do here is just display the menu on the screen.

Now how do I call this or include or link the two files ?


Thanks in advance
JS
# 2  
Old 01-27-2011
You usually don't include ".c" files. That just adds the contents of that file to yours raw, so you might as well have just copy-pasted it in the first place.

If you use header files and link properly you can define the object once but use it many times.

"parent" and "child" are the wrong terms to use. They mean something specific and this isn't it. Let's call them main.c and menu.c. We also need a menu.h, which specifies exactly what functions are available in menu.c but doesn't define them.
Code:
/* main.c */

#include <stdio.h>
#include "menu.h"

int main()
{
        int ret_option;
        ret_option = menu();
        return 0;
}

Code:
/* menu.h */
// The ifdef parts make multiple inclusion safe.
// So, optional, but will save you many headaches later
// when your include files start including other include files.
#ifndef __MENU_H__
#define __MENU_H__

extern int menu(void);

#endif/*__MENU_H__*/

Code:
/* menu.c */
#include <stdio.h>
#include "menu.h"

int menu(void)
{
        int option;
        printf("\tWelcome to Address Search Engine\n");
        printf("-----------------------------------------\n");
        printf("1. Add\n2. Edit\n3. Delete\n4. Search\n0 for exit\n");
        printf("Enter your Selection : ");
        scanf("%d",&option);
        return (option);
}

Code:
$ gcc menu.c main.c -o filename
$ ./filename
	Welcome to Address Search Engine
-----------------------------------------
1. Add
2. Edit
3. Delete
4. Search
0 for exit
Enter your Selection : 0
$

In short, you use .c files to define functions. When you compile multiple files together, all the functions in each file are available to the other files, but you must have the function described in detail for them to use it properly. This is what header files are traditionally for.

Or, if you want a little more detail:

Code:
$ gcc -c menu.c
$ gcc -c main.c
$ ls menu.o main.o
menu.o  main.o
$ gcc menu.o -o filename
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
$ gcc main.o -o filename
main.o: In function `main':
main.c:(.text+0x9): undefined reference to `menu'
collect2: ld returned 1 exit status
$ gcc menu.o main.o -o filename
$ ./filename
	Welcome to Address Search Engine
-----------------------------------------
1. Add
2. Edit
3. Delete
4. Search
0 for exit
Enter your Selection : 0
$

During the compilation step, each .c file is compiled into an object (.o) file. An object knows two things, what symbols it provides, and what symbols it needs. At this point it's okay for it to use functions that aren't defined inside the C file if you convince it they will exist somewhere else. Which, again, is what header files are for.

During the linking step, it takes objects and uses them to fill in any missing bits. It finds the menu() function from lib.o and attaches it to the place it's used in main.c, and all the functions from stdio.h it fills in from the default libraries. If there's any functions that've been used but not defined anywhere, or somehow defined more than once, this is the point it'll give up at.

Once it's tracked down where absolutely everything belongs, it fixes things into place and turns it into an executable. 'Parent' and 'child' are bad terms because none of these objects is "higher" than each other. Functions get tossed into one big pile. (library ordering, on the other hand, is sometimes a little more important on some compilers. Still, you usually don't need to worry about it.) menu.o could use a function from main.o just as easily as main.o uses functions from it.

Compiling and linking always happen when you turn C files into executables. When you do gcc filename.c that just does both steps for you behind the scenes.

Last edited by Corona688; 01-27-2011 at 11:19 AM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 01-28-2011
Wow.. this was so informative ..
Thanks a lot for the detailed explanation. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Program crashes on calling __libc_msgrcv()

Hi, I am a newbie to linux programming. I have implemented msgqueue in C. msgrcv() call at the client end is as below: msgrcv( msgqid, msgptr, msgsize, msgtype, 0 ); My program works fine when msgrcv () from /lib/libc.so.6 is called. However it crashes when __libc_msgrcv() is called. ... (3 Replies)
Discussion started by: praasanna
3 Replies

2. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

3. Programming

Calling a shell script from a C program

Hi, I have a shell script which connects to a database and fetches the count of the records from a table. I want to embed this whole script in a C program. Also the count fetched should be available in the C program for further usage. Please let me know how this can be done. Thanks (9 Replies)
Discussion started by: swasid
9 Replies

4. Shell Programming and Scripting

Calling a shell script from a C program

Hi, I have a shell script which connects to a database and fetches the count of the records from a table. I want to embed this whole script in a C program. Also the count fetched should be available in the C program for further usage. Please let me know how this can be done. Thanks ... (0 Replies)
Discussion started by: swasid
0 Replies

5. UNIX for Dummies Questions & Answers

Calling a c program using perl script

On bash I run precompiled c Program as follows: ./create_cust 1 10000 US S > us_cust.csv create_cust is a c program and requires 4 parameters. I am redirecting the output of this program to csv file I need to run this same program in perl I am aware of exec command though not... (7 Replies)
Discussion started by: gkbond
7 Replies

6. UNIX for Advanced & Expert Users

calling a Universe program

Can someone offer some technical advice concerning an call to a IBM U2 (Universe) program? When I use the following script from a unix shell, it works fine: $ " xxx.sh " (contains the following --->) 1. cd /links/ACCOUNT1 2. /shapps/ibm/uv/bin/uv "COUNT FILE1" ... (2 Replies)
Discussion started by: smintz
2 Replies

7. Shell Programming and Scripting

calling a program (w/ params) from within shell

Hi all, I need to call some script (s1) from within my shell script (s2). s1 accepts parameters and I want to feed it with values of params from my script. I tried many things but none work (I am so much of a beginner), please help one of my attempts : . . . param1="hehe" param2="haha" ... (12 Replies)
Discussion started by: Lorna
12 Replies

8. Shell Programming and Scripting

Calling Functions of Other K Shell Program

Hi, I have a K shell a.ksh function abc { // Some logic } In b.ksh i have included the a.ksh ./a.ksh I want to call the abc function from this b.ksh script. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies

9. UNIX for Advanced & Expert Users

calling program

hi, i have a script.sh on my machine and it used in the system but my question is how can i know the program called this script.sh?? i.e. from where it called and execute?? Many thanks (1 Reply)
Discussion started by: alzuma
1 Replies

10. Shell Programming and Scripting

Calling SHELL script from C program

Hi, I just tried to call a simple script from a pretty simple C program. I could not succeed :-( a message was thrown saying "sh: line 1: "Script name with path": Permission denied" The C program and shell script are below, both are in the same directory and shell script is given... (7 Replies)
Discussion started by: Chanakya.m
7 Replies
Login or Register to Ask a Question

Featured Tech Videos