1

Consider the program:

#include <gc/gc.h>
int main() {
  void* p = GC_MALLOC(15);
}

Under Ubuntu 10.04 LTS this compiles (gcc -lgc test.c). Under 12.04 LTS:

/tmp/cc7GcTfU.o: In function `main':
main.c:(.text+0xe): undefined reference to `GC_malloc'
collect2: ld returned 1 exit status

It looks like between 10.04 and 12.04 they've changed the library not to compile in malloc replacements. Or that's what I think this description of the libgc1c2 package says:

[...] However, it does not work as a drop-in malloc(3) replacement.

Is there a simple way to get around this? (Say, something simpler than recompiling libgc manually...)

madth3
  • 7,275
  • 12
  • 50
  • 74
Max
  • 3,384
  • 2
  • 27
  • 26

1 Answers1

2

To answer my own question: actually, the Boehm GC library still works the same way as it used to in 12.04. The problem is that GCC doesn't! GCC has started to default to --as-needed, and it drops -lgc completely if it is at the beginning of the line. This is a very major change!!

Solution is to move -lgc to the end:

gcc test.c -lgc

Or add:

gcc -Wl,--as-needed -lgc test.c
Max
  • 3,384
  • 2
  • 27
  • 26
  • The order of libraries on a linker command line have always been important. When libraries are archives (.a files) it will link in code from the library when it sees the library. If the library is before the .o files, then it will see nothing is needed from the library. – brian beuning Dec 20 '12 at 13:52
  • It depends on the compiler. GCC used to default to linking everything. Now it defaults to `--as-needed`. – Max Jan 03 '13 at 22:44