-1

There is a very big C file, which defined a lot of strings and used locally. I would like to do access these strings from a C++ file, but using extern "C" does not help.

The C file look like this: data.c

#include <stdio.h>

static char* str = "string\n";

void p() {
   printf(str);
}

and the c++ file look like this:

#include <iostream>

extern "C" {
   extern char* str;
   extern void p();
};

int main(int argc, char* argv[]) {
   p();
   std::cout << str;
   return 0;
}

I am using VS2013, the compile gives error

error LNK2001: unresolved external symbol _str
fatal error LNK1120: 1 unresolved externals

Calling the functions defined in C file has no problem.

Is it even possible to access the variables in C from C++? And how to do it correctly?

savram
  • 500
  • 4
  • 18
Tiger Hwang
  • 300
  • 1
  • 5
  • 16
  • 8
    You can't. It's `static` – StoryTeller - Unslander Monica Oct 25 '17 at 08:17
  • 1
    Some more info about static [here](https://stackoverflow.com/questions/7259830/why-and-when-to-use-static-structures-in-c-programming/7259892#7259892). Short version: `static` used in this context limits the variable's visibility to the current "translation unit" (roughly the file in which it's defined). – fvu Oct 25 '17 at 08:19
  • I was hoping that "extern" can break the "static" ruler :( – Tiger Hwang Oct 25 '17 at 08:21
  • 1
    @Tiger Hwang Remove the static specifier in the C file. – Vlad from Moscow Oct 25 '17 at 08:22
  • 2
    @TigerHwang - Doesn't work like that I'm afraid. And good that it doesn't, or proper encapsulation would be impossible. – StoryTeller - Unslander Monica Oct 25 '17 at 08:22
  • 1
    `extern` cannot affect the C file as it is only used in the C++ file. – Gerhardh Oct 25 '17 at 08:24
  • I can not remove the "static", there are many of them and the file is still build in the project – Tiger Hwang Oct 25 '17 at 08:24
  • you can use regex to edit all of them no? – Adalcar Oct 25 '17 at 08:25
  • Actually it's generated by a tool from XML, maybe I can try change the generator, and hope my colleges will not angry at me :) – Tiger Hwang Oct 25 '17 at 08:29
  • @TigerHwang Well is there any reason those variables have to be declared in C? if it is a large generated file shouldn't it be something like a CSV? – Adalcar Oct 25 '17 at 08:34
  • 2
    The `static` modifier implies that your intention is to keep the field private. I would certainly be angry if a junior member of the team decided to make all fields public so that they can be accessed from anywhere. – vgru Oct 25 '17 at 08:36
  • 1
    @TigerHwang But wait, do all the strings have the `void p()` that will print them out, or is it only in your example? Basically is there a need to access those strings themselves? – Adalcar Oct 25 '17 at 08:42
  • 2
    What problem are you __actually__ trying to solve. Tell us more about the context. This smells like an [XY Problem](http://xyproblem.info/). – Jabberwocky Oct 25 '17 at 08:46
  • There are a lot of such C files generated, which contain many strings as arrays and build into a lib. They also contain some functions using these strings. The functions are not static, they are exported in a header file. I want to use the string in my C++ code, but not the functions. If this does not work, then I have to figure out another solution, maybe generate some functions to export the strings. – Tiger Hwang Oct 25 '17 at 09:22

2 Answers2

3

when you are using static keyword in your file. it indicates that the variable prefixed with static can't be exported in another file. you can keep that variable as non static and then it is possible to access those variables in another files as well.

you can refer the following article. https://cboard.cprogramming.com/cplusplus-programming/102259-global-variable-access-c-cplusplus.html

Jayesh Vyas
  • 1,145
  • 3
  • 15
  • 35
2

In C, the static statement implies that the symbol should NOT be exported.

Thus, it is possible to access those variables simply by removing that keyword.

This article might help you understand better the symbol visibility concept

Apart from that, while C might have trouble using mangled C++ symbols, the opposite poses absolutely no compatibility issue.

Adalcar
  • 1,458
  • 11
  • 26