GLFW linking, undefined reference to init - linux

So i am trying to compile a simple GLFW window app, and i ran into a linker problem.
gcc -o bin/mtx_gui `pkg-config --libs glfw3 glew` src/main.c
i use this command to compile single main. pkg-config expands into -L/usr/local/lib -lglfw3 -lGLEW -lGLU -lGL which should compile everything. i checked all libs are where they suposed to be. no idea why it is not linking it
main.c:(.text.startup+0x2): undefined reference to `glfwInit'

First, as pointed by G.M in the comment, main.c goes before all libs. second and the most important.
-lglfw3 -lrt -lm -ldl -lpthread -lGL
Libpthread must be also linked, and if you plan on using opengl link Libgl as shown above.

Related

HEXAGON Halide Tools 2.3: /usr/bin/ld: cannot find -lc++abi

I did not get response from Qualcomm forum so I decided to post here. When I was trying to run examples of Halide for Hexagon by running make run as written in the document. Then I got the following issue. The -lc++abi is missing.
clang++ -std=c++11 -I /opt/qcom/Hexagon_SDK/4.3.0.0/tools/HALIDE_Tools/2.3.03/Halide/include -stdlib=libc++ -O3 -g -fno-rtti -rdynamic conv3x3_generator.cpp /opt/qcom/Hexagon_SDK/4.3.0.0/tools/HALIDE_Tools/2.3.03/Halide/lib/libHalide.a /opt/qcom/Hexagon_SDK/4.3.0.0/tools/HALIDE_Tools/2.3.03/Halide/tools/GenGen.cpp -o /opt/qcom/Hexagon_SDK/4.3.0.0/tools/HALIDE_Tools/2.3.03/Halide/Examples/build/offload/hexagon_benchmarks/bin/conv3x3.generator -lz -lrt -ldl -lpthread -lm
/usr/bin/ld: cannot find -lc++abi
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I checked the /usr/lib and find. So it should be there?
./x86_64-linux-gnu/libc++abi.so.1.0
./x86_64-linux-gnu/libc++abi.so.1
./llvm-10/lib/libc++abi.so.1.0
./llvm-10/lib/libc++abi.so.1
Did I miss anything or make anything stupid? Thanks!
ld is your system's host linker -- that's GNU BFD ld. clang++ must be in your PATH but it's the host clang++ and not the hexagon-clang++ that would be with Halide/Hexagon tools.
I contacted the person from Qualcomm. I am not supposed to use either /usr/bin/clang++ nor hexagon-clang++. I downloaded clang+llvm-7.0.1-x86_64-linux-gnu-ubuntu-18.04.tar.xz from https://releases.llvm.org/7.0.1/. You should use clang++ under the bin folder after you extract it. Now it works for me.

Undefined symbol: fstat in dynamic library

I'm writing a dynamic library that's loaded at runtime as a plugin. At runtime, the library fails to load with the following message:
dlerror:/path/to/so: undefined symbol: fstat
The dynamic library consists of a bunch of static libraries linked together, including libsodium. I've tracked down libsodium as the only place that fstat() is used in the library.
As I understand it, fstat() should be part of libc, and should not require additional libraries to be installed. What am I missing?
EDIT1:
I've found some additional information about how linking to fstat() is different than most functions, but I'm not sure exactly how it applies to my situation. I am compiling/linking libsodium with the provided makefiles for their distribution, and the dynamic library is being compiled with the compiler driver as recommended in that answer.
EDIT2: here's the linker command, generated by CMake:
/usr/bin/c++ -fPIC -O3 -DNDEBUG -rdynamic -nodefaultlibs -undefined_warning -fPIC -fvisibility=hidden -shared -Wl,-soname,lin.xpl -o src/xplane_plugin/lin.xpl src/path/to/object/file1.cpp.o src/path/to/object/file2.cpp.o src/plugin/libplugin.a /usr/local/lib/libpng16.a -lz src/plenty/of/dependencies.a lib/glew/libglew.a src/another/library.a /usr/local/lib/libsodium.a /usr/lib/x86_64-linux-gnu/libcurl.so lib/json11/libjson11.a -lpthread deps/some/library.a /usr/local/lib/libzip.a -lbz2 -llzma -lssl -lcrypto -lstdc++fs -lz

Build a program with GLFW3 on Linux

I know this question has been asked many times before but I have been trying to get this working for days and none of the current answers solve my problem. I'd appreciate not being linked to this post: How to build & install GLFW 3 and use it in a Linux project Most of the answers I have seen redirect there but I have gone through it thoroughly and am still having problems.
I am running Linux Mint 17.1.
I have downloaded and built GLFW 3.1.1:
*Downloaded source; extracted; terminal to extract directory*
cmake .
make install
I have also downloaded the example program at http://www.glfw.org/docs/3.0/quick.html#quick_example
I am using the following build commands:
g++ -std=c++11 -c HelloGLFW.cpp
g++ HelloGLFW.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi
The program compiles fine but when I try to link it I get these errors:
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `initExtensions':
x11_init.c:(.text+0x1aeb): undefined reference to `XineramaQueryExtension'
x11_init.c:(.text+0x1b05): undefined reference to `XineramaIsActive'
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `_glfwCreateCursor':
x11_init.c:(.text+0x21f9): undefined reference to `XcursorImageCreate'
x11_init.c:(.text+0x22d0): undefined reference to `XcursorImageLoadCursor'
x11_init.c:(.text+0x22e0): undefined reference to `XcursorImageDestroy'
//usr/local/lib/libglfw3.a(x11_monitor.c.o): In function `_glfwPlatformGetMonitors':
x11_monitor.c:(.text+0x6e9): undefined reference to `XineramaQueryScreens'
collect2: error: ld returned 1 exit status
I have also gone through the instructions on the GLFW website but they seem to be quite lacking for Linux. I have gotten it working on Linux before but that was a year ago and I can't replicate it.
Can someone please let me know all the steps required to build a program with GLFW3 on Linux (including all dependencies required)?
When glfw3 was installed using cmake a file glfw3.pc was created along with it. This is a pkg-config file and lists what libraries are required for linking.
For me this file was located in /usr/local/lib/pkgconfig
The command I used to successfully build the program with was:
g++ HelloGLFW.cpp -lglfw3 -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -lpthread
The GLFW site does list details at http://www.glfw.org/docs/latest/build.html#build_link_pkgconfig but it's a bit unclear what to look for if you aren't that familiar with Linux systems.
Specificity the instructions list this command for compiling with the static glfw3 library:
cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --static --libs glfw3`
and with the dynamic glfw3 library:
cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`

undefined reference when cross-compiling for ARM with static OpenCV libraries

I'm trying to compile a simple test program using static OpenCV libraries that have been compiled using an ARM compiler. But when I try to compile it with the command
$arm-linux-gnueabihf-g++ `pkg-config --static opencv` -I/usr/local/include -L<path to static libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2
This gives
/tmp/ccxNeUbK.o: In function main':
ARMtest2.cpp:(.text+0x8a): undefined reference tocv::Mat::ones(int, int, int)'
/tmp/ccxNeUbK.o: In function cv::Mat::~Mat()':
ARMtest2.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x20): undefined reference tocv::fastFree(void*)'
/tmp/ccxNeUbK.o: In function cv::Mat::release()':
ARMtest2.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x30): undefined reference tocv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
The code itself is just some simple test code that prints a Mat type variable.
I compiled the static OpenCV library with cmake-gui. I selected UNIX Makefile and then selected 'specify options for cross-compiling' where I gave the path to the ARM (arm-linux-gnueabihf) gcc and g++ compiler. Then I unticked BUILD_SHARED_LIB so it compiled static libraries. It seemed to compile fine without errors. After that I did make & sudo make install.
I also tried it with shared libraries and that worked fine on the ARM board (once I copied the libraries to the board and exported the library path).
The static .a files landed nicely in the build folder. Apparently it can also find it when I -L link to it. I have tried reversing the order of the libraries, but to no avail.
So I'm a bit at a loss what is going wrong.
I solved it. Using the normal --static pkg-config command to compile with OpenCV libraries;
`pkg-config --libs --static opencv`
Of course I installed the static libraries also to the folder /usr/local/lib where libraries get installed first. But still i don't know what I missed in the command line I tried to use. I had a look in the config file /usr/local/lib/pkgconfig/opencv.pc
Here is whats in it:
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.4.9
Libs: ${exec_prefix}/lib/libopencv_calib3d.so ${exec_prefix}/lib/libopencv_contrib.so ${exec_prefix}/lib/libopencv_core.so ${exec_prefix}/lib/libopencv_features2d.so ${exec_prefix}/lib/libopencv_flann.so ${exec_prefix}/lib/libopencv_gpu.so ${exec_prefix}/lib/libopencv_highgui.so ${exec_prefix}/lib/libopencv_imgproc.so ${exec_prefix}/lib/libopencv_legacy.so ${exec_prefix}/lib/libopencv_ml.so ${exec_prefix}/lib/libopencv_nonfree.so ${exec_prefix}/lib/libopencv_objdetect.so ${exec_prefix}/lib/libopencv_ocl.so ${exec_prefix}/lib/libopencv_photo.so ${exec_prefix}/lib/libopencv_stitching.so ${exec_prefix}/lib/libopencv_superres.so ${exec_prefix}/lib/libopencv_ts.a ${exec_prefix}/lib/libopencv_video.so ${exec_prefix}/lib/libopencv_videostab.so -lrt -lpthread -lm -ldl
Cflags: -I${includedir_old} -I${includedir_new}
I believe this is what is being called with the pkg-config <--something_or_other> opencv line.
And saw some other things that probably get linked when compiling -lrt -lpthread -lm -ldl Not really sure though as I tried it ina normal command line and I apparently still missed somethings.
But it worked, so didn't really bother too much with it much further :)
In your command:
*$arm-linux-gnueabihf-g++ `pkg-config --static opencv` -I/usr/local/include -L<path to static libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2*
The cflags was missing.
Try with:
$arm-linux-gnueabihf-g++ `pkg-config --cflags --static opencv` -I/usr/local/include -L<path to static cross-compiled libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2
Assuming here that your cross-compiled headers are saved in /usr/local/include (as you specified).

std.net.curl linker errors in Linux

I'm trying to use std.net.curl on 32-bit Ubuntu Linux (11.10 Ocelot) using DMD 2.058.
When I build (dmd source.d) I get:
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl19_sharedStaticCtor30FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticCtor30FZv+0x6): undefined reference to `curl_global_init'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl19_sharedStaticDtor31FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticDtor31FZv+0x4): undefined reference to `curl_global_cleanup'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl_12fd_140.o): In function `_D3std3net4curl4HTTP4Impl6__dtorMFZv':
std/net/curl.d:(.text._D3std3net4curl4HTTP4Impl6__dtorMFZv+0x12): undefined reference to `curl_slist_free_all'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl_12fd_140.o): In function `_D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP':
std/net/curl.d:(.text._D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP+0x53): undefined reference to `curl_slist_append'
[snip]
I added -L-lcurl to my command line (full command: dmd source.d -L-lcurl) but I get the exact same result. I have libcurl4-openssl installed. I can build a simple C curl program on this computer without issue (gcc simple.c -lcurl). I'm not sure where to look from here to figure this out.
Update: Here is how the linker is invoked according to dmd -v:
gcc source.o -o source -m32 -lcurl -Xlinker -L/usr/lib -Xlinker -L/usr/lib64 -Xlinker --no-warn-search-mismatch -Xlinker --export-dynamic -lphobos2 -lpthread -lm -lrt
As you yourself discovered, the order of libraries is incorrect. A quote from "An Introduction to GCC": "A library which calls an external function defined in another library should appear before the library containing the function." In your case libphobos calls external library's (libcurl) function(s), so I guess libphobos should go before libcurl.
The book is available online. Chapter related to the OT is here: http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html
The DMD compiler is clearly broken in this respect. I published a patch to fix it (maybe not the best) but the maintainer of the compiler is not convinced this needs to be fixed at all so there is no much discussion about it.
https://github.com/D-Programming-Language/dmd/pull/497
http://d.puremagic.com/issues/show_bug.cgi?id=7044
Seemingly the simplest way to fix the phobos2/curl linker order problem, as mentioned elsewhere in this thread:
dmd source.d -L-lphobos2 -L-lcurl
Works like a charm for me.
I figured out if I obtain the linker command through -v and reorder it so the -lcurl is at the end it links without issue.
gcc source.o -o source -m32 -Xlinker -L/usr/lib -Xlinker -L/usr/lib64 -Xlinker --no-warn-search-mismatch -Xlinker --export-dynamic -lphobos2 -lpthread -lm -lrt -lcurl
I'm still curious if there is a way to fix this without manually running the linker.

Resources