openmp not linking correctly when compiling with clang - c

I have built clang 4.0 from source on Ubuntu 16.04 and am trying to compile a simple OpenMP program but receive the following errors.
/tmp/test-7f2c7c.o: In function `main':
/home/me/sf_shared/test.c:(.text+0x52): undefined reference to `__kmpc_fork_call'
/tmp/test-7f2c7c.o: In function `.omp_outlined.':
/home/me/sf_shared/test.c:(.text+0xd9): undefined reference to `__kmpc_for_static_init_4'
/home/me/sf_shared/test.c:(.text+0x16d): undefined reference to `__kmpc_for_static_fini'
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)
To compile I am using ./bin/clang ~/sf_shared/tset.c -fopenmp where bin is the bin folder where I have build clang from source and test.c is a simple openmp program.
Adding -v results in the following
clang version 4.0.1
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/me/release_build/./bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Candidate multilib: .;#m64
Selected multilib: .;#m64
"/home/me/release_build/bin/clang-4.0" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all
-disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model static
-mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables
-fuse-init-array -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /home/me
/release_build/bin/../lib/clang/4.0.1 -internal-isystem /usr/local/include -internal-isystem /home/me
/release_build/bin/../lib/clang/4.0.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu
-internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /home/me
/release_build -ferror-limit 19 -fmessage-length 117 -fopenmp -fobjc-runtime=gcc -fdiagnostics-show-option -o
tmp/test-c9b0bd.o -x c /home/me/sf_shared/test.c
clang -cc1 version 4.0.1 based upon LLVM 4.0.1 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/home/me/release_build/bin/../lib/clang/4.0.1/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
"/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/
ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc
/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtbegin.o -L/usr/
lib/gcc/x86_64-linux-gnu/5.4.0 -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu -L/lib/
x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../.. -L/
home/me/release_build/bin/../lib -L/lib -L/usr/lib /tmp/test-c9b0bd.o -lomp -lgcc --as-needed -lgcc_s
--no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/5.4.0/
crtend.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crtn.o
/tmp/test-c9b0bd.o: In function `main':
/home/me/sf_shared/test.c:(.text+0x52): undefined reference to `__kmpc_fork_call'
/tmp/test-c9b0bd.o: In function `.omp_outlined.':
/home/me/sf_shared/test.c:(.text+0xd9): undefined reference to `__kmpc_for_static_init_4'
/home/me/sf_shared/test.c:(.text+0x15f): undefined reference to `__kmpc_for_static_fini'
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)
Any idea as to why the openmp sections are not being linked correctly?
I've include test.c below if anyone is curious
#include <omp.h>
#include <stdio.h>
int main() {
printf("Max threads: %d\n", omp_get_max_threads());
omp_set_dynamic(0);
omp_set_num_threads(omp_get_max_threads());
#pragma omp parallel for
for (int i = 0; i < 32; ++i) {
printf("I am thread %d\n", omp_get_thread_num());
}
}

Installing the libiomp5 package and changing -fopenmp to -fopenmp=libiomp5 when compiling has resolved the issue.

Related

Cannot cross-compiling code using clang++

