I am running 2 CPP programs simultaneously and both of them have a main() function. I want to use a variable that is getting updated frequently from A.cpp in B.cpp. When I tried importing the header file of A.cpp (A.hpp) using extern and tried accessing global_x in B.cpp, I get 'undefined reference to 'global_x'. Can anyone point me in the right direction?
Asked
Active
Viewed 112 times
0
-
3Each process has its own separate address space. If you want to share information between processes you need to learn IPC (Inter-process communication). The methods available differ by operating system. – Dave S Mar 20 '20 at 05:29
-
2You can't share variables in two different programs like that. The easiest way to "share" data between programs is either by using files, pipes, shared memory or one of the many other ways of [*inter-process communication*](https://en.wikipedia.org/wiki/Inter-process_communication). – Some programmer dude Mar 20 '20 at 05:30
-
Reopened as this clearly isn't a duplicate of the suggested question. This question is really about inter-process communication, not how to manage global variables. – john Mar 20 '20 at 07:24
-
@john While the title of the question I marked as duplicate is misleading, it is asking for the same thing: To share a variable between two `.cpp` files, each with its own `main()`, i.e. two separate programs. The top answer mentions how to share variables between translation units, but also how to share them between processes. – walnut Mar 20 '20 at 07:42
-
@walnut OK, I missed that. Please feel free to mark it as a duplicate again. – john Mar 20 '20 at 07:58
-
@john I cannot again close vote. I am not sure whether you can close vote after a reopen vote, the question is still linked on the right. – walnut Mar 20 '20 at 08:13
-
@DaveS Thank you for that. I will look into Inter-Process Communication. – md.mizba Mar 21 '20 at 19:18