The conjugate function for clang 3.4.1 on FreeBSD fails?

Dealing with the conjugate of complex numbers brought me some linker failures for clang 3.4.1 on FreeBSD 11 CURRENT amd64:

vanja@current:/tmp % cat konjugat.c
#include <complex.h>
#include <stdio.h>

int main(void)
{
double complex kpx = 1.0 + 3.0*I;
double complex konjugat = conj(kpx);
printf(“Kompleksni broj je %.2f%+.2fi\n”, creal(kpx), cimag(kpx));
printf(“Konjugat od kpx je %.2f%+.2fi\n”, creal(konjugat) ,cimag(konjugat));
return 0;
}

Clang fails to link the program:

vanja@current:/tmp % clang -o konjugat konjugat.c
/tmp/konjugat-86380f.o: In function `main’:
konjugat.c:(.text+0x56): undefined reference to `conj’
clang: error: linker command failed with exit code 1 (use -v to see invocation)

With the “-v” switch, there are a bit more details about it:

vanja@current:/tmp % clang -o konjugat konjugat.c -v
FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512
Target: x86_64-unknown-freebsd11.0
Thread model: posix
Selected GCC installation:
“/usr/bin/clang” -cc1 -triple x86_64-unknown-freebsd11.0 -emit-obj -mrelax-all -disable-free -main-file-name konjugat.c -mrelocation-model static -mdisable-fp-elim -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -v -resource-dir /usr/bin/../lib/clang/3.4.1 -fdebug-compilation-dir /tmp -ferror-limit 19 -fmessage-length 213 -mstackrealign -fobjc-runtime=gnustep -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /tmp/konjugat-eaa539.o -x c konjugat.c
clang -cc1 version 3.4.1 based upon LLVM 3.4.1 default target x86_64-unknown-freebsd11.0
ignoring nonexistent directory “/usr/bin/../lib/clang/3.4.1/include”
#include “…” search starts here:
#include <…> search starts here:
/usr/include/clang/3.4.1
/usr/include
End of search list.
“/usr/bin/ld” –eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 –hash-style=both –enable-new-dtags -o konjugat /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib /tmp/konjugat-eaa539.o -lgcc –as-needed -lgcc_s –no-as-needed -lc -lgcc –as-needed -lgcc_s –no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o
/tmp/konjugat-eaa539.o: In function `main’:
konjugat.c:(.text+0x56): undefined reference to `conj’
clang: error: linker command failed with exit code 1 (use -v to see invocation)

However, the GCC crunches it just fine:

vanja@current:/tmp % gcc48 -o konjugat konjugat.c

And running the program works as expected:

vanja@current:/tmp % ./konjugat
Kompleksni broj je 1.00+3.00i
Konjugat od kpx je 1.00-3.00i

The versions are:

vanja@current:/tmp % clang –version
FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512
Target: x86_64-unknown-freebsd11.0
Thread model: posix
vanja@current:/tmp % gcc48 –version
gcc48 (FreeBSD Ports Collection) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Edit:

It takes a mathematician to sort this one out (thank you tiksa!) but if you don’t have a mathematician at hand, man pages will suffice, too. Just make sure those are linux man pages for conj(), because BSD’s don’t have the necessary hint “Link with -lm” in the text!
When compiled with “-lm” switch for the linker, clang will happily compile everything just as if it was gcc itself! Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *