1

I am using Kubuntu 14.04, and installed the FreeImage library with

sudo apt-get install libfreeimage-dev

As far as I can tell, it is correctly installed, with FreeImage.h in /usr/include and libfreeimage.a in /usr/lib. However, this trivial C program

#include <FreeImage.h>

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

  FreeImage_Initialise(FALSE);
  FreeImage_DeInitialise();
  return 0;

}

fails to compile. Running

gcc -lfreeimage fitest.c -o fitest

outputs

/tmp/ccFbb0HQ.o: In function `main':
fitest.c:(.text+0x15): undefined reference to `FreeImage_Initialise'
fitest.c:(.text+0x1a): undefined reference to `FreeImage_DeInitialise'
collect2: error: ld returned 1 exit status

What am I doing wrong?

Mark Raymond
  • 906
  • 8
  • 22
  • Check capitalization. –  Sep 02 '14 at 22:18
  • That's a bit odd, because when I copy-paste your code exactly and compile it with the same command, with `libfreeimage-dev` installed, it compiles perfectly well. – Dolda2000 Sep 02 '14 at 22:20
  • 2
    You mention `libfreeimage.a` instead of `libfreeimage.so`, which you would normally actually be linking against. Is there a reason for this? Are you somehow explicitly attempting to link your program statically or something (and why doesn't that show in your compile command, in that case)? *Is* `libfreeimage.so` properly installed? – Dolda2000 Sep 02 '14 at 22:22

1 Answers1

4

This wouldn't normally be the case with shared libraries but only static ones, but I'm going to give it a shot anyway since it matches your symptoms, and you also mention libfreeimage.a instead of libfreeimage.so, indicating that you're trying to use the static library.

When linking against static libraries, you need to give the library arguments after the explcit source/object arguments to the compiler, because the compiler will only process yet unresolved symbols from the library:

gcc -o fitest fitest.c -lfreeimage

If you give a static library argument before any source/object arguments, then, no symbols will yet be unresolved, nothing will be picked from the library, and the symbols will instead be seen as unresolved at the end.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • Ah, searching around a bit confirmed why it was working for me (on Debian( but not for you (on Ubuntu). [This answer](http://stackoverflow.com/questions/7826448/linking-libraries-with-gcc-order-of-arguments) holds the secret. – Dolda2000 Sep 02 '14 at 22:51