H
H
HamsterGamer2022-01-12 00:04:47
GCC
HamsterGamer, 2022-01-12 00:04:47

Why is the definition not found when linking assembler and c++ objects?

There is the following c++ code (main.cpp file):

#include <iostream>

extern int _sub(int, int);

int main() {
    std::cout << _sub(3, 15);
    return 0;
}

and the implementation of the _sub function (file sub_f.asm):
section .text

global _sub
_sub:
    mov rax, rcx
    sub rax, rdx
ret

When compiling and linking as follows;
nasm -f elf64 -g sub_f.asm -o sub_f.o
g++ -c main.cpp -o main.o
g++ -o asm_test.exe main.o sub_f.o

for some reason an error is thrown: undefined reference to `_sub(int, int)' (compiled and linked on Linux platform)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2022-01-12
@HamsterGamer

Tryextern "C" int _sub(int, int);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question