NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: dimaria on August 10, 2017, 10:09:55 AM

Title: How to mix nasm with C code?
Post by: dimaria on August 10, 2017, 10:09:55 AM
I am using MS Visual C++ 6, with this code in "main.c":

#include

int dev1(); //asm function

void main(void)
{
int a=0;
a=dev1();
printf("%d",a);
}

and the "dev1.asm" compiled to "dev1.obj" with "nasm -fwin32 dev1.asm":

BITS 32
global _dev1

section .text
_dev1:
enter 0,0
mov eax,1
leave
ret

- It works OK, but if I change the "main.c" to "main.cpp" it fails:

Linking...
main.obj : error LNK2001: unresolved external symbol "int __cdecl dev1(void)" (?dev1@@YAHXZ)
Debug/nasm.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

- Then, how can I mix ASM with C++? It is important to use ASM with Classes, that are C++.
Title: Re: How to mix nasm with C code?
Post by: dreamCoder on August 10, 2017, 11:09:43 AM
try

Code: [Select]
extern "C"
{
   int dev1();
}

in your C++ source.