سؤال

I have no idea why the following gives me: *"error LNK2001: unresolved external symbol 'struct Win32Vars_t win32' (?win32@@3UWin32Vars_t@@A)"* If I start a new project and create this header file it compiles fine. I'm also creating another struct similar to this and it also compiles just fine (although instead of "extern -variablename-" it's a static. Should this not work?

win_local.h

#ifndef __WIN_LOCAL_H__
#define __WIN_LOCAL_H__

#include <windows.h>

void System_CreateConsole(void);

typedef struct {

    HWND hWnd;
    HINSTANCE hInstance;

} Win32Vars_t;

extern Win32Vars_t win32;

#endif

What it with the cryptic @@3U message in parenthesis?

Sorry for creating yet another post about unresolved externals. I did do some reading before hand and tried various things. From what I read, this is C way of doing things but should still work in C++. And yes, .h file is set to compile C/C++.

هل كانت مفيدة؟

المحلول

That's because an "extern" variable is declared but not defined: you're only telling the compiler that somewhere else in your code, there's a "Win32Vars_t win32" variable and it can use it.

With only the "extern", no symbol is allocated, that's why you're getting an unresolved symbol.

That means you must define it somewhere else in your code. The usual pattern is to use an "extern" in header file, so every other file including that header will be able to "see" the variable and in a source file (something.c) define the variable, something like this:

foo.h

#ifndef FOO_H
#define FOO_H

extern int foo;

#endif

.c

#include "foo.h"

int foo;

You can find more information about external variables here: http://en.wikipedia.org/wiki/External_variable

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top