1
votes

Problem: A variable 'VarOriginal' is defined in source C file under pragma say 'parameterX' and declared as an extern variable in a header file under the same pragma 'parameterX'. There is a process using it in which it is known that the variable is shown declared in some other header file where this variable is not present at all.

As a Workaround:I declared one more different variable 'VarNew' before the pragma 'parameterX' in the header file and similarly defined the variable 'VarNew' before the line where 'VarOriginal' was defined. And it all worked.

Header file: header_file_with_problem.h

#ifndef HEADER_FILE_WITH_PROBLEM_H

#define HEADER_FILE_WITH_PROBLEM_H

#include "ABC.h"

declare variable 'VarNew' here <------

#pragma BSS(".parameterX")

extern int VarOriginal;

#pragma BSS(DEFAULT_SECTION_BSS)

Source C File:

#define  HEADER_FILE_WITH_PROBLEM_C

#include "XYZ.h"

#include "header_file_with_problem.h"

declare variable 'VarNew' here <------

#pragma BSS(".parameterX")

int VarOriginal;

#pragma BSS(DEFAULT_SECTION_BSS)

But I am not able to understand why the problem was coming earlier. Why was the linker not able to find the definition of 'VarOriginal' in the source file and now it is able to do so, after declaring another variable before 'VarOriginal' itself?

Also, this problem is not with all source and header files present in the folder, but only a few of them.

1
You should show a minimal example. You don't even mention what pragma. That is relevant info. We are not psychic, we're just humanssehe
Also, please edit your post to use meaningful variable names. A variable "A"... and pragma say "X" and a "B" before "A" and "J" and say "C" and maybe "Y" or "Z" reads like nonsense and makes it very hard to understand your actual problem. Also remember that we can't see your monitor from here; the only information we have is what you provide us. The more clearly and concisely you can state your question, the more likely you'll get help quickly.Ken White
How did pragma even creep in here at all? The question was about some variable that was extern.Pete Wilson
@PeteWilson Probably because the OP is using #pragma once instead of regular multiple inclusion guardsPraetorian
Oh, yes. That must be it. But that would cause the problem he's seeing, right?Pete Wilson

1 Answers

3
votes

I don't see anything defined in your source file. The

extern int VarOriginal;

is still a non-defining declaration, regardless of where you put it (source file or header file). There's not a single variable definition in your code sample, so no wonder the linker is complaining about missing definitions.

In order to define a variable in your C file you need to do either

int VarOriginal; /* no `extern` !!! */

or add an explicit initializer

extern int VarOriginal = 0; /* definition */

A mere

extern int VarOriginal; /* not a definition!!! */

that you seem to have there now (according to what you posted) is not a definition.