Command:
I am trying to cross compile a simple C++ program using clang++. I'm using Linaro gcc tool-chain to obtain the library and other includes required.
${root}/bin/clang++ --target=arm-linux-gnueabihf --rtlib=compiler-rt --stdlib=libc++ -nostdinc++ -I${root}/include/c++/v1 -Wl,-L${root}/lib --sysroot ${sysroot} --gcc-toolchain=/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf -rpath ${root}/lib TestCodeX86toARM.cpp -o Test -v
The value of root and sysroot is as follows:
root=/path/to/clang/install_dir
sysroot=/path/to/linarogcc/arm-linux-gnueabihf/libc
TestCodeX86toARM.cpp is just a hello world code
output:
clang version 10.0.0
Target: arm-unknown-linux-gnueabihf
Thread model: posix
InstalledDir: /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/bin
Found candidate GCC installation: /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0
Selected GCC installation: /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0
Candidate multilib: .;#m32
Selected multilib: .;#m32
"/home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang-10" -cc1 -triple armv6kz-unknown-linux-gnueabihf -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name TestCodeX86toARM.cpp -mrelocation-model static -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -target-cpu arm1176jzf-s -target-feature +strict-align -target-abi aapcs-linux -mfloat-abi hard -fallow-half-arguments-and-returns -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -v -nostdinc++ -resource-dir /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/clang/10.0.0 -I /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/include/c++/v1 -isysroot /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc -internal-isystem /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/local/include -internal-isystem /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/clang/10.0.0/include -internal-externc-isystem /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/include -internal-externc-isystem /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/include -fdeprecated-macro -fdebug-compilation-dir /home/user/Tejas/CrossCopileTestCode -ferror-limit 19 -fmessage-length 0 -fno-signed-char -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o /tmp/TestCodeX86toARM-438a38.o -x c++ TestCodeX86toARM.cpp
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/local/include"
ignoring nonexistent directory "/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/include"
#include "..." search starts here:
#include <...> search starts here:
/home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/include/c++/v1
/home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/clang/10.0.0/include
/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/include
End of search list.
"/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld" --sysroot=/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc -EL -z relro -X --hash-style=gnu --eh-frame-hdr -m armelf_linux_eabi -dynamic-linker /lib/ld-linux-armhf.so.3 -o Test /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/../lib/crt1.o /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/../lib/crti.o /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/crtbegin.o -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0 -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/lib/../lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/../lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/../lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib -L/home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib -rpath /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib /tmp/TestCodeX86toARM-438a38.o -lc++ -lm /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/clang/10.0.0/lib/linux/libclang_rt.builtins-armhf.a -lc /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/clang/10.0.0/lib/linux/libclang_rt.builtins-armhf.a /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/crtend.o /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/../lib/crtn.o
/home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libc++.so.1: file not recognized: File format not recognized
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
Your linker is trying to link against libc++. However clang cant find this library in your arm tool-chain ( probably your arm tool-chain is providing libstdc++). So the only libc++ found by the linker is /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libc++.so.1 which is probably an x86 library ( you can confirm that with objdump or readelf).
Try to replace --stdlib=libc++ by --stdlib=libstdc++ in your command line.

clang: linker command failed with exit code 1

I checked multiple questions like this, but none of the answers worked in my case.
I have a simple "hello world!" c program. When I tried to run it using clang, I get the following error:
/..//bin/ld: cannot find -lgcc
/..//bin/ld: cannot find -lgcc_s
clang: error: linker command failed with exit code 1 (use -v to see invocation)
when I do -v it gives me:
clang version 3.4 (branches/release_34)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Selected GCC installation:
"/nfs/projects/zephyr/Ruturaj/softboundcets-34/softboundcets-llvm-clang34/Debug+Asserts/bin/clang" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name agsd.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-linker-version 2.29.1 -v -resource-dir /nfs/projects/zephyr/Ruturaj/softboundcets-34/softboundcets-llvm-clang34/Debug+Asserts/bin/../lib/clang/3.4 -internal-isystem /usr/local/include -internal-isystem /nfs/projects/zephyr/Ruturaj/softboundcets-34/softboundcets-llvm-clang34/Debug+Asserts/bin/../lib/clang/3.4/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /projects/zephyr/Ruturaj -ferror-limit 19 -fmessage-length 96 -mstackrealign -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /tmp/agsd-ad6d7d.o -x c agsd.c
clang -cc1 version 3.4 based upon LLVM 3.4 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/nfs/projects/zephyr/Ruturaj/softboundcets-34/softboundcets-llvm-clang34/Debug+Asserts/bin/../lib/clang/3.4/include
/usr/include
End of search list.
"/..//bin/ld" --hash-style=gnu --no-add-needed --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /lib/../lib64/crt1.o /lib/../lib64/crti.o /lib/../lib64/crtbegin.o -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/agsd-ad6d7d.o -L/usr/lib/ -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed crtend.o /lib/../lib64/crtn.o
/..//bin/ld: cannot find -lgcc
/..//bin/ld: cannot find -lgcc_s
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I tried to run it with:
clang -L /usr/lib/gcc/x86_64-redhat-linux/8
But, it is still not working.
I installed newer llvm (6.0) and it worked fine. But, I need this 3.4 version to run a project.

