problem with a global in C

Started by
7 comments, last by shizik 22 years, 5 months ago
basicaly i get the following linker error: header.o(.rodata+0x0): multiple definition of `MAX_LEN'' main.o(.rodata+0x0): first defined here collect2: ld returned 1 exit status this works fine with c++ but not with c, what is the correct way to do this in c? project is made up of main.c - main(), uses MAX_LEN, includes header.h header.h - contains a global constant declared: const int MAX_LEN = 50; header.c - some functions that use MAX_LEN, includes header.h i compile my project in unix with make: Makefile: a.out : main.c header.h header.c cc -o a.out main.o header.o main.o : main.c header.h cc -c main.c header.o : header.c header.h cc -c header.c
Advertisement
Have you got an inclusion guard in your header file?

e.g.

#ifndef HEADER_H
#define HEADER_H

// constants, types, etc.

#endif

This should solve your problem.

oops forgot to say that
I do have the inclusion guard
initialy i though that was the problem also

any other ideas?
try this

#ifndef _MAX_LINE_
#define _MAX_LINE_
const int MAX_LEN = 50;
#endif

(always anonymous)
I''m pretty sure that straight C doesn''t treat const quite the same way. Either make it a pure define.
#define MAX_LEN 50
or declare it as an extern and define it in main.
header file
extern int MAX_LEN
main file
int MAX_LEN = 50;

C++ deals with const the way you''re trying to use it, I''m pretty sure C does not.

Hope that helps

Brad
You are quite correct. In C, ''const'' really means read-only, not constant. That''s why you can''t use it in this way.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
If I remember correctly, const is an addition to C++ and not to C, therefore if you try to compile a C program using const it wont work. I think the only way to declare constants in C is to use #define. I am pretty sure this is right but dont quote me on it :-p
thanks everybody
const in C means that the variable maintains it''s value throughout the scope that it''s declared in - and typically that scope is global. So for example it can be used to declare the number of elements in an array.
const int length = 10;

int main(void)
{
int i;
int array[length];
for (i = 0; i < length; i++)
{
array = i;
}
}
A define can also be used for this same purpose - but typechecking for a define isn''t typically as strong as it is for a declared type - so the recommendation is to use a const when you want to use a variable that never changes during runtime.

If you want to have a local variable that can be altered and yet maintain it''s value across calls to a function - you want to use the ''static'' keyword. (if you''re already aware of this consider it a refresher or correct me if I''m mistaken :-).

for example

void myfunc()
{
static int counter = 0;
counter++;
}

each call to myfunc will increment counter - even though it would appear to reset it the compiler takes care that it doesn''t happen.

Outside the scope of a function the ''static'' keyword will limit the scope of the variable to the current ''translational unit'' - that it - the current module or source file "myfile.c"

I think a judicious use of ''extern'' will solve your problem.

For a quick rundown on these kinds of C issue check out Ben Pfaff''s web site - http://www.msu.edu/~pfaffben/ - or the comp.lang.c faq - http://www.eskimo.com/~scs/C-faq/top.html

Both urls are very helpful.




This topic is closed to new replies.

Advertisement