This is really making mad. I'm trying to add two numbers. This is a basic code that I can't get to compile.

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

extern int addus(int,int);

int main() {

	int a;
	int b;
	int result;
	a=5;
	b=6;

	result = addus(a,b);
	printf("Your value is: %d", result);
	return EXIT_SUCCESS;
}
.globl _addus
_addus:

push %ebp
movl %esp, %ebp

movl 8(%ebp), %ecx
addl 12(%ebp), %ecx
movl %ecx, %eax

movl %ebp, %esp
popl %ebp
ret

Here is my error.

**** Build of configuration Debug for project Trial447 ****

**** Internal Builder is used for build ****
gcc -O0 -g3 -Wall -c -fmessage-length=0 -osrc\Trial447.o ..\src\Trial447.c
gcc -oTrial447.exe src\Trial447.o
src\Trial447.o: In function `main':
C:/Workspace/Trial447/Debug/../src/Trial447.c:24: undefined reference to `addus'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 1062 ms.

Recommended Answers

All 5 Replies

undefined reference to `addus'

The linker cannot find the function addus. This is not a compile error - it is a linking error. Your assembly code needs to be turned into an object file, and linked against.

Ahhh. Can I do that by creating an object code in command prompt?

I would check what you calling the function name in your asm file. Your calling it _addus in the asm file and calling it addus in your main executable. The linker won't be able to resolve the names if they don't match.

I just tried your code an it worked for me.

test.c

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

extern int addus(int,int);

int main() 
{

	int a;
	int b;
	int result;
	a=5;
	b=6;

	result = addus(a,b);
	printf("Your value is: %d", result);
	return EXIT_SUCCESS;
}

asmfile.s//note function label, I changed it to addus

.globl addus
addus:

pushl %ebp
movl %esp, %ebp

movl 8(%ebp), %ecx
addl 12(%ebp), %ecx
movl %ecx, %eax

movl %ebp, %esp
popl %ebp
ret

Compile lines

as asmfile.s --32 -o asmfile.o
gcc test.c -c -m32
gcc test.o asmfile.o -o test -m32

Note I had to use the -m32 and --32 switches because my machine is a Intel 64 bit.

hmmm. changing _addus to addus did nothing. the reference is still undefined.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.