The calling function in a C ++ program, where the function is declared in another C ++ program

how can a function be called in a c ++ program where the function is declared in another c ++ program? how can this be done? can i use extern ?

+1
c ++
Sep 18 '10 at 11:31
source share
4 answers

I would suggest that the best way is to reorganize the first C ++ program so that the required function is included in the library. Then both of your programs can reference this library, and the function is available for both (and for any other programs that require it).

Check out this tutorial . It describes how to create and then use the library with gcc . Other similar tutorials will exist for other C ++ variants.

+7
Sep 18 '10 at 11:36
source share

If you mean programs as "processes", it depends on how you run your programs. In most cases, you cannot easily (if at all) because the processes will need to share memory. In debug versions of some of them, this is possible. In a few words: if you mean that you want to call a function in the code of a running program from another program, it is very difficult, and VERY system-dependent.

+3
18 sept. '10 at 12:32
source share

#include source file with a different function declaration.

+2
Sep 18 '10 at 11:34
source share

Announced or defined? It is important to keep in mind that before using a function, the compiler must know about the prototype of the function, so use #include to make sure that the compiler has access to the prototype. It does not need the actual code for the function, it is mandatory that it becomes important during communication.

So, if you have:

MyFunc.hpp:

 int add( int a, int b); 

MyFunc.cpp:

 int add( int a, int b) { return a + b; } 

Then you can use this in another file:

main.cpp

 #include <iostream> #include <MyFunc.hpp> // This is the important bit. You don't need the .cpp int main( int argc, char* argv[] ) { std::cout << add( 20, 30 ) << std::endl; } 
+2
Sep 18 '10 at 12:38
source share



All Articles