Skip to main content
gamedev.net
🔒 Locked

problem with "extern"

Started by _dogo Sep 23, 2006 at 5:24 PM 17 replies 2.2k views
Original Post
_dogo
_dogo
hi, i have a project, wich i have to modify. It has planty of external functions and variables. I wanted to use a new one, and declared in the same place. used in the same place like other external variables - it does not work, in fact i cannot link mí project, it says "unresolved external". what can be the problem? i just declared it in one of my files, and want to use in an other, and or that i declared as extern in that file. it should work... any idea? thanks a lot.
credo
JohnBolton
JohnBolton
This statement:
    extern int x;
simply tells the compiler that there is a variable somewhere named x. If you do not actually have a variable x somewhere (and accessible) then the linker complains that you have an "unresolved external". In other words, then "external" variable x that you declared is "unresolved" (meaning that it doesn't exist).

Now, I'm only guessing that this is the problem because it is hard to tell what you are talking about if you don't provide any code.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Darobat
Darobat
// in blah.hextern int x;// in blah.cppint x;

If you don't have the part in the cpp file, you'll get the unresolved external error.
--------------------C++ Home - Check it out!Lol... - Amazing video
_dogo
_dogo
Thanks,
but i HAVE it declared. Its declared in one of my cpp files, and i want to use it in another. (Actually they're the mouse position variables. They are declared and used in tha main file, where WindowProc can change it, and other parts want to use them.)
So
POINT ptCurrentMousePosit;
declares it
and
extern POINT ptCurrentMousePosit;
are in other cpp files.

credo
GPX
GPX
You are'nt giving us enough information...
Why not just paste both files contents?
Yoni Levy.
IFooBar
IFooBar
the line:

POINT ptCurrentMousePosit;

must be present once and once only in some *cpp* file.

the line:

extern POINT ptCurrentMousePosit;

can be there as many times as you want. Usually you put this in the *header* and include the header where you want the variable to be available.

[size=2]aliak.net
_dogo
_dogo
Quote:
Original post by GPX
You are'nt giving us enough information...
Why not just paste both files contents?


Because only one of the files is 1000 lines long...

I think i know the rules of using extern, and there's many external vars in the projects. so everything seems fine. and is not.

My var is not declared in a header, its in a cpp file, but it must not count...

Have any idea, what other can cause this error?
credo
Ezbez
Ezbez
Well, you can always copy and paste the relevant code. If you're just typing it out, chances are you've missed a typo or something in your original code.

Also, is the variable a global variable, not a local? If it's declared inside of, say, main(), it won't be accessible through extern.
Darobat
Darobat
Quote:
Original post by _dogo
So
POINT ptCurrentMousePosit;
declares it
and
extern POINT ptCurrentMousePosit;
are in other cpp files.

The extern decleration should be in a .h file. You rarely #include a .cpp files, so the extern decleration isn't being found, hence the linker error.

// example.h#IFNDEF _EXAMPLE_H_#DEFINE _EXAMPLE_H_extern int blah;#ENDIF// example.cppint blah;// main.cpp#include "example.h"// Now you can use the variable blah without any problems.
--------------------C++ Home - Check it out!Lol... - Amazing video
zrak
zrak
It could be that the problem is in the order in which the files are being compiled and linked.
I once had a similar problem with extern, but I just copied declaration to another file and it worked :) - it just wasn't an important project to mess with it too much.
_dogo
_dogo
Hi. thanks for replies. I dont know why, but the only way i could make it linked is that the header contains the declaration (without extern).
But.
Its cpp can use it correctly, but the other cpp (wich call it as extern, or even without extern, its all the same...) cannot use it. the vars value in this other cpp is always zero. Why?
[Other way it cannot even be linked. If i declared it as extern in header, and declared it in its cpp, then other cpps (wich have this header included) cannot see it, it dies with "unresolved external". thats why i tried the mentioned way...]
credo
_dogo
_dogo
still doesnt work... if u have any experinece, pls, share it...
credo
discman1028
discman1028
post. the. code.
--== discman1028 ==--
_dogo
_dogo
ok, i try...
attempt 1: without extern.
in hook.h:

#ifndef _EXTERNMOUSE_H_#define _EXTERNMOUSE_H_static POINT ptCurrentMousePosit;#endif 

in hook.cpp:
#include "hook.h"(...)LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){(...)case WM_MOUSEMOVE:	         	{				ptCurrentMousePosit.x = LOWORD (lParam);			ptCurrentMousePosit.y = HIWORD (lParam);             		}}

writing in a file the value of mouse position shows that it is correct.
In any other cpp files the ptCurrentMousePosit values are zero. Why?

attempt 2, using extern
hook.h:
#ifndef _EXTERNMOUSE_H_#define _EXTERNMOUSE_H_extern POINT ptCurrentMousePosit;#endif 


in hook.cpp:
#include "hook.h"POINT ptCurrentMousePosit;(...)LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){(...)case WM_MOUSEMOVE:	         	{				ptCurrentMousePosit.x = LOWORD (lParam);			ptCurrentMousePosit.y = HIWORD (lParam);             		}}

other cpp file:
#include "hook.h"extern POINT ptCurrentMousePosit;

and it dies with unresolved external symbol ptCurrentMousePosit.
Any idea?
Thanks
credo
Paradigm Shifter
Paradigm Shifter
I hope you are not trying to extern a static variable... many bad things may happen if you do.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
JBourrie
JBourrie
First Version:
Consider #include to be a copy/paste of the header file into the position of the #include statement. Because of this, you are really making a different static version of the variable for each .cpp file.

Second Version:
In "other cpp file" you don't have to declare:
extern POINT ptCurrentMousePosit;
If you include hook.h it is already declared.

This may be where the error is coming from.
ApochPiQ
ApochPiQ
Multiple extern int foo; declarations do not cause problems - they are simply ignored (unless you do something silly like declare foo as an int once and, say, bool somewhere else). But it is wasteful.

Don't use static if you want a variable to be accessible outside the file where it is declared. Always remember to RTFM before using keywords.


If you simply do this:
// Header.hint foo;// Code.cpp#include "Header.h"// ...foo = 3;// Code2.cpp#include "Header.h"// ...cout << foo;


This will not work. An #include literally takes the text of one file and pastes it into another before running the compiler - so in essence, your code is this:

// Code.cppint foo;// ...foo = 3;// Code2.cppint foo;// ...cout << foo;


You have created two foo variables, one in Code.cpp and one in Code2.cpp.


The correct solution is:

//Header.hextern int foo;// Code.cpp#include "Header.h"// ...int foo;// ...foo = 3;// Code2.cpp#include "Header.h"// ...cout << foo;


If you set up a minimal, blank project and try that out, I think you'll find it works without errors. If that doesn't work in your own code, there is something about it which you have not stated - and that information is vital to solving your problem. In that case you will need to post your code.
_dogo
_dogo
there is still unresolved external error this way.
I cannot post all my code, its about 10-12 thousands lines... iva just tried to copy the relevant lines here (above), what else could be important?
credo

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.