Unresolved external called in c++, written in assembly
Hi all,
I'm a complete beginner to assembly and am trying to bootstrap my understanding by writing some assembly and linking it into a c++ program. So far I have a very simple project in VS Express 2008:
Code:
#include <tchar.h>
extern "C"
{
void clear();
}
int _tmain(int argc, _TCHAR* argv[])
{
clear();
};
I also have an assembly file:
Code:
.586
.MODEL FLAT, C
.CODE
clear PROC
xor eax, eax
xor ebx, ebx
ret
clear ENDP
END
which defines the clear() function (poached from an online example). The assembly is successfully compiled into an obj file. However when linking I get an unresolved external:
error LNK2019: unresolved external symbol _clear referenced in function _wmain
Am I missing some "export like" command, or is the clear() function getting mangled somehow, or something else? (NB I tried chaning the definition to _clear PROC but that didn't work either)
Thanks,
Andy
Re: Unresolved external called in c++, written in assembly
Try to add a
directive.
I'm not sure whether the langtype specification in the .MODEL directive will lead to an underscore prepended to the function name automatically, like it is expected by the C++ compiler. So you may or may not need to change the function name to _clear.
You are aware that this function won't do anything really useful in the C++ program? But I suppose that's not the point of this experiment anyway.
HTH
Re: Unresolved external called in c++, written in assembly
Hi Eri523,
Thanks for the input. Sadly my problem was far more basic [prepares to hang head in shame]. It was an issue of where the obj file was being created after compiling. The output was going to ..\X\Debug when the linker expected it in ..\X\X\Debug (same X!), hence no link errors but a missing external [hangs head in shame]. It was my fault for copying and pasting the custom build text from the web. The macros in VS Express (or the proj defaults) don't appear to be quite the same as the earlier version the example was based on. Took me a while to spot though with the X's being the same.
Anyway, the original code actually works fine. Sorry for wasting time. Also, I was aware that the code would do nothing. But hopefully not for long.
Found a nice article by David McClarnon during my searches though. Useful for other beginners:
http://www.developer.com/lang/other/...rogramming.htm
Andy
Re: Unresolved external called in c++, written in assembly
Fine that you got it working now! :)
Quote:
Originally Posted by
HormyAJP
It was an issue of where the obj file was being created after compiling. The output was going to ..\X\Debug when the linker expected it in ..\X\X\Debug (same X!), hence no link errors but a missing external [...].
BTW, an unresolved external is a linker error. But I, too, would have expected an error complaining about the missing .obj file if it's specified in the linker command line but not found. (I must admit, though, that I never had a closer look at the linker command line generated by the IDE yet. There just has been an occasion where I had to do that for the C++ compiler's command line so far.)
I also tried to convert a project from VC++ 2008 to 2010 (no assembly code, but a C++ library project of some dozen files) some weeks ago and couldn't really get it right. But that wasn't important, and so I put it aside and maybe I'll start over again once I have some more spare time.
The article you linked to is interesting, and in particular it doesn't delve too deep into details for beginners. But I wonder somehow why the author suggests to separately download the assembler. I also have a separately downloaded MASM 6.14 (don't know which version is behind the link in the article), but VC++ 2010 came packaged with a MASM 10.0, even in the Express Edition. I don't know, however, whether a MASM was packaged with your VC++ 2008 or the version current when the article was written (march 2005).
EDIT: According to an earlier thread here, at least VS 2008 apparently comes with a MASM.