C: Undefined reference to LibConfuse internal functions

I am testing LibConfuse on Code::Blocks trying to build the simplest first example on the tutorial:
#include <stdio.h>
#include <confuse.h>
int main(void)
{
cfg_opt_t opts[] =
{
CFG_STR("target", "World", CFGF_NONE),
CFG_END()
};
cfg_t *cfg;
cfg = cfg_init(opts, CFGF_NONE);
if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR)
return 1;
printf("Hello, %s!\n", cfg_getstr(cfg, "target"));
cfg_free(cfg);
return 0;
}
But I get the below errors when compiling:
Undefined reference to "cfg_init".
Undefined reference to "cfg_parse".
Undefined reference to "cfg_getstr".
Undefined reference to "cfg_free".
Note that there are no errors about the #include <confuse.h> line nor about the constans, like CFGF_NONE.
I installed LibConfuse with:
# sudo apt-get update
# sudo apt-get install libconfuse*
What is going on here and how can I solve it?
Further Data:
Operating System: Ubuntu 16.04 .
Tested on two different PC computers.
Tested on RaspBerry Pi running RaspBian.
Code::Blocks version 17.12 .
Tested compiling on Clang with same results.
Another detail: I use to cross-compile to FreeBSD target, so I tested this basic program with my usual procedure to generate binaries for FreeBSD x64:
$ clang -target x86_64-unknown-freebsd10.0 --sysroot=/opt/cross-freebsd-10/ main.c
/tmp/main-0044e3.o: En la función `main':
main.c:(.text+0x96): referencia a `cfg_init' sin definir
main.c:(.text+0xb3): referencia a `cfg_parse' sin definir
main.c:(.text+0xde): referencia a `cfg_getstr' sin definir
main.c:(.text+0x104): referencia a `cfg_free' sin definir
clang: error: linker command failed with exit code 1 (use -v to see invocation)
As can be seen, results are the same.
More details about compilation (to x64 Linux) when using clang :
$ clang main.c -v clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8.5
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.0.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Candidate multilib: .;#m64
Selected multilib: .;#m64
"/usr/lib/llvm-3.8/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib/llvm-3.8/bin/../lib/clang/3.8.0 -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /media/Almacen01/Temporal/CPlusPlus/PruebaGeneral -ferror-limit 19 -fmessage-length 83 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/main-06952f.o -x c main.c
clang -cc1 version 3.8.0 based upon LLVM 3.8.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
"/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../.. -L/usr/lib/llvm-3.8/bin/../lib -L/lib -L/usr/lib /tmp/main-06952f.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crtn.o
/tmp/main-06952f.o: En la función `main':
main.c:(.text+0x96): referencia a `cfg_init' sin definir
main.c:(.text+0xb3): referencia a `cfg_parse' sin definir
main.c:(.text+0xde): referencia a `cfg_getstr' sin definir
main.c:(.text+0x104): referencia a `cfg_free' sin definir
clang: error: linker command failed with exit code 1 (use -v to see invocation)
UPDATE:
I have tested LibConfig (as long as it is supposed to implement equivalent features) :
sudo apt-get install libconfig-dev
and, when trying to compile the included /examples/example1.c file, I get equivalent results: multiple undefined references on LibConfig functions.
You need to tell the linker where to find the libraries.
Check https://c-for-dummies.com/blog/?p=2159.

Cannot link to the C standard library on macOS

