GNU Scientific Library undefined reference to xx issue - gsl

I’m struggling with GSL, I’m on Ubuntu 14.04 and have just installed GSL.
I downloaded the latest GSL release, and ran ./configure in the extracted folder, then sudo make, and after that sudo make install.
Everything seems to have worked fine during the installation.
I’ve tried some basic examples from the documentation. The first example from this page works great.
But this example does not work. I get the following error:
simon#simon-bnt:~/Skrivbord/gsl-test$ gcc main.c -o main -lgsl -lgslcblas -lm
/tmp/cclZjB9J.o: I funktionen "main":
main.c:(.text+0xe): undefined reference to gsl_interp2d_bilinear
main.c:(.text+0x88): undefined reference to gsl_spline2d_alloc
main.c:(.text+0xbc): undefined reference to gsl_spline2d_set
main.c:(.text+0xde): undefined reference to gsl_spline2d_set
main.c:(.text+0x10e): undefined reference to gsl_spline2d_set
main.c:(.text+0x130): undefined reference to gsl_spline2d_set
main.c:(.text+0x156): undefined reference to gsl_spline2d_init
main.c:(.text+0x26b): undefined reference to gsl_spline2d_eval
main.c:(.text+0x308): undefined reference to gsl_spline2d_free
collect2: error: ld returned 1 exit status
So I googled for a while, found another way to reference the libraries needed:
simon#simon-bnt:~/Skrivbord/gsl-test$ gcc -o main main.c gsl-config --cflags --libs
simon#simon-bnt:~/Skrivbord/gsl-test$ ./main
./main: error while loading shared libraries: libgsl.so.19:
cannot open shared object file: No such file or directory`
But as you can see that did not work either.
I’d like to be able to compile the whole program into an executable file, so I’d rather link the libraries.
I’d be really happy for all the help I can get.

The gsl-config command prints arguments that are to be passed to the compiler and/or linker. For example, on my system:
$ gsl-config --cflags
-I/usr/include
$ gsl-config --libs
-L/usr/lib -lgsl -lgslcblas -lm
$
When you type:
gcc -o main main.c gsl-config --cflags --libs
you're not passing the output of gsl-config to gcc, you're passing the *string" gsl-config to gcc, and gcc doesn't know what to do with it.
I think the correct command line would be this, or something very similar to it:
gcc $(gsl-config --cflags) main.c -o main $(gsl-config --libs)
This assumes you're using some sh-derived shell that recognizes the $(...) syntax. If your interactive shell is csh or tcsh, you can use this:
gcc `gsl-config --cflags` main.c -o main `gsl-config --libs`
(You can use the backtick syntax with bash as well, but I find the $(...) syntax easier to use if it's available.)
(BTW, this didn't work for me, but it's likely I just haven't installed some required package.)

I suspect you have the libgsl* packages of the distribution shadowing your new installation. GSL version before 2.0 had no 2d-interpolation (IIRC) so it would explain how can compile (using your new headers) but not link.
Removing libgsl0-dev may be enough. You can then still run against Ubuntu packages depending on the run-time but allow you to build against your local gsl 2.0.
To prove that point, here is a copy&paste from a Docker session in a Debian testing container using the GSL 2.0 package. It then all works:
root#ab45b54cd90e:/tmp# gcc -o gsl_interp2d gsl_interp2d.c -lgsl -lgslcblas -lm
root#ab45b54cd90e:/tmp# ./gsl_interp2d | head
0.000000 0.000000 0.000000
0.000000 0.010101 0.010101
0.000000 0.020202 0.020202
0.000000 0.030303 0.030303
0.000000 0.040404 0.040404
0.000000 0.050505 0.050505
0.000000 0.060606 0.060606
0.000000 0.070707 0.070707
0.000000 0.080808 0.080808
0.000000 0.090909 0.090909
root#ab45b54cd90e:/tmp#
I did
apt-get update
apt-get install libgsl-dev
apt-get install build-essential # for gcc and friends
to get what is needed beyond the basic Docker container.

Related

Point Cloud Library, linking libraries with g++

I want to run sample code from PCL site to simple read cloud from file.
I installed libraries, and prepared build script:
g++ -std=c++11 main.cpp -lboost_system `pkg-config --libs --cflags opencv4` -g -o main
but compiller throws many errors like:
/usr/include/pcl/io/pcd_io.h:56: undefined reference to `vtable for pcl::PCDReader'
I know that I need to linking libpcl_io.so like I linked boost library by adding -lboost_system , but my question is - how?
I tried add to script something like -lpcl, -libpcl, -lpcl_io, but it doesn't works
So, what is keyword to linking PCL Libraries?

Undefined reference to 'dlsym' and 'dlopen'

I am compiling using arm-linux-gnueabi-g++ version 4.7.3.
I have the arm-linux-gnueabi libraries installed at location:
/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2,
and libdl-2.19.so.
libdl.so links to libdl.so.2 which links to libdl-2.19.so.
I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.
What am I missing with the ARM compiler command?
Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.
I don't understand why the flag is needed, but the code does finish linking now.
A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.

Compiling/linking g++ using -m32 flag on 64bit System -> manually add -ldl?

