Combining two programs into one in C

  • Thread starter kthouz
  • Start date
193
0

Main Question or Discussion Point

Hi!
Is it possible to combine two programs in C?
I mean for example you're wriitng a program where you will need to introduce another one that you have written before and you don't need to wrest your time in rewritting it again. Then i want to know how i can create if possible a link which can let you access it where it saved on the disk. (Like those links on the web pages).
 

Answers and Replies

chroot
Staff Emeritus
Science Advisor
Gold Member
10,165
34
You can use the #include preprocessor directive to include one source file inside of another. Depending upon your operating system, you can also use system calls like exec() to run a compiled program from within another.

- Warren
 
jim mcnamara
Mentor
3,658
1,895
You can also write one "main()" module, then compile all three together. Assuming the other two modules are not just one giant main() each. example
Code:
// prog1.c
int foo1(int);
int foo2(int);
int main(int argc, char **argv)
{
      printf("%d\n", foo1(13) +  foo2(10));
      return 0;
}
Code:
// prog2.c
 int foo1(int val)
{
    return val+1;
}
Code:
// prog3.c
int foo2(int val)
{
    return val+val;
}
Depending on your OS and your development environment you cimpile these togetehr. Here is a UNIX/Linux example:
Code:
cc -o myprog  prog1.c prog2.c prog3.c
You can also create a library or an archive of your foo1, foo2 modules. What they are called and how to make them depends on your OS and your compiler: .sl .so .a
are common "filetypes" for these in UNIX.
 
193
0
Ok i see but i can't understand clearly. May be it is just because i still on the low level. so can you give me a link where i can learn about string library cause i guess it is that field!?
 
jim mcnamara
Mentor
3,658
1,895

Related Threads for: Combining two programs into one in C

  • Last Post
Replies
1
Views
1K
Replies
4
Views
2K
Replies
3
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
6
Views
1K
Replies
1
Views
340
Replies
9
Views
1K
Top