1

I am trying to combine these languages for testing purposes. Does anyone know why, after building the project the clear function cannot be found when the .asm file is in the source folder. The following images shown below should explain what I am asking and I will edit further.

Visual Studio with an assembly and C++ file

this last photo shows the properties from my .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 
  • Maybe you meant to use`extern "C" void clear();` instead without the curly braces? – Michael Petch Dec 24 '17 at 01:51
  • @MichaelPetch I changed it to that but clear() still has a green squiggly underneath it I also added another picture just now. – Mercy Thomson Dec 24 '17 at 01:53
  • 1
    I'm not sure VS 2017 will parse MASM source files to determine if and identifier has been made global and available in _C_. Does the code at least compile and link? – Michael Petch Dec 24 '17 at 01:55
  • @MichaelPetch I copied the ml.exe to a different location in my account folder. The build fails because clear cannot be found. I am referencing this: http://www.cs.virginia.edu/~evans/cs216/guides/vsasm.html – Mercy Thomson Dec 24 '17 at 02:03
  • You should show us your assembly code. One possibility is that you don't account for the name mangling with 32-bit CDECL calling convention that requires an `_` to be prefixed to the function name in assembly. This can be done automatically if you use something like `.MODEL FLAT, C` . ithout seeing your code I can't tell. Please when putting code in SO do not take screenshots of it. paste it as text and format it with the `{}` button above the text edit window. – Michael Petch Dec 24 '17 at 02:07
  • @MichaelPetch Sorry I should of included that, my bad. I will keep that in mind and also the formatting. The updates are up. – Mercy Thomson Dec 24 '17 at 02:14
  • 1
    Okay, by default Visual Studio with a C++ project doesn't actually assemble ASM files with MASM automatically. If you haven't done so you may have to look at this SO question which is related to your in a way: https://stackoverflow.com/a/4549741/3857942 . You have to manually enable MASM build customisations and then add the ASM files to your project. Although that was for VS2010 it still applies to all VS after as well. – Michael Petch Dec 24 '17 at 02:16
  • @MichaelPetch thank you very much. I figured it out, I enabled MASM and I referenced the SO question you linked me. Is there some sort of follow up I need to take care of in this post I created? – Mercy Thomson Dec 24 '17 at 02:34
  • In a way I'd mark this question as a duplicate of the other. – Michael Petch Dec 24 '17 at 02:39
  • 1
    Possible duplicate of [Compiling assembly in Visual Studio](https://stackoverflow.com/questions/4548763/compiling-assembly-in-visual-studio) – Michael Petch Dec 24 '17 at 02:40
  • 1
    If you ever find yourself having aSM files in a project that didn't have the build customizations done first (and you added the ASMfiles after)you can always right mouseclick each ASM file in the project selection `properties` on the menu, select the `general` category of config properties, and in the `Item Type` select `Microsoft Assembler` and make sure that `exclude from build` is set to `no`. – Michael Petch Dec 24 '17 at 04:51

1 Answers1

0

If you skipped the default, you need to create a custom build step. Right click on the assembly source file name, then properties / custom build step and enter command line and outputs fields:

for debug build:

command: ml /Zi /c /Fo$(outdir)\example.obj example.asm
output: $(outdir)\example.obj

for release build:

command: ml /c /Fo$(outdir)\example.obj example.asm
output: $(outdir)\example.obj

Also you may need to declare clear as public in the assembly source file:

        public  clear

For a 64 bit build, use ml64 instead of ml.

I generally start a new project by creating an "empty project", then adding existing items (source file) to avoid the default stuff created by VS.

rcgldr
  • 27,407
  • 3
  • 36
  • 61
  • 1>------ Build started: Project: Project2, Configuration: Debug Win32 ------ 1>Assembling ..\..\..\..\Desktop\clear.asm... 1>Project2.vcxproj -> c:\Users\729347\source\repos\Project2\Debug\Project2.exe 1> Assembling: C:\Users\729347\source\repos\CPPASM\ProjectCPPASM\clear.asm 1>Microsoft (R) Macro Assembler Version 6.14.8444 1>Copyright (C) Microsoft Corp 1981-1997. All rights reserved. 1>Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped This is the output. The first photo shows that the clear is still a not found error I added public clear to the top of the assembly file @rcgldr – Mercy Thomson Dec 24 '17 at 02:45
  • @MercyThomson - did you fix the "does not participate in build" ? Change "excluded from build" to "No" if it's not defaulting to that. Also you are using an external version of masm instead of the one included as part of Visual Studio (version 10 or later). The VS version doesn't need command line option /coff. Try changing the build properties to match my answer (except for file name). – rcgldr Dec 24 '17 at 02:51
  • Hello again sir, I updated excluded from build to No I updated everything you said, the build is successful I added the assembly file after making the updates too. It doesnt make sense that the .asm file is not found when its physically sitting right above the .cpp – Mercy Thomson Dec 24 '17 at 02:58
  • The issue with VS is that there is a one time or perhaps one time per project creation to have it automatically generate the custom build step for .asm file(s). I never use it and instead manually enter the custom build step info. The custom build step parameters you need to enter have been the same since at least VS2005. – rcgldr Dec 24 '17 at 03:15