ld: library not found for - linker

I'm hitting a linker error (compile time, not run time):
ld: library not found for -l:/usr/lib/libobjc.A.dylib
That's just the latest version of the error. I'm trying to link against /usr/lib/libobjc.A.dylib, and I want to hard-code the path; the library is guaranteed to exist at this path on the platform I'm on. And it does:
bhaller#lanois % ls -l /usr/lib/libobjc.A.dylib
-rwxr-xr-x 1 root wheel 859536 Jan 23 08:59 /usr/lib/libobjc.A.dylib
I have tried:
-L/usr/lib/ -llibobjc.A.dylib
-L/usr/lib/ -llibobjc.A
-l/usr/lib/libobjc.A.dylib
-l:/usr/lib/libobjc.A.dylib
In every case I get the same error, that ld couldn't find the library. But it's right there, and I'm giving the complete path to it! What am I missing??
Here's the full invocation, with a bunch of other cruft:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -headerpad_max_install_names -macosx_version_min 10.10.0 -o QtSLiM.app/Contents/MacOS/QtSLiM -L/Users/bhaller/Desktop/QtSLiM QMAKE/QtSLiM/../core/ -L/Users/bhaller/Desktop/QtSLiM QMAKE/QtSLiM/../eidos/ -L/Users/bhaller/Desktop/QtSLiM QMAKE/QtSLiM/../treerec/tskit/ -L/Users/bhaller/Desktop/QtSLiM QMAKE/QtSLiM/../gsl/ -L/Users/bhaller/Desktop/QtSLiM QMAKE/QtSLiM/../eidos_zlib/ -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -rpath #executable_path/Frameworks -rpath /Users/bhaller/Qt5.9.8/5.9.8/clang_64/lib main.o QtSLiMWindow.o QtSLiMAppDelegate.o QtSLiMWindow_glue.o QtSLiMChromosomeWidget.o QtSLiMExtras.o QtSLiMPopulationTable.o QtSLiMIndividualsWidget.o QtSLiMEidosPrettyprinter.o QtSLiMAbout.o QtSLiMPreferences.o QtSLiMSyntaxHighlighting.o QtSLiMFindRecipe.o QtSLiMHelpWindow.o QtSLiMScriptTextEdit.o QtSLiMEidosConsole.o QtSLiMEidosConsole_glue.o QtSLiMConsoleTextEdit.o QtSLiM_SLiMgui.o QtSLiMTablesDrawer.o QtSLiMFindPanel.o QtSLiMGraphView.o QtSLiMGraphView_FixationTimeHistogram.o QtSLiMGraphView_FrequencySpectra.o QtSLiMGraphView_LossTimeHistogram.o QtSLiMGraphView_PopulationVisualization.o QtSLiMGraphView_FitnessOverTime.o QtSLiMGraphView_FrequencyTrajectory.o QtSLiMHaplotypeManager.o QtSLiMHaplotypeOptions.o QtSLiMHaplotypeProgress.o QtSLiMVariableBrowser.o qrc_buttons.o qrc_icons.o qrc_recipes.o qrc_help.o moc_QtSLiMWindow.o moc_QtSLiMAppDelegate.o moc_QtSLiMChromosomeWidget.o moc_QtSLiMExtras.o moc_QtSLiMPopulationTable.o moc_QtSLiMIndividualsWidget.o moc_QtSLiMAbout.o moc_QtSLiMPreferences.o moc_QtSLiMSyntaxHighlighting.o moc_QtSLiMFindRecipe.o moc_QtSLiMHelpWindow.o moc_QtSLiMScriptTextEdit.o moc_QtSLiMEidosConsole.o moc_QtSLiMConsoleTextEdit.o moc_QtSLiMTablesDrawer.o moc_QtSLiMFindPanel.o moc_QtSLiMGraphView.o moc_QtSLiMGraphView_FixationTimeHistogram.o moc_QtSLiMGraphView_FrequencySpectra.o moc_QtSLiMGraphView_LossTimeHistogram.o moc_QtSLiMGraphView_PopulationVisualization.o moc_QtSLiMGraphView_FitnessOverTime.o moc_QtSLiMGraphView_FrequencyTrajectory.o moc_QtSLiMHaplotypeManager.o moc_QtSLiMHaplotypeOptions.o moc_QtSLiMHaplotypeProgress.o moc_QtSLiMVariableBrowser.o -lcore -leidos -ltskit -lgsl -leidos_zlib -l:/usr/lib/libobjc.A.dylib -framework QtOpenGL -framework QtWidgets -framework QtGui -framework QtCore -framework DiskArbitration -framework IOKit -framework OpenGL -framework AGL -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/lib/darwin/libclang_rt.osx.a -F/Users/bhaller/Qt5.9.8/5.9.8/clang_64/lib
ld: library not found for -l:/usr/lib/libobjc.A.dylib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [QtSLiM.app/Contents/MacOS/QtSLiM] Error 1
make: *** [sub-QtSLiM-make_first] Error 2
I feel like I must just be an idiot, but I can't see it. Help?

