-1

I have been researching via Google and referencing http://www.cs.virginia.edu/~evans/cs216/guides/vsasm.html but I am wondering if anyone has any ideas on how to fix this not found error. The 'clear();' in extern "C" has a not found error. I hope this question is specific enough that it can be resolved. Any advice would be greatly appreciated.

This is the .cpp file.

extern "C" {
    void clear();
}

int main() {
    clear();
    return 1;
}

This is the .asm file.

.586              ;Target processor.  Use instructions for Pentium class machines
.MODEL FLAT, C    ;Use the flat memory model. Use C calling conventions
.STACK            ;Define a stack segment of 1KB (Not required for this example)
.DATA             ;Create a near data segment.  Local variables are declared after
              ;this directive (Not required for this example)
.CODE             ;Indicates the start of a code segment.

clear PROC
   xor eax, eax 
   xor ebx, ebx 
ret 
clear ENDP 
END

In Visual Studio I have the masm turned on in Build Customization Files. I also have exclude from build set to 'No'.

The command line property in Custom Build Step looks like this:

ml /Cx /coff clear.asm /link /SUBSYSTEM:console /out:go /entry:clear

When I build to solution/project/each file individually it succeeds but I can't figure out why the 'clear();' in the following --> has a green squiggly indicating a not found error.

extern "C" {
    void clear();
}
  • 1
    Is that a linker error or just Intellisense being confused? – Quentin Dec 24 '17 at 19:21
  • 1
    Please quote the exact error. – Richard Critten Dec 24 '17 at 19:29
  • @RichardCritten 'Function definition for 'clear' not found.' – Mercy Thomson Dec 24 '17 at 20:01
  • Your ml.exe command built clear.exe, a program that is not going to run. Instead add clear.asm to your project so it gets built to an .obj file and linked with your .cpp file code. The name of the function needs to be `_clear` to match the name decoration that the C compiler uses. – Hans Passant Dec 24 '17 at 20:25
  • [this answer](https://stackoverflow.com/questions/47957464/combining-assembly-and-c-in-visual-studio-2017-with-the-command-line-and-outpu/47957627#47957627) to a prior question worked. What are you trying to do differently this time? – rcgldr Dec 25 '17 at 16:18

1 Answers1

-1

Change the name in the asm file to "_clear" rather than just "clear".

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • What is the rationale behind this? – user7860670 Dec 24 '17 at 19:41
  • She's using `.MODEL FLAT, C` . The `C` changes the default convention to CDECL for any `PROC` statement that appears. With the CDECL convention MASM will prepend the `_` for you. As far as I know her issue isn't actually compiling/linking the code (that works). The issue is that VS doesn't recognize `clear` as defined in an external assemly module. I don't believe that VS (by itself) supports what this OP is trying to do - having the green squiggly line disapear – Michael Petch Dec 24 '17 at 19:50