I am working on a project that utilizes C source code to call upon a C++ function that is also linked to a C++ library. I have been able to allow for my C code to call on the C++ function, however I also want my C++ function to have access to the global variables defined in the C source code. I currently get errors when linking the C/C++ code .o files. The error messages go through each of the .o files that use these global variables and states "undefined reference to.."
This isn't the exact code or anything but basically a very simple of example of what I am trying to do. The constants.h file also gets used by other C code.
constants.h
#ifdef __cplusplus
extern "C" {
#endif
extern const int x;
#ifdef __cplusplus
}
#end if
constants.c
int x = 5;
mycppcode.cpp
#include "constants.h"
void print_x(int x){
printf("%d", x);
};