Your linkage command shows that your linker is the OS X linker. Refer to it's manual
with respect to the -l option:
-lx
This option tells the linker to search for libx.dylib or libx.a in the library search path.
If string x is of the form y.o, then that file is searched for in the same places, but
without prepending lib or appending .a or .dylib to the filename.
This will show you why all of:
-L/usr/lib/ -llibobjc.A.dylib
-L/usr/lib/ -llibobjc.A
-l/usr/lib/libobjc.A.dylib
-l:/usr/lib/libobjc.A.dylib
are wrong. In each of your attempts, where name is whatever you have typed after -l,
the linker will be directed to search for libname.dylib or libname.a. Of course
none of those files - e.g. liblibobjc.A.dylib.dylib,liblibobjc.A.dylib.a - exists
in /usr/lib/ or any of the other specified or default search directories, and
in the third and fourth attempts, libname.(dylib|a) resolves to strings that that
cannot even possibly be unqualified filenames.
Your final variation evidently wants to used -l:name in an attempt to make the linker
search precisely for name ( = /usr/lib/libobjc.A.dylib). However,
-l:name is an option for the GNU Binutils linker, (the default linker in Linux)
and as per the documentation, is not an option for the OS X linker. (Furthermore, that option of the Binutils linker
directs the linker to search in each directory for a file with the exact unqualified name name, which /usr/lib/libobjc.A.dylib
could not be).
To get the linker to locate libobjc.A.dylib in /usr/lib/ you require the option:
-lobjc.A
Consider for example the other -l options in your your linkage command:
-lcore -leidos -ltskit -lgsl -leidos_zlib
Before you try that however, I suggest that you check in /usr/lib for the existence
of a symbolic link:
libobjc -> libobjc.A
This would indicate that libobjc.A is the version of libobjc that your toolchain
is configured to select, and that the linkage option you require is simply:
-lobjc
The search option -L/usr/lib/ is in any case redundant, since /usr/lib/ is a default
linker search directory.

Related

a contradictory result when checking an example of "Linking with external libraries"

I just started to learn gcc/g++ by reading "An introduction to GCC"
In chapter 3.1 the author showed an example of linking with external libraries and header files. The main source code uses 'gdbm.h' header (installed at /opt/gdbm-1.8.3/include/ in text book) file and the library ‘libgdbm.a’ (installed at /opt/gdbm-1.8.3/lib/ in text book). The process of linking with the library and header file is as follows:
Link to example screenshot
When I tried to repeat this, I got a contradictory result. When I used method 1 (in the example fails) which only includes header file path, the program can be compiled, however, when I used method 2 (in the example succeeds), I got an error, saying that
"ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1"
(this is my full command line and error message:)
userMacBook-Pro:03 user$gcc -Wall -I/usr/local/include dbmain.c -L/usr/local/lib dbmain.c -lgdbm
duplicate symbol _main in:
/var/folders/6_/09vfzzms7dq1d73vl4mwlxmh0000gn/T/dbmain-9d15f9.o
/var/folders/6_/09vfzzms7dq1d73vl4mwlxmh0000gn/T/dbmain-907e96.o
this is my command that succeeds:
userMacBook-Pro:03 user$ gcc -Wall -I/usr/local/include dbmain.c -lgdbm
userMacBook-Pro:03 user$ ./a.out
Storing key-value pair... done.
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Can anyone explain why this happens?
I don't know which platform the author uses, I just use mac OS X.
gcc -Wall -I/usr/local/include dbmain.c -L/usr/local/lib dbmain.c -lgdbm
The file dbmain.c is present twice on your command line. For this reason the main function is present twice.

DSO missing from command line although it is available

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.

arm-gcc unresolved reference 'sinf'

