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

#include <stdio.h>

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

child.c

#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

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.

/* main.c */

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

int main()
{
        int ret_option;
        ret_option = menu();
        return 0;
}
/* 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__*/
/* 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);
}
$ 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:

$ 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.

2 Likes

Wow.. this was so informative ..
Thanks a lot for the detailed explanation. :slight_smile: