user3339901
user3339901

Reputation: 1

What is wrong with my makefile? I get an "undefined reference" error

I'm pretty new to c coding and I've checked the standard references on makefiles but I seem to be misunderstanding something because I'm getting an error. If anybody can help me it would be greatly appreciated.

Here it is:

#makefile

all: runme dontrunme

runme: runme.o 
    clang runme.o -o runme

dontrunme: dontrunme.o library.o fun.o
    clang dontrunme.o -o dontrunme

runme.o: library.o runme.c
    clang -c runme.c -o runme.o

dontrunme.o: dontrunme.c
    clang -c dontrunme.c -o dontrunme.o

library.o: library.c library.h
    clang -c library.c -o library.o

fun.o: fun.c fun.h
    clang -c fun.c -o fun.o


clean:
    rm  -f *.o runme dontrunme

The error I get when I type make is:

runme.o:runme.c:function main: error: undefined reference to 'reverse'

reverse is a function whose protoype is in library.h and is defined in library.c

Any help in understanding where I went wrong is greatly appreciated.

Edit: Here is the code for runme.c

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

#include "library.h"

int main(int argc, char * argv[]){

  char s[] = "Hello World!";

  printf("%s\n", s);

  reverse(s);

  printf("%s\n", s);

}

Upvotes: 0

Views: 149

Answers (2)

WhozCraig
WhozCraig

Reputation: 66244

Change this:

runme: runme.o 
    clang runme.o -o runme

to this:

runme: runme.o library.o
    clang runme.o library.o -o runme

And do the same for dontrunme

dontrunme: dontrunme.o library.o fun.o
    clang dontrunme.o library.o fun.o -o dontrunme

In both cases you're not including all the object code files in your final links.

Upvotes: 3

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25905

please change order in makefile as follows. you first need library.o for build runme.o so please build library.o first then runme.o

library.o: library.c library.h
    clang -c library.c -o library.o


runme.o: library.o runme.c
    clang -c runme.c -o runme.o

Upvotes: 0

Related Questions