1

I have two cpp files, and I want one of them to be able to tell me the value of some specific variables declared in the other C++ file. For example:

file1.cpp

int var1 = 10;
while (true) {std::cin >> var1;}

I want another cpp file, file2.cpp, when compiled, to be able to tell me the value of file1->var1.

Now, I realise that I can use files to store the value and read it but that seems like it's not the "right" way to do it, so what would the right way be (maybe storing it in a specific spot in RAM?)

Edit: Since some people asked, both the cpp files are going to be compiled separately. They are going to be independent executables.

avighnac
  • 376
  • 4
  • 12
  • It's OS specific. No such facility, apart from files, is part of C++. – doug Nov 21 '21 at 18:56
  • Either: (a) put the 2 files into one program; (b) use an external file. – Richard Critten Nov 21 '21 at 18:58
  • ***maybe storing it in a specific spot in RAM?*** Would be OS specific. Also unless you are on an embedded system (or a 30 year old OS) you likely have virtual memory and don't directly access system RAM. – drescherjm Nov 21 '21 at 19:04
  • What do you mean by "I have 2 cpp files", do you compile them to seperate executables? Or do you want to link them into one executable? – PeterT Nov 21 '21 at 19:05
  • We can't really help you if you don't edit the question to add additional information about the 2 cpp files. – drescherjm Nov 21 '21 at 19:10
  • Why would it be OS specific? Also how would one go about doing this on Windows and is it worth it? – avighnac Nov 21 '21 at 19:26
  • If it's 2 seperate executables then they are typically started as 2 different processes. The OS manages and isolates processes from each other. You can google "inter process communication" to find the different mechanisms that can be used to communicate between processes. – PeterT Nov 21 '21 at 19:27
  • 1
    Looks like [XY problem](https://xyproblem.info/), please provide more details what kind of problem you have. Do you have two separate application, does one application starts other application, how often this value changes and how often other application should see this changes. Why do you need two processes? Is shared value simple integer or something more complex. – Marek R Nov 21 '21 at 19:37
  • So many ways https://en.wikipedia.org/wiki/Inter-process_communication#Approaches – KamilCuk Nov 21 '21 at 19:48
  • I'm going to be making app2 be able to change and see the values in app1, app2 cannot start without app1 but vice versa is possible, no they do not start one other, some values I want to share update every frame, and app2 should be able to see this. – avighnac Nov 22 '21 at 03:55

2 Answers2

1

You can use what's called named pipe as solution to connect your program 1 with the program 2 . A named pipe, also called a FIFO for its behavior, can be used to connect two unrelated processes and exists independently of the processes; meaning it can exist even if no one is using it. A FIFO is created using the mkfifo() library function.

Example

writer.c

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";

    /* create the FIFO (named pipe) */
    mkfifo(myfifo, 0666);

    /* write "Hi" to the FIFO */
    fd = open(myfifo, O_WRONLY);
    write(fd, "Hi", sizeof("Hi"));
    close(fd);

    /* remove the FIFO */
    unlink(myfifo);

    return 0;
}

reader.c


#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_BUF 1024

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";
    char buf[MAX_BUF];

    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    printf("Received: %s\n", buf);
    close(fd);

    return 0;
}

Edit : sorry I noticed that you need it in C++ language, The code for C++ language you can find it here Create Named Pipe C++ Windows

yassine
  • 88
  • 9
1

Q: How to pass information between two separate processes (written in c++)?

A: Use an IPC:

https://en.wikipedia.org/wiki/Inter-process_communication

In computer science, inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data. Typically, applications can use IPC, categorized as clients and servers, where the client requests data and the server responds to client requests.

As the article discusses, there are many, many ways processes can communicate. Many are platform-specific. To name just a few:

Some questions you need to ask:

  • Do I need to communicate between processes, or just share information between threads in the same process?
  • Communicate between different processes on the same host, or will I need to communicate between hosts?
  • Should the solution be portable to different OSs (e.g. work on Windows, Linux and MacOS)?
  • Would I benefit from a 3rd party framework or library?
  • Etc. etc.

There is no "right way"; there is no "one size fits all solution".

Please provide more details about your "use case" and your "constraints", so we can provide you a better-informed answer.

paulsm4
  • 114,292
  • 17
  • 138
  • 190