Morpheus
Morpheus

Reputation: 1750

Can't call c-functions from NASM in VC++ except main, getting linking error

Can anyone please tell me why I can't call any global functions from NASM, except main ? (Before you ask) Yes, I have read all the questions regarding this in stackoverflow and in internet for about 8 hours.

C++ code.

void main();

extern "C" void nasm_function(void);

void c_function()
{
}

void main()
{
    nasm_function();

    system("pause");
}

NASM code,

extern _c_function
extern _main

segment .text

global _nasm_function
_nasm_function:
call _main  
call _c_function

Output,

1>Linking... 1>my_asm.obj : error LNK2001: unresolved external symbol _c_function 1>F:\Projects\OSDev\assmebly_test\Debug\project.exe : fatal error LNK1120: 1 unresolved externals

as you can see, we don't get linking error for main. I don't know why. :)

Settings,

building nasm using custom-build-rules with nasm.exe -f win32 
Calling convention is __cdecl (/Gd)
Visual Studio 2008
NASM version 2.05
Didn't include my_asm.obj as a input to linker

Anyone please tell me what is the issue ? Thanks in advance. (note that this is a sample program, but still getting the issue)

Upvotes: 0

Views: 740

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 119877

The issue is name mangling. The solution is extern "C".

Upvotes: 1

Related Questions