1

I have problems with linking c and assembly code. Tried to search some solutions, but none of which I found worked for me.

c file "l3.c" looks like this:

#include <stdio.h>
#include <stdlib.h>

const int INP_SIZE = 100;

extern int mult(int c1, int c2);

int main()
{
    char number[INP_SIZE];

    scanf("%s",number);
    printf("You typed: %s \n",number);

    int j=2;
    int k=5;
    j = mult(j,k); #here is the problem
    printf("%d",j);

    scanf("%s",number);
    return 0;
}

and asembly "mult.s" like this:

.type mult, @function
mult:
push %rbp
mov %rsp, %rbp
mov 8(%rbp), %rbx
mov 12(%rbp), %rcx

mov $0, %rax
add %rbx, %rax;
add %rcx, %rax;

mov %rbp, %rsp
pop %rbp
ret

In my makefile i got following lines(most recent solution i googled):

l3:
    as -g -o mult.o mult.s
    gcc -o l3.o -c l3.c
    gcc l3.o mult.o

When I type make in console it is throwing: undefined reference to 'mult' when I comment //j = mult(j,k); program works fine. How should I link this?

4

1 回答 1

3

It looks like you're not exporting mult as a symbol, so the linker can't find it later on. How to do it correctly depends on what assembler you're using. Since you're using AT&T syntax, I'm going to guess GNU as - in that case just add .global mult at the top of your assembly file.

于 2013-05-19T15:35:49.873 回答