I need to write an assembler module with a function definition and link it with my program in C++ where is a declaration of the function implemented in assembly. I don't know how to write a C++ file and an Assembler module to properly compile it all.
I have written a C++ file
#include <iostream>
using namespace std;
void copy(unsigned int * source, unsigned int * dest, int n);
int main() {
unsigned int source[] = {1, 2, 3, 4};
unsigned int dest[4];
copy(source, dest, 4);
return 0;
}
Then I checkout the assembly name of that function by command:
g++ -masm=intel -S main.cpp -o file.asm
and I inserted the found result to the assembly module
segment .text
global _Z4copyPjS_i@PLT
_Z4copyPjS_i@PLT:
ret
I would like to compile cpp file with an assembly module (where will be an implementation for function copy) but I don't know how to do it? I tried:
nasm -felf64 -o copy.o copy.asm
g++ main.cpp -o main.o
g++ -o main copy.o main.o
But during the second command, I get an error:
/usr/bin/ld: tmp/ccWFOX8p.o: in function 'main':
main.cpp:(.text+0x4b): undefined reference to 'copy(unsigned int*, unsigned int*, int)'
collect2: error: ld returned 1 exit status