I was trying to study the compilation/link process in detail using gcc.
So made a test .c file and compiled it using the verbose option of gcc.
gcc -v main.c
But in the verbose messages I cant find anywhere where it refers to libc. The only relevant (might be) thing I find is crtn.o and crtend.o
But when I do
readelf -d a.out
I can find the dependency on libc.so.6. So I am confused whether gcc doesn't output the message related to dynamically linking to libc or I am missing something.
The relevant message for the linker is as below:
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. /tmp/ccYoVOxt.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
The library operand you are looking for is -lc. It's in there, use Ctrl+F to find it.
See also the ld documentation.
Related
I try to build the demo app from pyqtdeploy and I am running into this linker error.
cannot find -lc++
Here is the command which generates the error:
/home/art/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -D__ANDROID_API__=27 -target aarch64-none-linux-android -gcc-toolchain /home/art/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot=/home/art/Android/Sdk/ndk-bundle/platforms/android-27/arch-arm64/ -Wl,-soname,libpyqt-demo.so -Wl,-rpath=/home/art/Qt/5.12.3/android_arm64_v8a/lib -Wl,--no-undefined -Wl,-z,noexecstack -shared -o libpyqt-demo.so pyqtdeploy_main.o pyqtdeploy_start.o pdytools_module.o _abc.o _bisectmodule.o blake2b_impl.o blake2module.o blake2s_impl.o _datetimemodule.o _hashopenssl.o _heapqmodule.o _math.o _posixsubprocess.o _randommodule.o sha3module.o _ssl.o _struct.o binascii.o mathmodule.o selectmodule.o socketmodule.o zlibmodule.o qrc_pyqtdeploy.o -L/home/art/Sourcecode/pyqtdeploy-2.4/demo/sysroot-android-64/lib -L/home/art/Sourcecode/pyqtdeploy-2.4/demo/sysroot-android-64/lib/python3.7/site-packages/PyQt5 -lQtAndroidExtras -lQtCore -lQtGui -lQtNetwork -lQtWidgets -lcrypto -lpython3.7 -lsip -lssl -lz -L/home/art/Qt/5.12.3/android_arm64_v8a/lib -lQt5Widgets -L/opt/android/android-ndk-r18b/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -lQt5Gui -lQt5Network -lQt5AndroidExtras -lQt5Core -lGLESv2 -L/home/art/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a /home/art/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++.so.27 -llog -lz -lm -ldl -lc -v
Android (5220042 based on r346389c) clang version 8.0.7 (https://android.googlesource.com/toolchain/clang b55f2d4ebfd35bf643d27dbca1bb228957008617) (https://android.googlesource.com/toolchain/llvm 3c393fe7a7e13b0fba4ac75a01aa683d7a5b11cd) (based on LLVM 8.0.7svn)
Target: aarch64-none-linux-android
Thread model: posix
InstalledDir: /home/art/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /home/art/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x
Selected GCC installation: /home/art/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x
Candidate multilib: .;#m64
Selected multilib: .;#m64
"/home/art/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld" --sysroot=/home/art/Android/Sdk/ndk-bundle/platforms/android-27/arch-arm64/ -EL --fix-cortex-a53-843419 -z now -z relro --hash-style=both --enable-new-dtags --eh-frame-hdr -m aarch64linux -shared -o libpyqt-demo.so /home/art/Android/Sdk/ndk-bundle/platforms/android-27/arch-arm64//usr/lib/crtbegin_so.o -L/home/art/Sourcecode/pyqtdeploy-2.4/demo/sysroot-android-64/lib -L/home/art/Sourcecode/pyqtdeploy-2.4/demo/sysroot-android-64/lib/python3.7/site-packages/PyQt5 -L/home/art/Qt/5.12.3/android_arm64_v8a/lib -L/opt/android/android-ndk-r18b/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -L/home/art/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -L/home/art/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.7/lib/linux/aarch64 -L/home/art/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x -L/home/art/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/lib/../lib64 -L/home/art/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/lib -L/home/art/Android/Sdk/ndk-bundle/platforms/android-27/arch-arm64//usr/lib --exclude-libs libgcc.a --exclude-libs libatomic.a -soname libpyqt-demo.so -rpath=/home/art/Qt/5.12.3/android_arm64_v8a/lib --no-undefined -z noexecstack pyqtdeploy_main.o pyqtdeploy_start.o pdytools_module.o _abc.o _bisectmodule.o blake2b_impl.o blake2module.o blake2s_impl.o _datetimemodule.o _hashopenssl.o _heapqmodule.o _math.o _posixsubprocess.o _randommodule.o sha3module.o _ssl.o _struct.o binascii.o mathmodule.o selectmodule.o socketmodule.o zlibmodule.o qrc_pyqtdeploy.o -lQtAndroidExtras -lQtCore -lQtGui -lQtNetwork -lQtWidgets -lcrypto -lpython3.7 -lsip -lssl -lz -lQt5Widgets -lQt5Gui -lQt5Network -lQt5AndroidExtras -lQt5Core -lGLESv2 /home/art/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++.so.27 -llog -lz -lm -ldl -lc -lc++ -lm -lgcc -ldl -lc -lgcc -ldl /home/art/Android/Sdk/ndk-bundle/platforms/android-27/arch-arm64//usr/lib/crtend_so.o
/home/art/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: cannot find -lc++
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
Has anybody got an idea how to fix this?
Just in case other people are also running into a similar problem.
I could solve this problem using NDK 19 instead of NDK-20.
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.
While trying to compile a C program using make file, I am facing the following linking error described below.
I know this kind of issues are already discussed in many other posts, but I tried with solutions suggested in them but they all did not work.
/usr/bin/ld: cannot find -lclntsh
libclntsh.so and libclntsh.so.10.1 are present in oracle path /u01/app/oracle/product/10.2.0/lib and that is being given as -L option in make file. Still I am facing the issue:-
Linking yieldrpt ...
cc -v -g -D_HPUX_SOURCE -DPARETO -Wl,-aarchive -L/u01/app/oracle/product/10.2.0/lib/ yieldrpt.o -lclntsh `cat /u01/app/oracle/product/10.2.0/lib/ldflags` -lmalloc -ldl -lm \
-lmalloc -o yieldrpt
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)
/usr/libexec/gcc/i386-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 -o yieldrpt /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/u01/app/oracle/product/10.2.0/lib/ -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2/../../.. -aarchive yieldrpt.o -lclntsh -lnbeq10 -lnhost10 -lnus10 -lnldap10 -lldapclnt10 -lnsslb10 -lntcp10 -lntcps10 -lnsslb10 -lntcp10 -lntns10 -lmalloc -ldl -lm -lmalloc -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crtn.o
/usr/bin/ld: cannot find -lclntsh
collect2: ld returned 1 exit status
make: *** [yieldrpt] Error 1
Please let me know if any other info is needed.
Thank you
You are passing -aarchive to the linker. This is an HP/UX compatibility option
that is synonymous with -Bstatic, which directs the linker not to link dynamic
libraries. Hence your libclntsh.so is ignored. See the documentation
If you don't know why this option is there you may well remove it and try again. If you
know it is necessary then you will have to find static libraries, including libclntsh.a to
link against.
If you have libclntsh.so.12.1 or similar is already installed you may try symlinking the library to libclntsh.so with the following command: ln -s /usr/lib/oracle/12.1/client64/lib/libclntsh.so.12.1 /usr/lib/oracle/12.1/client64/lib/libclntsh.so
For me it was /usr/bin/ld: cannot find -lclntshcore. For some reason, the following symlink is missing.
libclntshcore.so -> /home/ubuntu/ora2pg/instantclient_12_2/libclntshcore.so.12.1
[oracle#em12c bin]$ pwd
/u01/em12c/middleware/oms/bin
[oracle#em12c bin]$ export ORACLE_HOME=/u01/em12c/middleware/oms
[oracle#em12c bin]$ ./genclntsh
[oracle#em12c bin]$ ls -l /u01/em12c/middleware/oms/lib32//libclntsh.so
lrwxrwxrwx. 1 oracle oinstall 17 Oct 23 22:16 /u01/em12c/middleware/oms/lib32//libclntsh.so -> libclntsh.so.11.1
I've downloaded and compiled: http://leenissen.dk/fann/wp/
cmake version 2.8.11.2
gcc (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1
Command used to compile:
cmake -D CMAKE_INSTALL_PREFIX:PATH=/usr .
Installation:
sudo make && sudo make install
Then I go to examples/ directory inside fann project and try to compile examples by running:
make all
I'm getting an error:
gcc -O3 xor_train.c -o xor_train -lfann -lm
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `sin'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `exp'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `cos'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `log'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `pow'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `sqrt'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `floor'
collect2: error: ld returned 1 exit status
make: *** [xor_train] Error 1
Update:
I've followed an instruction given by a library
I've checked on another machine and provided instruction works as intended so I guess my environment is in a some way misconfigured.
Some more info about shared library dependencies:
ldd /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so
linux-vdso.so.1 => (0x00007fff3abfe000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6f3997c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6f39f84000)
As suggested by #michael-burr compiled with -Wl,-v
/usr/bin/ld --sysroot=/ \
--build-id --eh-frame-hdr -m elf_x86_64 \
--hash-style=gnu --as-needed \
-dynamic-linker /lib64/ld-linux-x86-64.so.2 \
-z relro -o xor_train \
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o \
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o \
/usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o \
-L/usr/lib/gcc/x86_64-linux-gnu/4.8 \
-L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu \
-L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib \
-L/lib/x86_64-linux-gnu \
-L/lib/../lib -L/usr/lib/x86_64-linux-gnu \
-L/usr/lib/../lib \
-L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. \
-v /tmp/cc0AHZgU.o -lfann -lm -lgcc --as-needed -lgcc_s --no-as-needed \
-lc -lgcc --as-needed -lgcc_s --no-as-needed \
/usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o \
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
GNU ld (GNU Binutils for Ubuntu) 2.23.52.20130913
Root cause: missing dependencies in FANN library (Will send a patch to author). Such a dependency is called "inter library dependency".
It may happen that one build a shared library A and doesn't have correct dependencies set (let's say B).
In such a case a shared library A will be build without any error msg as it's not required to provide implementation during compiling.
The problem will appear as a lack of library B when one try to create an executable file which depends on A.
In this specific case a solution is to modify a CMake configuration file according to CMake manual
Example changeline:
TARGET_LINK_LIBRARIES(fann m)
It looks like you're compiling your own program as 64-bit, but the FANN library is 32-bit. You might need to specify an architecture for FANN when you compile, which might mean modifying GCC flags in the makefile, unless there are autoconf settings to do it for you. Assuming that you want 64-bit FANN.
Alternatively, you could specify 32-bit architecture when you compile your own code.
Consider the hello world C program:
hello.c:
#include "stdio.h"
int main()
{
printf("Hello, World!\n");
}
If I call:
$ gcc -c hello.c -o hello.o
It will produce an ELF Relocatable File hello.o
If I then call:
$ gcc hello.o -o hello [1]
It will link hello.o with ld and produce an ELF Executable File hello
However if I call ld directly [2] instead of [1]:
$ ld hello.o -o hello [2]
I get these errors:
/usr/bin/ld.bfd.real: warning: cannot find entry symbol _start
test.c:(.text+0xa): undefined reference to `puts'
gcc must be passing other options to ld (to link the C library for example).
Is there anyway to determine exactly what the command-line gcc is passing through to ld in command [1] ?
Yes, you can use gcc -v hello.o -o hello to get the link line. For your example on my ubuntu machine, I get this link line (edited to be multiline for readability):
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/collect2
--build-id
--eh-frame-hdr
-m elf_x86_64
--hash-style=gnu
-dynamic-linker
/lib64/ld-linux-x86-64.so.2
-o hello
-z relro
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.5
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.5
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib
-L/lib/../lib
-L/usr/lib/../lib
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../.. -L/usr/lib/x86_64-linux-gnu
hello.o
-lgcc
--as-needed -lgcc_s --no-as-needed
-lc
-lgcc
--as-needed -lgcc_s --no-as-needed
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crtn.o
Note that collect2 is just an alias for ld.
For oneline lovers:
echo "int main(void) {}" | gcc -o /dev/null -v -x c - &> /dev/stdout| grep collect | tr -s " " "\012"
Replace -x c with -x c++ to get c++ flags.
Can be used also with clang, but in such case you should grep for /usr/bin/ld