0

While trying to setup Visual Studio 2019 to compile a .asm project, I am getting an error.

I created an empty c++ project and added a Main.asm file. I tried both MASM and NASM and get the same error:

LNK1104 cannot open file 'C:\Users\Serge Fonville\source\repos\NASM\x64\Debug\Main.obj'

Both NASM and MASM produce the same error. The mentioned .obj file is also not present on the location. When compiling manually, it works without issue.

I followed all the guides I could find to set this up, but to no avail.

I removed and reinstalled everything between attemps.

Initially I thought it had to do with MASM (which I tried first), but since NASM gives the same errors, it seems to me it is something else. Unfortunately, I have no idea what.

Before compiling I verified the build extensions for the applicable assembler were enabled and the .asm file had the corresponding item type.

Any help is greatly appreciated.

For reference, the .asm (NASM) I tried to compile: From https://cs.lmu.edu/~ray/notes/nasmtutorial/

; ----------------------------------------------------------------------------------------
; This is a Win64 console program that writes "Hello" on one line and then exits.  It
; uses puts from the C library.  To assemble and run:
;
;     nasm -fwin64 hello.asm && gcc hello.obj && a
; ----------------------------------------------------------------------------------------

        global  main
        extern  puts
        section .text
main:
        sub     rsp, 28h                        ; Reserve the shadow space
        mov     rcx, message                    ; First argument is address of message
        call    puts                            ; puts(message)
        add     rsp, 28h                        ; Remove shadow space
        ret
message:
        db      'Hello', 0                      ; C strings need a zero byte at the end

The MASM file:

;Descr  : My First 64-bit MASM program
;ml64 prog.asm /c
;golink /console /entry main prog.obj msvcrt.dll, or
;gcc -m64 prog.obj -o prog.exe (main/ret). This needs 64-bit GCC.
;-----------------------------------
extrn printf:proc
extrn exit:proc

.data
hello db 'Hello 64-bit world!',0ah,0

.code
main proc

        mov     rcx,offset hello
        sub     rsp,20h
        call    printf
        add     rsp,20h

        mov     rcx,0
        call    exit

main endp
end

The output when I do a rebuild:

Rebuild started...
1>------ Rebuild All started: Project: NASM, Configuration: Debug x64 ------
1>LINK : fatal error LNK1104: cannot open file 'x64\Debug\Main.obj'
1>Done building project "NASM.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

When I dir for the files

C:\Users\Serge Fonville>dir /s /b c:\main.obj
c:\VTRoot\HarddiskVolume3\Users\Serge Fonville\source\repos\NASM\NASM\x64\Debug\Main.obj
Serge Fonville
  • 304
  • 2
  • 4
  • Have you added masm as a [build customization](https://stackoverflow.com/a/43038502/2189500)? – David Wohlferd Sep 13 '21 at 18:39
  • @DavidWohlferd, I have, and verified the item type for the .asm file. – Serge Fonville Sep 13 '21 at 18:50
  • 1
    Well, sounds like it's either not building it, or not putting it in the right place. If you do a 'Rebuild Solution' and look at the "Output" window, do you see "Assembling main.asm"? If yes, then it's probably somewhere. `dir c:\main.obj /s /b` will probably turn it up. – David Wohlferd Sep 14 '21 at 04:14
  • I added the rebuild output. It suggests that it is not being assembled. – Serge Fonville Sep 14 '21 at 04:20
  • The `dir` does seemed to have turned up something, everything seems to be located in `c:\VTRoot\HarddiskVolume3\Users\Serge Fonville\source\repos`. That suggests the antivirus perhaps? – Serge Fonville Sep 14 '21 at 04:30
  • There are no log messages from the asm source being assembled? What if you introduce a syntax error into the MASM or NASM source file, so the build process should definitely contain errors from it? Or are you trying to just assemble manually outside the IDE, and have its build process find it for linking? If so, your IDE probably uses some complicated directory structure for temporary .obj files. – Peter Cordes Sep 14 '21 at 04:32
  • @PeterCordes, when I add invalid contents to the `Main.obj` I get the same output – Serge Fonville Sep 14 '21 at 04:47
  • I'd recommend setting up your build system so building `Main.obj` from `Main.asm` is part of the process. I was talking about invalid contents in `Main.asm`. Are you confirming that you're only manually building `Main.obj` from `Main.asm`, and hoping the VS build process finds that already-assembled `.obj` as one of its inputs? There might be a way to make that work, but even if it's possible, it's inconvenient to have to tab out and assemble separately from linking within the IDE. – Peter Cordes Sep 14 '21 at 05:00
  • When I add random text to `Main.asm` the error is the same (except when I increase the verbosity, then it has exit code 1). When manually building the file (using the same command from the log output) it builds, but the linker is still unable to find it. – Serge Fonville Sep 14 '21 at 05:08
  • It seems indeed to have to do with the antivirus. When I disable it, I get different errors that indicate the linker cannot resolve references. – Serge Fonville Sep 14 '21 at 05:12
  • Virus protection would not have been my guess, but if it helps, it helps. I'm guessing the missing symbols can be found in MSVCRT.lib, kernel32.lib, vcruntime.lib, & ucrt.lib. Adding them to the `Additional Dependencies` under Linker/Input should help (separate them with semicolons). – David Wohlferd Sep 14 '21 at 06:27
  • Adding `ucrt.lib` and `legacy_stdio_definitions.lib` did the trick. – Serge Fonville Sep 14 '21 at 19:31

0 Answers0