VSNASM icon indicating copy to clipboard operation
VSNASM copied to clipboard

State Error LNK2001 unresolved external symbol _mainCRTStartup

Open FashionFlora opened this issue 3 years ago • 4 comments

image Whats wrong?

FashionFlora avatar May 04 '21 14:05 FashionFlora

I'm assuming that you're trying to write just assembly and nothing else, but it's in a C++ project. Under the hood in C++ int main is not the first thing that is called when your program starts, there is setup required for the C runtime to work correctly and the setup function for that (_mainCRTSetup) is generated by the compiler. If all you want is to run assembly while being able to get CRT functions (like malloc) I'd suggest just defining something like extern "C" int asmMain() in a C++ file and call it from C++ like so:

#include <iostream>

extern "C" int asmMain();

// _mainCRTSetup secretly called before this
int main()
{
    return asmMain();
}

and in assembly your main function is now called asmMain which will run after _mainCRTSetup so something like this in assembly:

bits 64
global asmMain

asmMain:
    mov rax, 69 ; some return code, 0 if no error but 69 is funny
    ret  

hope this was helpful, but this issue is pretty old so I'm sure you've figured it out yourself by now

Zman2024 avatar Feb 06 '22 01:02 Zman2024

@Zman2024 Tried your simple application, but having the same issue.

image

main.cpp

extern "C" int asmMain();

int main()
{
	return asmMain();
}

asmmain.asm

[BITS 64]

GLOBAL asmMain

asmMain:
    mov rax, 69
    ret  

Any ideas what could be wrong?

GWOSTIK avatar Feb 08 '23 10:02 GWOSTIK

@Zman2024 Tried your simple application, but having the same issue.

image

main.cpp

extern "C" int asmMain();

int main()
{
	return asmMain();
}

asmmain.asm

[BITS 64]

GLOBAL asmMain

asmMain:
    mov rax, 69
    ret  

Any ideas what could be wrong?

it seems like your main.cpp and asmmain.asm object files aren't linking to each other for some reason

Zman2024 avatar Feb 08 '23 11:02 Zman2024

I ran into a similar issue. everything looked correct with dumpbin -symbols .\assemblyNasm64.obj but could not get it to link in visual studio 2019 I changed global get_assembly_test_num ; makes this visible to linker

get_assembly_test_num: mov rax,1234 ; just return a constant ret to section .text ; required or visual studio will not link the function

global get_assembly_test_num ; makes this visible to linker

get_assembly_test_num: mov rax,1234 ; just return a constant ret

and then it linked you must give it a section or it will not link in visual studio Also note. if win32 the function requires a leading _ whereas win64 does not

brettrjones-github avatar Mar 14 '23 08:03 brettrjones-github