Access Extern Variable in C++ from Another File

Accessing an extern C variable in c++ from another file

They are not the same.

extern "C" int foo;

Does TWO things: it declares that foo is extern (i.e. this is a declaration only, and the symbol will be defined by another module), and it declares the linking of foo as "C", which influences the symbol name.

On the other hand,

extern "C" {
int foo;
}

only declares that foo has C linking. It does not declare the symbol as extern, which means that this is a definition, not just a declaration.

In other words

extern "C" int foo;

is the same as (note the repetition of the extern keyword)

extern "C" {
extern int foo;
}

How do I share a global variable between c files?

file 1:

int x = 50;

file 2:

extern int x;

printf("%d", x);

How do I share variables between different .c files?

In fileA.c:

int myGlobal = 0;

In fileA.h

extern int myGlobal;

In fileB.c:

#include "fileA.h"
myGlobal = 1;

So this is how it works:

  • the variable lives in fileA.c
  • fileA.h tells the world that it exists, and what its type is (int)
  • fileB.c includes fileA.h so that the compiler knows about myGlobal before fileB.c tries to use it.

How to access Static Variable in other file without using extern in .h?

Now I want the get the updated value of Data_updated_u8 in second.c file

This is a design problem. If you have a local static variable declared at file scope in a .c file, then that variable is to be regarded as private. If your design is sound, other files should not need direct access to that variable. So this is where you should step back and consider your program design before anything else.

Or using pointers?

Bad idea, that's even worse than using global variables. You shouldn't expose private variables through pointers. Nor should you use global variables. Overall, you shouldn't design in tight coupling between several files by creating strange dependencies like this.

If you actually need to share this variable with other files, then the correct way is to write a setter/getter API function which you provide through the header file. Then set/get the data by value. (You might not even need to set it from the outside?)

Additionally, don't invent some local garage standard uint8. Use the international C language standard uint8_t from stdint.h.

data.h

#include <stdint.h>

uint8_t get_data (void);

void set_data (uint8_t val);

data.c

#include "data.h"

static uint8_t data;

uint8_t get_data (void) { return data; }

void set_data (uint8_t val) { data = val; }

Usage of extern variable in c files

When you are declaring a variable inside the function, it is a local variable.
It scope will be with in that function block. When you are using the extern key word it search for variable in the global section. So you have to declare the variable as a global variable.

#include "header.h"
int var = 4;
void func2()
{
printf("I am function 2\n");
return ;
}

Output will be ,

 I am function 2
I am in function 1 var = 4


Related Topics



Leave a reply



Submit