Re: Multiple declaration problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Shriramana Sharma wrote:

> I found a different solution, which is by declaring the variables 
> static. Find attached solutions.
> 
> But still the C parser (or what is it?) did throw up an error with the
> const variable, which C++ did not.

In C, "const" is only valid for pointer and array targets, not for
primitive types or structures.

> @Andre: Is there any I should not declare a variable in a header file if
> I want that variable to be visible to many cpp files? I can avoid
> writing many "extern" lines by using static.

Presumably, you mean "define a variable"; a declaration (e.g.
"extern int foo") merely tells the compiler that the variable exists,
and its type, while a definition actually creates the variable.

If you define a variable in a header file, each object file will get a
separate copy of the variable. All of the variables will have the same
name, but they will be different variables; modifying one won't affect
any of the others.

This is fine for a constant (unless it occupies a lot of memory, in
which case you don't want to include a separate copy in each object
file), but not for a variable.

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux