Author Topic: How to mix nasm with C code?  (Read 9282 times)

Offline dimaria

  • New Member
  • Posts: 1
How to mix nasm with C code?
« 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++.

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: How to mix nasm with C code?
« Reply #1 on: August 10, 2017, 11:09:43 AM »
try

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

in your C++ source.