Access variables defined in header file of another project

I have multiple C++ projects in a single solution. I have correctly defined the Additional Include Folders... settings for the project which needs to access the files from another project.So I can include the header files defined in another projects without any compilation error.

Now my problem is: Project A has a header file projA.h and its cpp file projA.cpp. The projA.h has some variables in it( and probably in projA.cpp). Now after I am able to include projA.h in projB.cpp(in another project-ProjectB), I would like to access the variables defined in projA.h inside projB.cpp. Will it be possible? If so how?

PS: There are no classes defined in ProjA.h and ProjA.cpp.

Thank you.

Last edited on
Just like normal. Note, however, that you need to define the variables as extern, otherwise multiple definition errors will ensue. Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// projA.h
#ifndef __PROJA_H_INCLUDED__
#define __PROJA_H_INCLUDED__

extern int var;
void func();

#endif


// projA.cpp
#include "projA.h"
int var = 0;
func() {
    var += 1;
}


// projB.cpp
#include "projA.h"
#include <iostream>

void doSomething() {
    while (var < 100) {
        func();
        std::cout << var << ' ';
    }
}


EDIT:
This is assuming that at least one of these is either a static or dynamic library, so that it can find the variable at linking time. Otherwise, I don't think you can share variables across processes.
Last edited on
Topic archived. No new replies allowed.