Linker error when setting variables

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

Linker error when setting variables

Post 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.
Sorry for my bad English...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Linker error when setting variables

Post by Combuster »

Compiler + toolchain? Are you using a GCC Cross-Compiler?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Linker error when setting variables

Post 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?
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

Re: Linker error when setting variables

Post by Cmaranec »

I'm using MS Visual C++ 2005 to compile. Function memset is in C++ file
Sorry for my bad English...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Linker error when setting variables

Post by Combuster »

Wouldn't MSVC mangle that name if its defined in C++ context? Try declaring it as extern "C" void memset
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

Re: Linker error when setting variables

Post by Cmaranec »

Still doesn't work... I don't know why it doesn't appear when i remove my definition (initialization).
Sorry for my bad English...
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Linker error when setting variables

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

Re: Linker error when setting variables

Post 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)
Sorry for my bad English...
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Linker error when setting variables

Post 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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Linker error when setting variables

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

Re: Linker error when setting variables

Post 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);
Sorry for my bad English...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Linker error when setting variables

Post by Combuster »

still C++ linkage rather than C linkage there...
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Linker error when setting variables

Post 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);
Post Reply