Multiple definitions... what should I be looking for??
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Multiple definitions... what should I be looking for??
With the eradication of the problem in my previous thread, I ventured further with my little program...
... now when I make my program I get the following error:
george@George:~/George Lair/030506$ make
g++ -c main.cpp
g++ -c fight.cpp
g++ -c gSDL.cpp
g++ -g main.o fight.o gSDL.o -Wall `sdl-config --cflags --libs` -lSDL -o test031206
fight.o:\(.bss+0x8): multiple definition of `yloc'
main.o:\(.bss+0x0): first defined here
fight.o:\(.bss+0xc): multiple definition of `xloc'
main.o:\(.bss+0x4): first defined here
fight.o:\(.bss+0x10): multiple definition of `screen'
main.o:\(.bss+0x8): first defined here
gSDL.o:\(.bss+0x0): multiple definition of `yloc'
main.o:\(.bss+0x0): first defined here
gSDL.o:\(.bss+0x4): multiple definition of `xloc'
main.o:\(.bss+0x4): first defined here
gSDL.o:\(.bss+0x8): multiple definition of `screen'
main.o:\(.bss+0x8): first defined here
collect2: ld returned 1 exit status
make: *** [test031206] Error 1
george@George:~/George Lair/030506$
At first I thought it was being confused with some other defintion of x and y that were arguements to functions ( ex: int move_left(int x, int y)). But I seem to have crossed that out for now.(though I still would like to know if I would have conflicts with same variables but within different locations.. such as one int x being global [in the headerfile.h] and the other being in a function arguement parameter. Any answer to this question would be greatly appreciated) Anyways, what exactly should I be looking for... again I do not want a direct answer if at all possible, but just a hint in the right direction. I have uploaded the files in pntbalg.shackspace.com/GeorgeLair/032206. Thank you in advance.
PS: Sorry for lack of documentation and the absence of helpfull comments... once I have 10 minutes to burn I'll spend it commenting in the code, maybe add a readme.
I think this error describes very well what it is: You have multiple definitions of those variables. I don't know your code, but let's take the variable "yloc"; I guess you defined it in "fight.h" (or so) at global scope? If this is true, then I think further, that you included this file ("fight.h")in "fight.cpp" as well as in "main.cpp" ==> multiple definitions!
And the other thing: You won't get any conflicts, if you use the same name for variables in different/nested scopes. If you do so, then the most inner variable will be used.
It's strange how it says multiple def in fight.o when it is never even called. Only in headerfiles.h does it declare xloc and yloc globaly. Here is part of my headerfiles.h
#ifndef HEADERFILES_H
#define HEADERFILES_H
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <SDL/SDL.h>
SDL_Surface *screen;
int xloc, yloc;
void fightque();
int fight();
int fightstart(int who, int player, int mob);
void display_bmp(char *file_name, int x2, int y2);
using namespace std;
#endif
Is there anything not usualy done in the ^above^ code?
Ok, I had a little look at your code and it is like I guessed in my first post: You have defined those variables globally in "headerfiles.h" and included this file in "main.cpp" as well as in other files like "fight.cpp". You have to know, that the "include"-directive really includes the whole given file, it's just like you would copy the file's content at that position. With that in mind your error should be clear, or isn't it? -You have included the hederfile to "main.cpp" ==> variables are defined there; but you also included it i.e. in "fight.cpp" ==> variables are also defined there ==> multiple definitions. You said you don't want a direct solution, so I won't give it to you, but I hope this helps.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.