🔒 Locked
problem with "extern"
Advertisement
Original Post
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.
This statement:
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.
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.
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.
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.
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?
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.
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.
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.
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...]
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...]
post. the. code.
ok, i try...
attempt 1: without extern.
in hook.h:
in hook.cpp:
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:
in hook.cpp:
other cpp file:
and it dies with unresolved external symbol ptCurrentMousePosit.
Any idea?
Thanks
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
I hope you are not trying to extern a static variable... many bad things may happen if you do.
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.
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.
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:
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:
You have created two foo variables, one in Code.cpp and one in Code2.cpp.
The correct solution is:
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement