Im having a very strange problem that I can't explain =/

this is what the compiler is saying:

1>------ Build started: Project: 3d, Configuration: Debug Win32 ------
1>Compiling...
1>RENDER.CPP
1>Generating Code...
1>Compiling...
1>GEOM.CPP
1>MAIN.CPP
1>MOVEMENT.CPP
1>Generating Code...
1>Linking...
1>MAIN.obj : error LNK2005: "struct OBJECT * obj" (?obj@@3PAUOBJECT@@A) already defined in GEOM.obj
1>MAIN.obj : error LNK2005: "struct CAMERA camera" (?camera@@3UCAMERA@@A) already defined in GEOM.obj
1>MOVEMENT.obj : error LNK2005: "struct OBJECT * obj" (?obj@@3PAUOBJECT@@A) already defined in GEOM.obj
1>MOVEMENT.obj : error LNK2005: "struct CAMERA camera" (?camera@@3UCAMERA@@A) already defined in GEOM.obj
1>RENDER.obj : error LNK2005: "struct OBJECT * obj" (?obj@@3PAUOBJECT@@A) already defined in GEOM.obj
1>RENDER.obj : error LNK2005: "struct CAMERA camera" (?camera@@3UCAMERA@@A) already defined in GEOM.obj
1>.\Debug/3d.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Creating browse information file...
1>Microsoft Browse Information Maintenance Utility Version 9.00.30729
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Build log was saved at "file://c:\Users\Nicolas\Desktop\3DRendering 3\Debug\BuildLog.htm"
1>3d - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This is a link to the code: my appologies for the 30 seconds =(
VS2008
http://rapidshare.com/files/198510158/3D.zip.html

In my project I need to have these two things, available to all the other classes and routines:
CAMERA camera;
OBJECT *obj;

so I put them in a file MAIN.H that is included at the begining of all the other header files for the other classes.

I have put these two objects inside the ifndef tags like this:

#ifndef _MAIN_H
#define _MAIN_H
CAMERA camera;
OBJECT *obj;
#endif

anywho, bottom line is that I need these two things available from anywhere in the project...how can I accomplish this or fix this?

Thanks, Nick

Recommended Answers

All 6 Replies

I believe you can put this in main.h:

extern CAMARA camera;
extern OBJECT *obj;  // should probably be a longer name for a global

and then in main.c you just put

CAMERA camera;
OBJECT *obj;

sorry...but that doesn't really make sense to me.

why would I have to declare it again in main.cpp?

why can't I just declare two variables in a header file, and include that header file in all the other files that need to access that same object?

How is the linker supposed to know about the object/variables?

How is the linker supposed to know about the object/variables?

I don't really get this...I guess I better look up the compiling process...I have never done it any other way than hitting play on VC++

but this still doesnt explain: why is it giving an error for the two objects/variables, but not all the structs/constants that I have in there?

You are still mixing declarations and definitions up. Each variable in C++ can have multiple (identical) declarations but should have one and only one definition. This one definition rule was broken.
See http://en.wikipedia.org/wiki/One_Definition_Rule

There are DEFINITIONS of global variables:

CAMERA camera;	// Allocate static memory for these variables
OBJECT *obj;	// and initialize them by default.

There are DECLARATIONS of these variables:

extern CAMERA camera;	// Means: I have two global
extern OBJECT *obj;	// variables defined elsewhere

You place definitions in main.h and now you have these vars definitions in every module included main.h.

Now come back to nucleon's post...

OOOOOOKay! I stuck this is GEOM.CPP in the global section,

CAMERA camera;
OBJECT *obj;

and this in MAIN.H below the structs

extern CAMERA camera;
extern OBJECT *obj;

now it compiles. I just have to tighten all the nuts and bolts and check if it all works ok.

thanks again! If this works properly this thing should be able to handle meshes now, hopefully not be so dreadfully slow!@#
screenshot of 1.0...lol
<img src="http://i103.photobucket.com/albums/m140/nicksespace/soft_rend.jpg">

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.