If I have written some functions ina program p1.c, and would like to call those functions from another program, p2.c, what do I need to do to #include the p1.c program in the p2.c program?
Thanks.
If I have written some functions ina program p1.c, and would like to call those functions from another program, p2.c, what do I need to do to #include the p1.c program in the p2.c program?
Thanks.
Declare the funcs in p1.c in a header file and #include that in p2.c...then call the funcs you need
Sorry to labour the point, but how do I declare the functions in p1.h, and how do I indicate that the functions I have declared can be found in p1.c?
Thanks again!!
When you run the compiler/linker, you need to specify both source files. The linker will take care of making the functions in P1.C available to P2.C
But for the compiler to work properly, the routines defined in P1 must be 'prototyped' in P2 (look it up, you'll find information lots of places) and that's where Fordy's message becomes very useful. Add the prototypes to P1.H and #include this file in P2.C
Walt
Here's an example
Test1.cpp
Test2.cppCode:#include "Test2.h" int main() { foobar(); }
Test2.hCode:#include <iostream> void foobar() { std::cout << "Hello World"; }
Code:void foobar();
It is not usually advisable to include an entire C file in another header file, because any changes in the included file will cause all files that include the file to be recompiled. This approach will also cause unnecessary copies of function definitions to appear in the resulting program.
Peter O. Programming tips