Assembly file compilation issues - Error: undefined reference to 'function name'
I am trying to look at the test program my professor gave us, but I am having trouble compiling it. I am on Ubuntu 14.04. I am compiling it with
gcc -Wall test.c AssemblyFunction.S -m32 -o test
I was having trouble running the code on a 64 bit machine and read that adding -Wall and -m32 would make it work. This fixed the first problem I ran into, but now I get a link : undefined to `addnumbersinAssembly ' .
Here is the C file
#include <stdio.h>
#include <stdlib.h>
extern int addnumbersinAssembly(int, int);
int main(void)
{
int a, b;
int res;
a = 5;
b = 6;
// Call the assembly function to add the numbers
res = addnumbersinAssembly(a,b);
printf("\nThe sum as computed in assembly is : %d", res);
return(0);
}
And here is the build file
.global _addnumbersinAssembly
_addnumbersinAssembly:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp), %eax
addl 12(%ebp), %eax # Add the args
movl %ebp,%esp
popl %ebp
ret
Thank you for your time. I've been trying to figure this out for hours, so I appreciate any help.
source to share
I believe with GCC you will want to remove _
in your assembler file. So these lines:
.global _addnumbersinAssembly
_addnumbersinAssembly:
Should be:
.global addnumbersinAssembly
addnumbersinAssembly:
More information on this issue can be found in this question / answer.
The compile option is -m32
required because the assembly code you have must be rewritten to support some 64-bit operations. In your case, these were stack operations. -Wall
not required for compilation, but it raises many more warnings.
source to share