user1261852
user1261852

Reputation:

Problems compiling assembly file - Error: undefined reference to `function name'

I am trying to take a look at a 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 problems running the code on a 64-bit machine and read that adding -Wall and -m32 will allow it to work. Doing that fixed the first problem I had, but now I am getting the error: undefined reference 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 assembly 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 have been trying to figure this out for hours, so I appreciate any help.

Upvotes: 5

Views: 16061

Answers (1)

Michael Petch
Michael Petch

Reputation: 47553

I believe that with GCC you are going to want to remove the _ 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 StackOverflow question/answer.

The -m32 compile parameter is needed because the assembly code you have needs to be rewritten to support some 64 bit operations. In your case it was stack operations. The -Wall isn't needed to compile but it does turn on many more warnings.

Upvotes: 5

Related Questions