Page 1 of 1

Linker error when setting variables

Posted: Tue Apr 14, 2009 8:02 am
by Cmaranec
Hello again, i have a little problem - i'm woking on simple module manager for my text operating system and linker says that Unresolved external symbol _memset has been found here.

It appears only if i try to do this:

Code: Select all

static moduleinfo modules[MAX_MODULES] = {
	{"Calculator","calc","Calculator v0.1 by Kennny",&modulehandlers::test_calc},
	{"Testing Game","testgam","First Testing Game by Kennny",&modulehandlers::empty_module_handler},
	{"External Module","ext","External module test",&modulehandlers::empty_module_handler},
	{NULL,NULL,NULL,&modulehandlers::empty_module_handler}
};
- if i remove this, linker says nothing and everything (except this) works fine. :roll:

Edit: function memset is declared in other file, which is included from there.

Sorry for my bad english.

Re: Linker error when setting variables

Posted: Tue Apr 14, 2009 9:32 am
by Combuster
Compiler + toolchain? Are you using a GCC Cross-Compiler?

Re: Linker error when setting variables

Posted: Tue Apr 14, 2009 9:38 am
by frank
Is memset in an assembly file? If it is you might try adding an _ to it. If its not then your going to have to provide some more information about what compiler you are using (DJGPP Cygwin MingW if on Windows) and what OS you are compiling on Windows, Linux?

Re: Linker error when setting variables

Posted: Tue Apr 14, 2009 10:05 am
by Cmaranec
I'm using MS Visual C++ 2005 to compile. Function memset is in C++ file

Re: Linker error when setting variables

Posted: Tue Apr 14, 2009 10:37 am
by Combuster
Wouldn't MSVC mangle that name if its defined in C++ context? Try declaring it as extern "C" void memset

Re: Linker error when setting variables

Posted: Tue Apr 14, 2009 10:53 am
by Cmaranec
Still doesn't work... I don't know why it doesn't appear when i remove my definition (initialization).

Re: Linker error when setting variables

Posted: Tue Apr 14, 2009 5:09 pm
by neon
MSVC does indeed mangle the names if they are defined as C++ symbols.

Anyways, try putting extern void *memset(void*, int, size_t) at the top of your file. If it is still occurring, it is possible that the compiler is calling the intrinsic form of the function instead which can be fixed by putting #pragma function(memset) before you use it.

Re: Linker error when setting variables

Posted: Sat Apr 18, 2009 1:29 pm
by Cmaranec
isn't work... i can't understand, why it appears only if i try to initialize my struct.

Czechian would say "No to mÄ› poser" :D (don't try to translate)

Re: Linker error when setting variables

Posted: Sat Apr 18, 2009 7:16 pm
by frank
If you understand assembly language then you could try looking at the asm version of what gets compiled. It's under output files in the project settings. Then you could see what its doing with the memset call.

Re: Linker error when setting variables

Posted: Sat Apr 18, 2009 9:46 pm
by neon
I know your declaration of memset has extern C, but also does its definition? (This is important as you are building it as a C++ program)

Also, posting the *exact* error message might be helpful as it should contain the symbolic name that it is trying to call.

Re: Linker error when setting variables

Posted: Sun Apr 19, 2009 1:50 am
by Cmaranec
Error message:
1>module.obj : error LNK2019: unresolved external symbol _memset referenced in function "void __cdecl `dynamic initializer for 'modules''(void)" (??__Emodules@@YAXXZ)
Function *memset: (string.cpp)

Code: Select all

void *memset(void *dest, char val, size_t count)
{
    unsigned char *temp = (unsigned char *)dest;
	for( ; count != 0; count--, temp[count] = val);
	return dest;
}
extern declaration: (string.h)

Code: Select all

extern void *memset(void *dest, char val, size_t count);

Re: Linker error when setting variables

Posted: Sun Apr 19, 2009 3:16 am
by Combuster
still C++ linkage rather than C linkage there...

Re: Linker error when setting variables

Posted: Sun Apr 19, 2009 9:27 am
by frank
Cmaranec wrote:extern declaration: (string.h)
Code:
extern void *memset(void *dest, char val, size_t count);
It needs to be

Code: Select all

extern "C" void *memset(void *dest, char val, size_t count);