I'm trying to build the following example code in a step-by-step fashion:
#include <stdio.h>
int main(void) {
puts("hello, world");
}
Here is my Makefile:
CC=clang
CFLAGS=-g
LD=ld
LDFLAGS=-macosx_version_min 10.12
LDLIBS=-L/usr/lib/system/
a.out: main.o
$(LD) $(LDFLAGS) $< $(LOADLIBES) $(LDLIBS)
main.o: main.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
It has no problems creating main.o, but throws an error in the linking stage:
$ make
ld -macosx_version_min 10.12 main.o -L/usr/lib/system/
Undefined symbols for architecture x86_64:
"_puts", referenced from:
_main in main.o
ld: symbol(s) not found for inferred architecture x86_64
make: *** [a.out] Error 1
It seems that ld can't find the C standard library. What am I doing wrong?
For your reference, this is the output of clang main.c -v, which does the linking correctly:
$ clang main.c -v
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
"/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 278.4 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.1.0 -fdebug-compilation-dir /Users/sunqingyao/Projects/play-ground -ferror-limit 19 -fmessage-length 167 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/main-f5cd6b.o -x c main.c
clang -cc1 version 8.1.0 (clang-802.0.42) default target x86_64-apple-darwin16.7.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.1.0/include
/Library/Developer/CommandLineTools/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out /var/folders/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/main-f5cd6b.o -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a
I tried copying the last line into the Makefile, only to get a file not found error:
$ make
/Library/Developer/CommandLineTools/usr/bin/ld -macosx_version_min 10.12 main.o -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out /var/folders/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/main-f5cd6b.o -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a
ld: file not found: /var/folders/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/main-f5cd6b.o
make: *** [a.out] Error 1
It's unusual these days to use ld directly to link a binary, and under macOS you need to add -lc to force it to include the standard C library.
Generally it's better to use cc to invoke the linker, and it'll arrange to pass the right flags.
This Makefile works for me:
CC=clang
CFLAGS=-g -mmacosx-version-min=10.12
main: main.o
$(CC) -o main $(CFLAGS) $(LDFLAGS) main.o

Homebrew-installed Clang fails to find headers on Mac OS X

So, for no apparent reason (I haven't installed a new version of clang, or changed any compiler configuration), clang suddenly can't find assert.h on my Mac.
In my development VM, running Ubuntu, both gcc and clang compile the project fine; on the host Mac OS X, gcc still compiles it fine, but clang spits out the following:
> make
clang -g -O0 -Wall -Wextra -pedantic -Wshadow -Wstrict-overflow -Wno-missing-field-initializers -std=c99 -m64 -Wno-unused-variable -Wno-unused-parameter -o csim csim.c cachelab.c -lm
csim.c:1:10: fatal error: 'assert.h' file not found
#include <assert.h>
^
1 error generated.
cachelab.c:5:10: fatal error: 'assert.h' file not found
#include <assert.h>
^
1 error generated.
make: *** [csim] Error 1
Here's some further relevant information:
> xcode-select --print-path
/Applications/Xcode-beta.app/Contents/Developer
> gcc -g -O0 -Wall -Wextra -pedantic -Wshadow -Wstrict-overflow -Wno-missing-field-initializers -std=c99 -m64 -Wno-unused-variable -Wno-unused-parameter -o csim csim.c cachelab.c -lm
> clang -std=c99 -o csim csim.c -v
clang version 3.8.1 (tags/RELEASE_381/final)
Target: x86_64-apple-darwin16.0.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
"/usr/local/Cellar/llvm/3.8.1/bin/clang-3.8" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name csim.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 273 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /usr/local/Cellar/llvm/3.8.1/bin/../lib/clang/3.8.1 -std=c99 -fdebug-compilation-dir /Users/ec/Dropbox/Documents/Homework/CS351/mps/04 -ferror-limit 19 -fmessage-length 211 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/xt/wm01x2h50nv993t_csb1t5qm0000gn/T/csim-9590db.o -x c csim.c
clang -cc1 version 3.8.1 based upon LLVM 3.8.1 default target x86_64-apple-darwin16.0.0
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/local/Cellar/llvm/3.8.1/bin/../lib/clang/3.8.1/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
csim.c:1:10: fatal error: 'assert.h' file not found
#include <assert.h>
^
1 error generated.

Resources