0

I have these two different program where I want to access the static variable declared in program1 from program2.

Program1. ( /* file a.c */)

#include<stdio.h>    

static int a = 100; /* global static variable not visible outside this file.*/
int *b = &a; /* global int pointer, pointing to global static*/

Program2

#include<stdio.h>
/* file b.c */
extern int *b; /* only declaration, b is defined in other file.*/

int main()
{
        printf("%d\n",*b); /* dereferencing b will give the value of variable a in file a.c */
        return 0;
}

While I compile program1 , gcc a.c , no compilation error, but while I compile program2 ( gcc b.c) I am getting compilation error .

test_b.c:(.text+0x7): undefined reference to `b'
collect2: error: ld returned 1 exit status

Why there is compile error ? Here is the link of program static

Thanks in advance.

EDIT 1:

My intention to use static variable from other program. I thought every .c program must have main() function and only .h program have declaration , I am wrong at that point. So I remove main() function from a.c program and instead of compiling two different program separately , now I compile only once using gcc a.c b.c as per suggestion of Filip. Now it's working fine. Thanks all of you.

Community
  • 1
  • 1
bholanath
  • 1,699
  • 1
  • 22
  • 40
  • 1
    Why is this tagged C++? – Lightness Races in Orbit Feb 06 '14 at 15:01
  • You cannot have one program access variables from another program with the extern keyword. See this [SO question](http://stackoverflow.com/questions/856636/effects-of-the-extern-keyword-on-c-functions) that explains pretty well what the extern keyword is used for. – Jabberwocky Feb 06 '14 at 15:02

3 Answers3

1

You have to link against a.c while compiling b.c:

gcc a.c b.c

You can't expect the linker to magically find the C file where b is defined. extern means it is defined elsewhere - you have to say where. By compiling and linking with a.c, the linker can now find a declaration for b.

Of course, you can't have 2 main() functions.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
1

Well, your code already said it. b.cpp only has a declaration, not a definition, of the symbol in question.

Since these are clearly meant to be source files from two separate projects, I would suggest moving your definition to its own .cpp file which may then be shared between the two projects.

$ gcc a.c myIntPointerIsHere.c
$ gcc b.c myIntPointerIsHere.c

However, there are clearer ways to share code between two different projects.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I want to use the static variable of a.c program from b.c . your suggestion is to define my own program with these definition . will myIntPointerIsHere.c hold all the variables I need to use ? – bholanath Feb 06 '14 at 17:30
-1

The both modules contain the definition of main. It seems that the compiler did not include the first module in your project. Otherwise I think it would issue an error that main was redefined.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Look at his build commands. He is building one at a time, deliberately. It's deliberate because these are separate projects. You can tell from the multiple use of `int main()`. And from the opening statement: _"two different program"_ – Lightness Races in Orbit Feb 06 '14 at 15:02
  • @Lightness Races in Orbit He said nothing about building a project. He said about compilation of modules. They are different things. – Vlad from Moscow Feb 06 '14 at 15:05
  • He didn't say "modules". He has two _programs_. In fact, it is quite clear from the question what the intention is. Please go ahead and read it again now. – Lightness Races in Orbit Feb 06 '14 at 15:05
  • @ Lightness Races in Orbit No, it is totally unclear whether he is going to join these two modules in one project. – Vlad from Moscow Feb 06 '14 at 15:06
  • I refer you to my comment on your previous question, wherein I asked you to use SO notification syntax properly instead of mangling it with random spaces. It's _entirely_ clear, because he actually showed us his build commands. They're right there in the question, for your delectation. – Lightness Races in Orbit Feb 06 '14 at 15:07
  • @Lightness Races in Orbit No, it is unclear because if he is not going to join these modules together then the question has no any sense. – Vlad from Moscow Feb 06 '14 at 15:13
  • Code sharing between projects, more even more than one executable output within a single project, which is hardly unusual! It's not rocket science. But I'll leave you to your doubts. – Lightness Races in Orbit Feb 06 '14 at 15:31