1

I'm trying to compile a C program linking two previously created object files but keep getting an 'undefined reference' error.

I'm using Visual Code to write the code and Ubuntu on Windows to compile using a makefile. The two C files, task5.c and reverse.c which have been made into object files both contain #include reverse.h statements which contains the prototypes for the functions in reverse.c.

task5.c

#include <stdio.h>
#include "ctap.h"
#include "reverse.h"

TESTS {
    const char *a = "Family";
    char *b = reverse(a);

    //test 1
    ok(string_length(a) == string_length(b), "Strings are the same size");

    //test 2
    is("ylimaF", b, "Strings match");
}

reverse.c

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

char *reverse(const char *str) {
    //code
}

int string_length(const char *str) {
    //code
}

reverse.h

char *reverse(const char *str);
int string_length(const char *str);

makefile

linked:
    gcc -o linked task5.o reverse.o

task5.o
    gcc -c -o task5.o task5.c

reverse.o
    gcc -c -o reverse.o reverse.c

When I run the command make linked I expect it to be made and return nothing.

But when I run that command I get this error:

cc   task5.o   -o linked
task5.o: In function `ctap_tests':
task5.c:(.text+0x1abe): undefined reference to `reverse'
task5.c:(.text+0x1ace): undefined reference to `string_length'
task5.c:(.text+0x1adc): undefined reference to `string_length'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'linked' failed
make: *** [task5] Error 1
7
  • 3
    You have a makefile which links both object files. But you don't seem to be using it? Or using an old makefile? Because the output you show doesn't match the makefile you show. Apr 10, 2019 at 6:28
  • stackoverflow.com/questions/12573816/… describes the topic (C++ centered, but probably generally also helpful for C).
    – Yunnosch
    Apr 10, 2019 at 6:31
  • @Someprogrammerdude What part doesn't match? If you're referring to the 'In function `ctap_tests':' ctap_tests is there because the macro TESTS{} expands to 'void ctap_tests(void)' according to VSCode when I hover over TESTS{} in task5.c.
    – Nathan
    Apr 10, 2019 at 6:42
  • Ah, I've just tried compiling it using 'gcc -o linked task5.o reverse.o' instead of make linked and it worked. I'm not sure why it would be using another make file but thanks for getting me to check it out, it's definitely something wrong with the make file.
    – Nathan
    Apr 10, 2019 at 6:49
  • 1
    Try to rename it Makefile
    – Phantom
    Apr 10, 2019 at 7:19

1 Answer 1

1

According to this GNU make documentation, the GNU make program will try to use GNUMakefile, makefile or Makefile.

The make program will not try makefile.mk, which means that e.g. make linked will use no makefile and only the default rules.

You can solve this by either renaming your makefile as Makefile (the most common) or makefile; Or by using the -f option to specify the makefile

$ make -f makefile.mk linked
1
  • Thankyou! You were right, it was the way VSCode named the makefile when i saved it, I just rewrote it in notepad++ and saved it as just makefile (VSCode wouldn't let me) and it worked!
    – Nathan
    Apr 10, 2019 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.