DSO missing from command line although it is available - linker

I am working with c++ code for a physics simulation, which uses a lot of external libraries (like GSL and cern`s ROOT). Trying to recompile project I encountered problems with linking. When running compilation of final file via:
g++ -fno-inline -O2 -fpic -o main.out ${ROOTINCS} main.o ext.o ${ROOTLIBS} $(objects2)
with :
objects2= many .o files made by us
ROOTLIBS=-L/usr/local/lib/root -lTree -lRIO -lNet -lHist -lMathCore -lCore -lGraf -lGraf3d -lGpad -lMatrix -lThread -lCint -lPhysics -lPostscript -lRint -lSpectrum -lg
ROOTINCS=-pthread -m64
I get annoying error:
/usr/bin/ld: /usr/local/lib/root/libHist.so: undefined reference to symbol 'gRandom'
/usr/local/lib/root/libMathCore.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
The problem is nm -C run on libMathCore states 'gRandom' is declared there. Also -lMathCore is present in my command line.
When I run ld to check if it understands the flag:
ld -L/usr/local/lib/root -lMathCore --verbose 2>/dev/null
it does not complain and tries to link properly.
According to https://stackoverflow.com/a/24675715/3602168 order of libraries is correct in my linking (libHist uses libMathCOre and therefore is stated first).
Compilation runs under g++ 4.8.2 on ubuntu 14.04, 64 bit

Converting comment to answer:
Have you tried moving $(objects2) before ${ROOTLIBS}? I think the issue may be that you have libraries specified before the object files that use them.

Related

Error trying to install glibc in WSL: relocation R_X86_64_PC32 against undefined symbol `__GI___open64_nocancel'

When trying to install the latest glibc into my machine provided I executed the "config" executable with flags: ../configure CFLAGS="-O3 -fPIC" --disable-sanity-checks it generates the makefile correctly, although I think disable-sanity-checks will give some errors.
But whenever I try to execute the generated Makefile using make, the console prints the following error:
make[3]: Leaving directory '/mnt/c/Users/SCP173/Downloads/Thread/glib/glibc/elf'
gcc -nostdlib -nostartfiles -shared -o /mnt/c/Users/SCP173/Downloads/Thread/glib/glibc/glibc-build/elf/ld.so.new \
-Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both -Wl,-z,defs \
/mnt/c/Users/SCP173/Downloads/Thread/glib/glibc/glibc-build/elf/librtld.os -Wl,--version-script=/mnt/c/Users/SCP173/Downloads/Thread/glib/glibc/glibc-build/ld.map
\
-Wl,-soname=ld-linux-x86-64.so.2 \
-Wl,-defsym=_begin=0
/mnt/c/Users/SCP173/Downloads/Thread/glib/glibc/glibc-build/elf/librtld.os: In function `process_envvars':
/mnt/c/Users/SCP173/Downloads/Thread/glib/glibc/elf/rtld.c:2686: undefined reference to `__GI___open64_nocancel'
/usr/bin/ld: /mnt/c/Users/SCP173/Downloads/Thread/glib/glibc/glibc-build/elf/librtld.os: relocation R_X86_64_PC32 against undefined symbol `__GI___open64_nocancel' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
As I said, I tried to add the -fPIC flag but still didn't catch it. And I'm kind of new to .os, .map and other extensions such as those. Am I doing it completely wrong? Or else, how could I compile just that file using gcc instead of passing the flag to the whole make command?
Your file system is not set up correctly. By default, NTFS is case-preserving, but case-insensitive. As far as I understand it, current versions of WSL set up only part of the file system tree in case-sensitive mode. (A previous system-wide configuration is now disregarded.)
Building glibc requires a case-sensitive file system. Please also note that WSL is not a proper development environment for glibc because there are so many test failures due to bugs in the WSL kernel emulation.

Linker issue in g++

I have the following .sh file (from here).
g++ -c -pipe -g -std=gnu++11 -Wall -W -fPIC -I. -I./tensorflow
-I./tensorflow/bazel-tensorflow/external/eigen_archive -I./tensorflow/bazel-tensorflow/external/protobuf/src -I./tensorflow/bazel-genfiles -o main.o ./main.cpp
g++ -o Tutorial main.o -L./tensorflow/bazel-bin/tensorflow
-ltensorflow_cc
cp ./tensorflow/bazel-bin/tensorflow/libtensorflow* .
When I try to run this .sh file from terminal I got an error. Therefore I executed the commands one by one. First one worked fine and I saw that when I run the second command ( g++ -o Tutorial main.o -L./tensorflow/bazel-bin/tensorflow
-ltensorflow_cc) I get the following error.
/usr/bin/ld: main.o: undefined reference to symbol '_ZN10tensorflow3Env19NewRandomAccessFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPSt10unique_ptrINS_16RandomAccessFileESt14default_deleteISA_EE'
libtensorflow_framework.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I saw the answer here and I see it as closely related to mine. But I cannot figure out how to adapt it to my problem.
Can someone please help with this?
The linker is saying that the linkage requires shared library libtensorflow_framework.so (presumably because -ltensorflow_cc depends on it and requests it) but is not given in your commandline. This should be solved by adding -ltensorflow_framework at the end, with an additional -L option if necessary.
I was too getting the same error.
If you are using tensorflow 2, then you need to link .so.2 files. You should find them in the bazel build directory. For me it is :
/tmp/bazel/output/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow
I linked the files using the below in my CMAKE:
file(GLOB LIBRARIES "${bazel_bin}/tensorflow/*.so.2")
message("LIBRARIES = ${LIBRARIES}")

Linking with shared libraries

I'm trying to compile and link some .c file. I have been using Eclipse IDE for C/C++ developers, and in my local machine i can compile without problems. However, when i try to compile and link the same file in a RedHat OS (gcc version is 4.9.2-6 in this OS) i'm having problems. I get some warnings at compile time, but those are fine, i think, i just ignored and the application still runs fine. Here are the commands i executed and the associated output:
gcc -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 -MMD -MP -MF"example.d" -MT"example.d" -o "example.o" "example.c"
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
warning: implicit declaration of function ‘wait’ [-Wimplicit-function-declaration]
This generates two files, example.d and example.o. Then, i try to link them, without luck, with the following command:
gcc -Xlinker -L/usr/lib -lrt -static -pthread example.o -o example
/usr/bin/ld: cannot find -lrt
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
The commands are taken directly from the ones that Eclipse generates, and work just fine in my local computer (Ubuntu OS) but not in the RedHat environment. The last command didn't work, with and without the -L option. I suppose the directory in -L is fine, as i run, for example,
locate libpthread.so
And one of the locations i get is /usr/lib (also /usr/lib64, but neither work).
Any help will be greatly appreciated!! :)
If you try to link a static executable, it will look for the *.a versions of the libraries, not what you usually want. Remove the -static flag. Or you can install the static libraries if you really want to. It also should not be necessary to add -L/usr/lib explicitly.

/lib/libmatrix.a: file not recognized: File format not recognized collect2: ld returned 1 exit status

I'm trying to compile a makefile which includes the following line:
gcc -I. -I/home/usr/Documents/MTV/include -ggdb3 -Wall -O2 -o ascii2bin.c \
-L. -L../lib -lmatrix -lseq_io -lpic -lm
And this is what I get:
../lib/libmatrix.a: file not recognized: File format not recognized
collect2: ld returned 1 exit status
Any idea on what might happen to libmatrix.a? How can I read what's inside libmatrix.a? I tried using the 'ar -t' command, but it also says file format not recognized.
The project was compiled on Cygwin before by others, and now I'm using ubuntu gcc to try to redo it, could this be the problem?
A library file built for cygwin will not work on linux.
The library itself must be recompiled from source to match the details (ABI, dynamic system library dependencies, etc) of the system on which it is intended to be used.
Cygwin tries to be source compatible with Linux, so if you have the source rebuilding may be straightforward. But it is not binary-compatible, and libraries are basically binary building blocks with metadata to permit linking them together.

compilation error if use --static flag with gcc

When I try to compile static, I am getting the following error:
gcc defrag.c -o abc.exe --static
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
However, the same thing compiles fine without static:
gcc defrag.c -o abc.exe
Question: Why did the compilation failed when static is specified?
The error is occurring becuase "--static" says that all subsequent libraries in your link command must be static ... but you only have a dynamic libc on your system.
Recommended solution:
gcc defrag.c -o abc -lc --static -lmystaticlib
If you're just trying to create a static exe for the sake of having a static exe - I'd recommend "don't". Shared libraries are Good. For many different reasons.
Here's a good link:
Linux static linking is dead?

Resources