It was a long long way, but I managed to include a shared file (libpi_pi_gcs2.so) included into C++ code under Linux Mint 17 Cinnamon 64-bit. Now as the dust is saddled I have a question left... let me summarize:
I copied libpi_pi_gcs2.so in usr/local/lib.
Trying to compile main.cpp with
g++ -Wall -o test main.cpp -lpi_pi_gcs2
faild, returning
/usr/bin/ld: skipping incompatible //usr/local/lib/libpi_pi_gcs2.so when searching for -lpi_pi_gcs2
/usr/bin/ld: cannot find -lpi_pi_gcs2
collect2: error: ld returned 1 exit status
I talked with my flatmate, and he guessed maybe the shared library is only 32bit compatible.
Using -m32 let me take a step forward! But yielding new errors.
I googled and it seemed like I needed C32 std libraries. And I installed:
apt-get install ia32-libs
Again the old error messages disappeared and new one arose, after googling around I installed:
apt-get install g++-multilib
Trying to compile it now using
g++ -Wall -o test -m32 main.cpp -lpi_pi_gcs2
Let to the error messages:
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlsym'
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlopen'
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlclose'
The solution was the following command:
g++ -Wall -o test -m32 main.cpp -lpi_pi_gcs2 -ldl
Now my question:
Is it necessary to explicitly state -ldl i.e. link with libdl.so because the -m32 flag declares everything what follows to be 32bit and hence the linker would without explicitly
writing -ldl search for a 32bit libdl.so? I guess mine is 64 bit I am working on a 64bit system.
What do you think? More details and background informations are appreciated.
Greetings,
newandlost

linking against clang/llvm libraries on linux always fails

I am using Linux Mint 15 Olivia, and I installed the clang compiler and libclang-dev packages available in the repositories, they are version 3.2.
I am trying to compile and link the example from clang repository http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_32/final/examples/clang-interpreter/main.cpp as you can see it's from clang version 3.2.
When I compiled it with the command:
$> clang++ `llvm-config --cflags` -c main.cpp
I got the file main.o which contains some undefined symbols to clang/llvm libraries. I made sure it contains the symbols by using the nm command:
$> nm main.o
A symbol from clang would be
_ZN5clang16CompilerInstanceC1Ev
Until now everything worked fine, until I tried to link the 'main.o' file with clang/llvm libraries. When I issue the command:
$> clang++ `llvm-config --ldflags` main.o `llvm-config --libs`
It fails with the following output (I just put the first error to not clutter this post):
main.o: In function `main':
main.cpp:74: undefined reference to `clang::TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream&, clang::DiagnosticOptions*, bool)'
Basically I get a whole bunch of undefined references to clang libraries. I've tried researching on this and all I've found is that this has to do with the order you put the libraries flags on the command line. In addition to this, I also tried some other things:
I tried a bunch of compiler flags, changing the order in which the linker flags appear, they never worked.
I downloaded and compiled the LLVM and clang source code version 3.2, and 3.4, same result: compiling works, linking fails. It's worth mentioning to say that for each version reordering the linker flags always gave different link errors (this is of course due to the way the link searches for libraries).
I ran out of ideas, and already spent 2 hours trying to compile a simple example from clang's repository, any help would be appreciated.
Thank you
The answer is easy - llvm-config will not give you clang libraries. You need to link them separately. Check clang/tools/driver/Makefile as an example of a library list.
The answer to this problem is as Anton Korobeynik suggested, I was missing the clang libraries (which are not part of the llvm build as I was expecting from the command 'llvm-config --libs').
In the end the final command turned out to be:
clang++ `llvm-config --ldflags` main.o -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangARCMigrate -lclangRewriteFrontend -lclangRewriteCore -lclangEdit -lclangAST -lclangLex -lclangBasic `llvm-config --libs`
If compiling any clang tool or example, make sure you check the Makefiles under clang/tools folder :)

OpenCV on ubuntu 11.10

I've just updated my system from ubuntu 11.04 to 11.10 and now I can't compile anymore any C program that contain references to OpenCV libraries
I've already tried to reinstall OpenCV (I use the 2.1 version) but I'm stuck with this error:
/tmp/ccArHTZL.o: In function `main':
z.c:(.text+0x59): undefined reference to `cvLoadImage'
z.c:(.text+0xa0): undefined reference to `cvNamedWindow'
z.c:(.text+0xb1): undefined reference to `cvShowImage'
z.c:(.text+0xbb): undefined reference to `cvWaitKey'
z.c:(.text+0xc5): undefined reference to `cvDestroyWindow'
z.c:(.text+0xd1): undefined reference to `cvReleaseImage'
collect2: ld returned 1 exit status
In order to install OpenCV I've always followed this procedure:
$ sudo apt-get install libcv2.1 libcv-dev libcvaux2.1 libcvaux-dev libhighgui2.1
libhighgui-dev opencv-doc python-opencv
$ export LD_LIBRARY_PATH=/home/opencv/lib
$ export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig
$ pkg-config --cflags opencv
-I/usr/include/opencv
$ pkg-config --libs opencv
-lcxcore -lcv -lhighgui -lcvaux -lml
$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.c
Anyone can help me?
Why don't you use pkg-config to your favor?
g++ hello.c -o hello `pkg-config --cflags --libs opencv`
I think it is because of some changes from gcc 4.5 to gcc 4.6
Try this command instead (i.e., move the libraries to the end, instead of at the beginning of your command line) -- it works for me:
g++ -I/usr/include/opencv hello.c -lcxcore -lhighgui -lm
I'm still on kubuntu 10.10 so I'm not really familiar how does 11.10 work, but the most common answer to problems with not finding libraries is to use ldconfig with sudo. It'll refresh libraries database. If that doesn't help, look into /usr/lib, /usr/lib64 and /usr/lib32, because its the default place where apt-get throws libraries in. When you find the libraries, change the LD_LIBRARY_PATH so it contains the directory. I don't think that /home/opencv/lib is where they are, but i don't know Your environment
I just upgraded to 11.04 on my laptop and having similar issues. I would try building the latest version of OpenCV (2.3.1) and see if this fixes anything, this seemed to fix quite a few issues for me.
Use the following command, it worked for me:
gcc pkg-config --cflags opencv opencv.c -o open_cv pkg-config --libs opencv