Issues developing with Clang, cmake & Haiku

Hi,

as I had a free weekend without appointments I thought I could play around with some developing for Haiku. Since I like cmake and clang/llvm I installed both using HaikuDepot.
Looking around the system I found zillions of places where headers and libraries are placed:

/boot/system/lib
/boot/system/lib/x86
/boot/system/develop/lib
/boot/system/develop/lib/x86
/boot/develop/lib/x86

etc…

since most of the entries seem to be symlinks to whatever lib or folder, I am confused.
So, what folder should be used primary for searching/finding system libs?
Is there any specified order which should be used?
Same goes for header files. Which are the primary locations? Where are the includes/lib of gcc and clang located?

Also is something like a template available for compiler flags, which should always be used? At the moment I use

CFLAGS="-I/boot/system/develop/headers -fPIC -Wall -W -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-comment -ffunction-sections -fdata-sections -O3 -DNDEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS"

Thanks,
Alexander

For clang you need to use the headers in: /boot/system/develop/headers/x86

And the libs in: /boot/system/develop/lib/x86

Things outside the "x86" folder are for gcc2 apps (it's this way for compatibility reasons with BeOS). Things outside "develop" are for runtime execution, inside "develop" for linking and compiling. The folders in /boot are just symlinks to the ones in system/ for convenience and, again, BeOS compatibility.

Hi PulkoMandy,

thanks for the explanation. I now use /boot/system/develop/lib/x86 as base for searching for libs in my cmake setup.
Compiling now works flawlessly, but clang produces a gcc2 shared library (I checked that with objdump).

Is there any magic to create a non-gcc2 lib with clang? It seems that clang under Haiku uses gcc for linking. Maybe I need to use the gcc-x86 linker?

Again thanks,
Alexander

This should work out of the box, but maybe we missed something. I suggest running: “setarch x86” and trying again (be sure to clean all cmake generated files, because it caches a lot of information).

After some tests there is something I really do not get here.
I made a small C++ sample code called test.cc
#include

int main()
{
std::cout << “Hello World!” << std::endl;
return 0;
}

Using “setarch x86” in a shell gives fine results:
~/Desktop/source/test> gcc test.cc -o test.gcc -lstdc++
~/Desktop/source/test> objdump -D test.gcc | grep -A2 -B2 ABI

00001cf0 <_gSharedObjectHaikuABI>:
1cf0: 00 00 add %al,(%eax)
1cf2: 04 00 add $0x0,%al

Without “setarch x86” I need a different std lib, but otherwise it compiles fine:
~/Desktop/source/test> gcc test.cc -o test.gcc2 -lstdc++.r4
~/Desktop/source/test> objdump -D test.gcc2 | grep -A2 -B2 ABI

00001b38 <_gSharedObjectHaikuABI>:
1b38: 02 00 add (%eax),%al
1b3a: 02 00 add (%eax),%al

So far I was not able to reproduce this with clang. It simply fails locating symbols or libs. And I need to give some further include directives:

(If I paste the terminal output here it triggers a spam filter?!?)

So what is wrong here (except for beeing too stupid…)?

Hmm.
I checked the linker calls from gcc and clang, both with “setarch x86”:

gcc:

~> setarch x86

Switching to architecture x86

~> gcc -Xlinker -v
collect2 version 5.4.0
/boot/system/develop/tools/x86/bin/../lib/gcc/i586-pc-haiku/5.4.0/../../../../i586-pc-haiku/bin/ld 
-plugin /boot/system/develop/tools/x86/bin/../lib/gcc/i586-pc-haiku/5.4.0/liblto_plugin.so 
-plugin-opt=/boot/system/develop/tools/x86/bin/../lib/gcc/i586-pc-haiku/5.4.0/lto-wrapper 
-plugin-opt=-fresolution=/tmp//cc8ASyvd.res -plugin-opt=-pass-through=-lgcc 
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lroot -plugin-opt=-pass-through=-lgcc 
-plugin-opt=-pass-through=-lgcc_s -m elf_i386_haiku -shared -no-undefined /boot/system/develop/lib/x86/crti.o
 /boot/system/develop/tools/x86/bin/../lib/gcc/i586-pc-haiku/5.4.0/crtbeginS.o 
 /boot/system/develop/lib/x86/start_dyn.o /boot/system/develop/lib/x86/init_term_dyn.o -L/boot/system/develop/tools/x86/bin/../lib/gcc/i586-pc-haiku/5.4.0 
 -L/boot/system/develop/tools/x86/bin/../lib/gcc 
 -L/boot/system/develop/tools/x86/bin/../lib/gcc/i586-pc-haiku/5.4.0/../../../../i586-pc-haiku/lib 
 -L/boot/system/develop/tools/x86/bin/../lib/gcc/i586-pc-haiku/5.4.0/../../.. 
 -L/boot/system/develop/lib/x86 -v -lgcc --as-needed -lgcc_s --no-as-needed -lroot -lgcc 
 --as-needed -lgcc_s --no-as-needed /boot/system/develop/tools/x86/bin/../lib/gcc/i586-pc-haiku/5.4.0/crtendS.o /boot/system/develop/lib/x86/crtn.o
GNU ld (GNU Binutils) 2.26.20160125
/boot/system/develop/lib/x86/start_dyn.o: In function `_start':
start_dyn.c:(.text+0x4c): undefined reference to `main'
collect2: error: ld returned 1 exit status
~> 

clang:

~> setarch x86

Switching to architecture x86

~> clang -Xlinker -v
collect2 version 2.95.3-haiku-2014_07_26 (i386 Haiku/ELF)
/packages/gcc-2.95.3_2014_10_14-3/.self/develop/tools/i586-pc-haiku/bin/ld -m elf_i386_haiku 
-shared -Bsymbolic --no-undefined -o a.out /boot/system/develop/lib/crti.o 
/packages/gcc-2.95.3_2014_10_14-3/.self/develop/tools/lib/gcc-lib/i586-pc-haiku/2.95.3-haiku-2014_07_26/crtbeginS.o
 /boot/system/develop/lib/start_dyn.o /boot/system/develop/lib/init_term_dyn.o 
 -L/packages/gcc-2.95.3_2014_10_14-3/.self/develop/tools/lib/gcc-lib/i586-pc-haiku/2.95.3-haiku-2014_07_26 
 -L/packages/gcc-2.95.3_2014_10_14-3/.self/develop/tools/i586-pc-haiku/lib 
 -L/packages/gcc-2.95.3_2014_10_14-3/.self/develop/tools/lib -L/boot/system/develop/lib 
 -v -lroot /packages/gcc-2.95.3_2014_10_14-3/.self/develop/tools/lib/gcc-lib/i586-pc-haiku/2.95.3-haiku-2014_07_26/crtendS.o 
 /boot/system/develop/lib/crtn.o
/boot/system/develop/lib/start_dyn.o: In function `_start':
start_dyn.c:(.text+0x57): undefined reference to `main'
GNU ld version 2.17-haiku-2013_04_21
collect2: ld returned 1 exit status
clang-3.8: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

For me it looks like that clang always tries to link with the old gcc (2.9)?
That seems wrong to me.

Yes, this most likely won’t work. I suggest opening a bug report at haikuports (github.com/haikuports/haikuports) about this, as it is a problem with clang and the way the haikuports team compiled it, not haiku itself.