0

Im trying to do a simple excercise in compilation. I have 1 c file 1 assembly file and a makefile. when I run the 'make' command I get the following error:

gcc -g -m32 -Wall -o mainAssignment0.o mainAssignment0.c /tmp/ccXfVxtg.o: In function `main': /home/caspl202/Desktop/tasks/Assignment0/mainAssignment0.c:12: undefined reference to `do_Str' collect2: error: ld returned 1 exit status makefile:10: recipe for target 'mainAssignment0.o' failed make: * [mainAssignment0.o] Error 1 

Meaning that for some reason the c program doesnt recognize the external ASM function. Whats even weirder is that when I run the same makefile on the same files on a different machine it works like a charm. I would really like someone to shed some light on this thing. C code:

#include <stdio.h> #define MAX_LEN 100 extern int do_Str(char*); int main(int argc, char** argv) { char str_buf[MAX_LEN]; int counter = 0; fgets(str_buf, MAX_LEN, stdin); counter = do_Str (str_buf); printf("%s%d\n",str_buf,counter); return 0; } 

ASM code:

section .data an: dd 0 section .text global do_Str do_Str: push ebp mov ebp, esp pushad mov ecx, dword [ebp+8] loop: cmp byte [ecx], 32 jnz noS inc dword [an] noS: cmp byte [ecx], 65 jl noC cmp byte [ecx], 90 jg noC add byte [ecx], 32 noC: inc ecx cmp byte [ecx], 0 jnz loop popad mov eax,[an] mov esp, ebp pop ebp ret 

Makefile:

all: exec libs: asm-lib asm-lib: asmAssignment0.s nasm -g -f elf -o asmAssignment0.o asmAssignment0.s exec: mainAssignment0.c libs gcc -g -m32 -c -o mainAssignment0.o mainAssignment0.c gcc -g -m32 -o Assignment0.out mainAssignment0.o asmAssignment0.o .PHONY: clean clean: rm -rf ./*.o Assignment0.out 
2

2 Answers 2

1

You don't need to declare the function external.

int do_Str(char*); 

Also, a function in C is prefixed with an underscore, so you must name it accordingly in your asm file.

 global _do_Str _do_Str: 

The underscore is automatically added by the C compiler, so you don't have to use it in the C module.

Sign up to request clarification or add additional context in comments.

Comments

0

The reason for your error you quote here is that your compile line is wrong. You can tell because you're trying to create an object file, but getting errors from the linker, so something is clearly not right:

gcc -g -m32 -Wall -o mainAssignment0.o mainAssignment0.c ... collect2: error: ld returned 1 exit status 

The problem is you forgot to add the -c flag to this compile line, so that the compiler generates an object file.

However, in your makefile the -c is present, so clearly this error you quote is not generated from the makefile you show us.

exec: mainAssignment0.c libs gcc -g -m32 -c -o mainAssignment0.o mainAssignment0.c 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.