1

I know there are several related posts on this topic but after going though it for the better part of all day, I am posting this question.

I am building and compiling a C ELF application (NOT C++) with clang. Everything compiles fine, but linking fails with a ton of undefined symbols starting with a underscore. These symbols are all defined in glibc at /lib/libc-2.27.so without the underscore but LLVM keeps putting the underscore in my object files. I strongly suspect I should be linking against a different libc, however, the stellar documentation has left me with no idea what this isn't working.

libtool: link: clang -I../include -I../libstuff -g -O2 -I/usr/lib64/llvm/6/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -target x86_64-pc-linux-gnu -std=c99 -stdlib=libc -I/usr/include -isystem/mnt/apple-sdk/4.6/4.6.2/SDKs/MacOSX10.7.sdk/usr/include --no-system-header-prefix=/usr/include --no-system-header-prefix=/mnt/apple-sdk/4.6/4.6.2/SDKs/MacOSX10.7.sdk/usr/include -o ar ar-append.o ar-ar.o ar-archive.o ar-contents.o ar-delete.o ar-extract.o ar-misc.o ar-move.o ar-print.o ar-replace.o -L/usr/lib64/llvm/6/lib64 ../libstuff/.libs/libstuff.a -lLLVMSymbolize -lLLVMDemangle -lLLVMSupport -lLLVMMC -lc ar-append.o: In function `append': /home/kwhat/NetBeansProjects/binutils-apple_new/ar/append.c:103: undefined reference to `_open' /home/kwhat/NetBeansProjects/binutils-apple_new/ar/append.c:113: undefined reference to `_close'

Update and Solution

After reading the GCC documentation for -I, -isystem and adjusting the include ordering in my CFLAGS I was able to work around this problem. The issue ended up being that one file from the OS X include folder needed to come before /usr/include and the rest after. Examining the Plo files offered useful insight into where the includes where coming from and which ones were unexpected. In case anyone is wondering what the hell I am doing, I am refactoring an old port of cctools & ld64 for Linux to OS X cross compiling. The project currently works, but includes a lot of header hacks and unneeded patching I am trying to resolve. More info: https://github.com/kwhat/binutils-apple

Alex Barker
  • 4,316
  • 4
  • 28
  • 47
  • 1
    The [underscores are completely normal](https://stackoverflow.com/questions/2627511/why-do-c-compilers-prepend-underscores-to-external-names), however, if you have [externs prefixed with an underscore then it's possible you could run into issues](https://stackoverflow.com/questions/2627004/embedding-binary-blobs-using-gcc-mingw) I suppose. – l'L'l Jun 17 '18 at 05:47
  • I think I figured it out. https://github.com/apple/darwin-xnu/blob/master/bsd/sys/cdefs.h#L659 Thats where this stupidity is coming from. I basically need to use /usr/include unless something is missing then fall back to the apple include folder. – Alex Barker Jun 17 '18 at 05:50
  • 1
    I feel your pain...maybe [take a look at what is mentioned here](http://nickdesaulniers.github.io/blog/2016/08/13/object-files-and-symbols/) perhaps, (eg.`nm a.out | c++filt`). [Clang devs discussed a "demangle" option](https://reviews.llvm.org/D34668) some time ago, although I don't know if anything ever became of it. – l'L'l Jun 17 '18 at 06:31
  • 1
    You seem to mix in standard header files for Apple OSX, but you are compiling on linux and linking to glibc ? This is not going to work. What are you really trying to do ? – nos Jun 17 '18 at 11:03
  • I ended up solving the issue by adjusting the include paths and order. The reason I was having some much trouble was that the include order was not consistent though the compile processes. Sometimes /usr/include needed to come first and other times the Apple SDK include to come first. I adjusted to the build processes to take that into account and was able to finally move forward. Thank you all for taking a look. – Alex Barker Jun 17 '18 at 15:31

0 Answers0