Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
I have code like this
void* __cdecl memcpy( void* dest, const void* src, size_t count );
char* __cdecl strcpy( char* strDest, const char* strSource );

#pragma intrinsic(memcpy)
void* CCompiler::memcpy( void* dest, const void* src, size_t count )
{
  return ::memcpy( dest, src, count );
}

#pragma intrinsic(strcpy)
char* CCompiler::strcpy( char* strDest, const char* strSrouce )
{
  return ::strcpy( strDest, strSource );
}

#pragma function(memcpy)
#pragma function(strcpy)


Whereas strcpy is perfectly happy I get a LNK2019 unresolved external symbol _memcpy referenced in function "public: void * __cdecl nsCompiler::CCompiler::memcpy(void*,void const*,unsigned int)"
when I try to build the this into a Dll.
FYI nsCompiler::CCompiler is exported and I know that's done right.

No warning that memcpy intrinsic is not available despite warning level 4 and no compile error claiming it can't generate nsCompiler::CCompiler::memcpy

Any help, known bug pointers, or workarounds would be much appreciated.
Posted
Updated 4-Feb-13 3:23am
v2
Comments
nv3 4-Feb-13 10:04am    
Just a guess into the blue: The intrinsic pragma documentation says: "... The compiler may call the function and not replace the function call with inline instructions, if it will result in better performance."

So in case of memcpy the compiler might decide to call the library function and not generate inline code (as it probably did for strcpy). Now, if you haven set the linker settings correctly to link in the runtime library, that might produce the unresolved external.
Matthew Faithfull 4-Feb-13 10:15am    
Thanks that might just be it although I have no idea why it would consider calling an external library function in another DLL more efficient. Linking the runtime would probably solve it if I wasn't implementing the runtime rather than using it in this case. You would think it would flag a level 4 warning at least.
nv3 4-Feb-13 11:41am    
Memcpy is a function that benefits from heavy optimization, while strcpy does not. That might be the reason why the compiler generally prefers calling memcpy, while using inline code for strcpy.

Guessing why they didn't issue a warning: Compiler developers might have assumed that one normally links with the runtime. So a normal user won't even notice the difference except for the performance gain.
Matthew Faithfull 4-Feb-13 11:58am    
Yes, I think you're right and it's an undocumented behaviour change between compiler versions as well. I need to investigate further to see how far it goes back but I'm reasonably confident VS2005 didn't do that. It's also the only intrinsic causing this issue despite that I've done the same thing with all 27 architecture independent ones. Anyway thanks for your input. I'll have to make a special case like _outpw where they changed the signature between VC8 and VC9.
If you post you explanation as a solution I'll accept it.
nv3 4-Feb-13 13:55pm    
You're most welcome, Matthew!

1 solution

Just a guess into the blue: The intrinsic pragma documentation says: "... The compiler may call the function and not replace the function call with inline instructions, if it will result in better performance." So in case of memcpy the compiler might decide to call the library function and not generate inline code (as it probably did for strcpy). Now, if you haven't set the linker settings correctly to link in the runtime library, that might produce the unresolved external.
 
Share this answer
 
Comments
Philip Stuyck 4-Feb-13 16:07pm    
since this is the solution i give you 5
nv3 4-Feb-13 16:54pm    
Thanks Philip!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900