user29356: I'm not a C++ programmer (but I have used regular C code for implementing low-level machine language equivalent SW for some time), and after trying to modify an existing cC+ program to do what I need (and failing), I've decided to rewrite parts of the code in plain C code.
But to be able to use the data, I need to create some variables that have to be accessible to parts of the existing C++ code.
When I declare a global variable (extern int variable name;) outside the main program, the compile fails because the references to the global variable aren't within the scope of the cpp parts of the program.
I don't program with multiple files myself, so I was wondering if I need to globally declare the same variables as part of each cpp program that needs to have access to the variables? Is there a another/better way?

user24459: When using multiple source files you need to use header files to share things between them.
myproject.h:
#ifndef __myproject_h__
#define __myproject_h__
extern int aGlobalVariable;
#endif

myNewCode.c
#include "myproject.h"
int aGlobalVariable;

modifiedOldCode.cpp
#include "myproject.h"

This way the header file defines the variable as an extern in all of your c / cpp files so that it is visible to all of them but the variable is only actually declared and memory allocated for it in one place, in the .c file.
Similarly any functions that you want to be visible in both files should be declared in the headerfile (e.g. void myfunction(int x);) but the actual definitions should be somewhere in the .c or .cpp files.
The #ifndef and #define lines in the header as there so that if the header somehow gets included twice you don't get lots of redefinition errors.
There is nothing strange about c++ and globals, a global defined this way is global to everything, it doesn't matter what object you're in or how things are scoped. This can however have some strange effects since there is only ever one copy of a global while you can possibly have multiple instances of the c++ class that uses it. This can cause some consistency related issues if you aren't careful. Whether you need to worry about this or not depends very much on the nature of the code.

29356: <p>I'm not a C++ programmer (but I have used regular C code for implementing low-level machine language equivalent SW for some time), and after trying to modify an existing cC+ program to do what I need (and failing), I've decided to rewrite parts of the code in plain C code.</p>

<p>But to be able to use the data, I need to create some variables that have to be accessible to parts of the existing C++ code.</p>

<p>When I declare a global variable (<code>extern int variable name;</code>) outside the main program, the compile fails because the references to the global variable aren't within the scope of the cpp parts of the program.</p>

<p>I don't program with multiple files myself, so I was wondering if I need to globally declare the same variables as part of each cpp program that needs to have access to the variables? Is there a another/better way?</p>
 24459: <p>When using multiple source files you need to use header files to share things between them.</p>

<p>myproject.h:</p>

<pre><code>#ifndef __myproject_h__
#define __myproject_h__
extern int aGlobalVariable;
#endif
</code></pre>

<p>myNewCode.c</p>

<pre><code>#include "myproject.h"
int aGlobalVariable;
</code></pre>

<p>modifiedOldCode.cpp</p>

<pre><code>#include "myproject.h"
</code></pre>

<p>This way the header file defines the variable as an extern in all of your c / cpp files so that it is visible to all of them but the variable is only actually declared and memory allocated for it in one place, in the .c file.<br>
Similarly any functions that you want to be visible in both files should be declared in the headerfile (e.g. <code>void myfunction(int x);</code>) but the actual definitions should be somewhere in the .c or .cpp files.<br>
The #ifndef and #define lines in the header as there so that if the header somehow gets included twice you don't get lots of redefinition errors.</p>

<p>There is nothing strange about c++ and globals, a global defined this way is global to everything, it doesn't matter what object you're in or how things are scoped. This can however have some strange effects since there is only ever one copy of a global while you can possibly have multiple instances of the c++ class that uses it. This can cause some consistency related issues if you aren't careful. Whether you need to worry about this or not depends very much on the nature of the code.</p>
