Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Combining two programs into one in C

  1. Mar 14, 2008 #1
    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).
     
  2. jcsd
  3. Mar 14, 2008 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  4. Mar 15, 2008 #3

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    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 (Text):

    // prog1.c
    int foo1(int);
    int foo2(int);
    int main(int argc, char **argv)
    {
          printf("%d\n", foo1(13) +  foo2(10));
          return 0;
    }
     
    Code (Text):

    // prog2.c
     int foo1(int val)
    {
        return val+1;
    }
     
    Code (Text):

    // 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 (Text):

    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.
     
  5. Mar 18, 2008 #4
    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!?
     
  6. Mar 18, 2008 #5

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Combining two programs into one in C
  1. C Program (Replies: 3)

  2. C program (Replies: 0)

  3. C programming (Replies: 9)

Loading...