Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

shizik

problem with a global in C

This topic is 6182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
oops forgot to say that
I do have the inclusion guard
initialy i though that was the problem also

any other ideas?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
try this

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

(always anonymous)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
You are quite correct. In C, ''const'' really means read-only, not constant. That''s why you can''t use it in this way.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.




Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!