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
Related
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.
I decided to learn how to make own OS by tutorial, I wrote boot on NASM and kernel on C also linker.ld and makefile. When I try linking all together I have this kind of error:
ld: library not found for -lgcc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Also, I have installed binutils using homebrew but it doesn't help me. I have Mac OS 10.14
And this is my makefile:
CC = gcc
main: kernel.c linker.ld boot.asm
nasm -felf32 boot.asm -o boot.o
$(CC) -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -Wall -Wextra
$(CC) -T linker.ld -o basilica.bin -ffreestanding -nostdlib boot.o kernel.o -lgcc
cp basilica.bin isodir/boot/basilica.bin
grub-mkrescue -o basilica.iso isodir
clean:
rm ./*.o ./*.bin ./*.iso ./isodir/boot/*.bin
What am I doing wrong? How can I fix this error: ld: library not found for -lgcc? Please help me.
I'm learning compiler construction following the book Modern Compiler Implementation in C. In the introduction there is a "PROGRAM STRAIGHT-LINE PROGRAM INTERPRETER", which it said is "available in the directory $TIGER/chap1", so I downloaded the TIGER compiler from https://www.cs.princeton.edu/~appel/modern/c/project.html.
In the chap1 directory, there some files:
chap1$ ls
1.png makefile prog1.h slp.h util.h
main.c prog1.c slp.c util.c
So I execute make command in the 'chap1' directory , but it shows an error:
chap1$ make
cc -g -c main.c
cc -g -c prog1.c
cc -g -c slp.c
cc -g -c util.c
cc -g main.o prog1.o slp.o util.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [a.out] Error 1
Here is the makefile:
a.out: main.o prog1.o slp.o util.o
cc -g main.o prog1.o slp.o util.o
main.o: main.c slp.h util.h
cc -g -c main.c
prog1.o: prog1.c slp.h util.h
cc -g -c prog1.c
slp.o: slp.c slp.h util.h
cc -g -c slp.c
util.o: util.c util.h
cc -g -c util.c
clean:
rm -f a.out util.o prog1.o slp.o main.o
It seems the chap1 directory is a complete project, but I don't know what the function of chap1 directory in TIGER compiler and how to use it.
In file main.c, there is not main function. Please provide it in this file. Here is the template:
#include "util.h"
#include "slp.h"
#include "prog1.h"
int main()
{
A_stm stm = prog();
return 0;
}
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.
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.