0

I am using MinGW on a Windows 8.1 machine and I'm trying to reference a static library. Try as I might I get an undefined reference error.

This is usually just a case of passing the -L and -l flags normally right but here is what I do...

I have the following directory structure:

myapp.c
mylib
    mylib.c
    mylib.h

mylib/mylib.c

#include <stdio.h>

void do_something_amazing(void) {
    printf("Woah\n");
}

mylib/mylib.h

void do_something_amazing(void);

myapp.c

#include "mylib/mylib.h"

int main() {
    do_something_amazing();
    return (0);
}

I run the following commands to first create a libmylib static library then to build the myapp that references it:

gcc -c -o mylib/mylib.o mylib/mylib.c
ar rcs mylib/libmylib.a mylib/mylib.o
gcc -Lmylib -lmylib myapp.c

And the last finishes with this error:

cchqpYVl.o:myapp.c:(.text+0xc): undefined reference to `do_something_amazing'
collect2.exe: error: ld returned 1 exit status

This seems kind of basic, so I am sure I am being silly and I missed a step - any idea what I'm doing wrong?

kmp
  • 10,215
  • 10
  • 72
  • 122
  • 2
    Put `-lmylib` after `myapp.c` in the last command. It links in order without a second pass by default – M.M Sep 27 '16 at 05:55
  • Ugh, THANK YOU! Cannot believe I did not try that. – kmp Sep 27 '16 at 05:55
  • http://stackoverflow.com/questions/11893996/why-does-the-order-of-l-option-in-gcc-matter – AnT Sep 27 '16 at 05:59

0 Answers0