I'm getting this error when trying to compile:
error: undefined reference to `sinf'
I have included math.h, and verified that it is defined in there:
#include <math.h>
However, I get an error while trying to link to the math library:
arm-none-eabi-ld -L/usr/lib -lm --gc-sections -T ../standalone.ld -o "main.elf" ./main.o ./startup_gcc.o
error: cannot find -lm
However, the library is obviously there:
Kens-MacBook-Pro:lib Ken$ pwd
/usr/lib
Kens-MacBook-Pro:lib Ken$ ls | grep libm
libm.dylib
libmecab.1.0.0.dylib
libmecab.dylib
libmecabra.dylib
libmenu.5.4.dylib
libmenu.dylib
libmx.A.dylib
libmx.dylib
Kens-MacBook-Pro:lib Ken$
What am I doing wrong? I'm using Eclipse.
Judging by your arm-none-eabi-ld command, I'm assuming you're cross compiling. You need to install some sort of math library into the toolchain for whatever your platform is.
The ls output you posted contains a list of libraries available for your host, not your target. On my machine, the libraries for my ARM cross-compiler resides in /opt/local/arm-none-eabi/lib/ for example.
danieltang ~$ ls /opt/local/arm-none-eabi/lib/
crt0.o ldscripts libm.a libssp_nonshared.a libstdc++.a-gdb.py libsupc++.la
elf2flt.ld libc.a libssp.a libssp_nonshared.la libstdc++.la thumb
fpu libg.a libssp.la libstdc++.a libsupc++.a

Qt5 Osx Linker Error

I am receiving the following error after this linker command from qmake generated Makefile.
clang++ -headerpad_max_install_names -mmacosx-version-min=10.6 -o bin/Clock tmp/clock.o tmp/config.o tmp/main.o tmp/qrc_resources.o tmp/moc_clock.o -L/Users/foo/Qt5.0.0/5.0.0/clang_64/lib -lQt5Widgets -lQt5Gui -lQt5Core -framework OpenGL -framework AGL
error:
ld: library not found for -lQt5Widgets
I can confirm that the library is not there with that spelling.
However there is a QtWidgets.la as well there is a QtWidgets.framework dir that has a QtWidgets.
In the lib directory none of the files have a Qt5 prefix is there some way of changing Qt so it does not look for it etc.
I understand that I could rename things and move them around but this is a big job remaining all libs to have a Qt5 prefix.

G++ on windows: cannot fild -llua5.1

Good morning,
I'm trying to build luabind using bjam binaries and g++ (mingw).
Lua root is in 'D:\Dev\lua-5.1.4\',
*.a file here: 'D:\Dev\lua-5.1.4\lib\libluadll.dll.a'
*.dll file here: 'D:\Dev\lua-5.1.4\lib\luadll.dll'
All these *.o files were compiled well, but when it started linking it, something went wrong:
gcc.link.dll bin\gcc-mingw-4.4.1\debug\libluabindd.dll.a
d:/programms/codeblocks/mingw/bin/../lib/gcc/mingw32/4.4.1/../../../../mingw32/b
in/ld.exe: cannot find -llibluadll.dll.a
collect2: ld returned 1 exit status
"g++" -L"D:\Dev\lua-5.1.4\lib" "-Wl,--out-implib,bin\gcc-mingw-4.4.1\debug\
libluabindd.dll.a" -o "bin\gcc-mingw-4.4.1\debug\libluabindd.dll" -shared -Wl,-
-start-group "bin\gcc-mingw-4.4.1\debug\src\class.o" "bin\gcc-mingw-4.4.1\debug\
src\class_info.o" "bin\gcc-mingw-4.4.1\debug\src\class_registry.o" "bin\gcc-ming
w-4.4.1\debug\src\class_rep.o" "bin\gcc-mingw-4.4.1\debug\src\create_class.o" "b
in\gcc-mingw-4.4.1\debug\src\error.o" "bin\gcc-mingw-4.4.1\debug\src\exception_h
andler.o" "bin\gcc-mingw-4.4.1\debug\src\function.o" "bin\gcc-mingw-4.4.1\debug\
src\inheritance.o" "bin\gcc-mingw-4.4.1\debug\src\link_compatibility.o" "bin\gcc
-mingw-4.4.1\debug\src\object_rep.o" "bin\gcc-mingw-4.4.1\debug\src\open.o" "bin
\gcc-mingw-4.4.1\debug\src\pcall.o" "bin\gcc-mingw-4.4.1\debug\src\scope.o" "bin
\gcc-mingw-4.4.1\debug\src\stack_content_by_name.o" "bin\gcc-mingw-4.4.1\debug\s
rc\weak_ref.o" "bin\gcc-mingw-4.4.1\debug\src\wrapper_base.o" -Wl,-Bstatic -Wl
,-Bdynamic -llibluadll.dll.a -Wl,--end-group -g
...failed gcc.link.dll bin\gcc-mingw-4.4.1\debug\libluabindd.dll.a bin\gcc-mingw
-4.4.1\debug\libluabindd.dll...
...failed updating 2 targets...
So, I renamed libluadll.dll.a into lua5.1, lua5.1.a, but it's still prints the same error message.
Thanks, beforehand.
Firstly, -l{name} directive searches for lib{name}.dll and lib{name}.a. So, yours should be -llua, not -llibluadll.dll.a.
Secondly, are Lua libraries compiled with g++ too? Object files and libraries compiled by different compilers are incompatible in general.
This part of the g++ command line is wrong:
-llibluadll.dll.a
If you want to link against libfoo.dll, the right linker flag is -lfoo (no lib, no dll).
If you have a static archive and you want a static link, just name the archive, with no -l prefix (and specify a path if it's not found).

Resources