CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Posts
    10

    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

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Unresolved external called in c++, written in assembly

    Try to add a

    Code:
       PUBLIC clear
    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
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Feb 2010
    Posts
    10

    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

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Unresolved external called in c++, written in assembly

    Fine that you got it working now!

    Quote Originally Posted by HormyAJP View Post
    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.
    Last edited by Eri523; November 15th, 2010 at 07:25 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured