问题
On Linux, the D runtime library relies on the _end
symbol in determining the GC root ranges (man 3 end
), which is in fact very similar to what the Boehm GC does. However, when linking in libcurl, the symbol is no longer found by the linker:
$ cat test.c
#include <stdio.h>
extern char _end[];
int main() {
printf("%p\n", &_end);
return 0;
}
$ gcc test.c # works
$ gcc test.c -lcurl
/usr/bin/ld: /tmp/ccOPtbEv.o: undefined reference to symbol '_end'
/usr/bin/ld: note: '_end' is defined in DSO /usr/lib/libssl.so.1.0.0 so try adding it to the linker command line
/usr/lib/libssl.so.1.0.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
From a quick search on the D forums, libpq, libdw and several other libraries seem to trigger the same problem. Any idea what could be happening here? test.c
doesn't even depend on symbols from libcurl. (Arch Linux x86_64, GCC 4.7.2, ld 2.23)
Also, please note that »try linking in libssl« is not the answer I am looking for, I want to understand what is happening here. I'm trying to fix this problem in a compiler I'm working on, so you can assume basic familiarity with how the linking process works.
Edit: The reason why I'm not particularly satisfied with telling the users to just link in libssl, ..., explicitly is that pkg-config --libs curl
, curl-config --libs
, etc. don't contain this information; requiring it would thus break build systems. If anyone has a better idea for determining the bounds of the data (initialized and BSS) segments, I'd be keen to know.
Edit 2: With the toolchain mentioned above, end
(without the underscore) also seems to be defined, and it doesn't trigger the problem. Still baffled as to why it occurs, though.
回答1:
please note that »try linking in libssl« is not the answer I am looking for.
But most likely it actually is the answer you are looking for. Your command line is all wrong. Try this instead:
gcc test.c -lcurl
The order of libraries and sources,objects on command line matters.
回答2:
_end symbol is provided by linker. It points past the last object in bss. However, it's just a convention, no standard requires this. Your toolchain's linker doesn't do that, apparently.
来源:https://stackoverflow.com/questions/13328144/end-symbol-disappears-when-linking-to-libcurl