1

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?

  • 2
  • 1
    Why not just write C code but still name the file .cpp and let the C++ compiler compile it. – Delta_G Jun 19 '17 at 21:47
  • So you know, using global variables is not best way to compose object oriented code. And I know where you are coming from (embedded processors with little room for extravagant C++ constructs). But contemporary embedded processors are way more powerful than the venerable 8051 of yesterday. And C++ is a great way to compartmentalize and isolate features to the extent they really only need to be tested once. Then reused over and over again. Give it some thought before you force old methods where newer approaches are starting to make sense and save money. – st2000 Jun 20 '17 at 2:40
  • I just deleted my 3rd attempt to answer your question. There are just too many assumptions that need to be made about your existing C++ code as well as your attempts to bring it all together using ordinary C. I keep coming back to it would be better if you just learned C++. I'll just say this, if the existing C++ classes still work for you, you should be able to use them using only formally passed values. Why do you need to use global variable on top of that? – st2000 Jun 20 '17 at 3:13
  • First, I haven't written the c code yet, just declared the new global variables, but it won't compile. – crusader27529 Jun 20 '17 at 4:18
5

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.

  • Thanks.....that's the information I needed.....hopefully I'll be able to implement it..... – crusader27529 Jun 20 '17 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.