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.

2
  • 1
    Why is this tagged C++? Commented Feb 6, 2014 at 15:01
  • You cannot have one program access variables from another program with the extern keyword. See this SO question that explains pretty well what the extern keyword is used for. Commented Feb 6, 2014 at 15:02

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The main() definitions would clash.
@ Filipe , thanks. I remove main from a.c and now it is working.
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.

1 Comment

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 ?
-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.

7 Comments

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 He said nothing about building a project. He said about compilation of modules. They are different things.
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 No, it is totally unclear whether he is going to join these two modules in one project.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.