c++ Makefile doesn't find library - c++

i want to run a "make" command in linux terminal in order to use some c++ libs.
Unluckily while running the "make" an error appears saying it cannot find the requested libs.
Here the relevant makefiles:
Here are the files in my filesystem which should be linked.
Why does makefile not find the libraries ?
Thanks for all answers in advance

Try to modify your Makefile for -lgmp rather than -libgmp
In background the compiler adds the 'lib' onto the name as well as the extension .so and similar stuff.
Try running the following to get an overview of what the linker is trying to do:
ld -L [yourincludepath] -lgmp --verbose
Such resolution issues might be due to the fact you are trying to link a 64bit library with a 32bit application? Try installing a proper XX-bits of the lib maybe?
Try to add -m option (machine dependency):
ld -melf_i386 -L /usr/lib/i386-linux-gnu/ -lgmp --verbose
Other values which might be supported on your side:
elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu i386linux elf_l1om elf_k1om i386pep i386pe
More info:
usr/bin/ld: cannot find -l<nameOfTheLibrary>

Related

Building GDAL with all libraries static

I want to develop a small program that checks which polygons from a shapefile intersect a given rectangle. This program is to be used in a website (with PHP's exec() command). The problem is, my webserver cannot install GDAL, for reasons unknown to me. So I can't link to the shared libraries. Instead, I must link to static libraries, but these aren't given.
I've downloaded the GDAL source code from here (2.3.2 Latest Stable Release - September 2018), and followed the build instructions from here. Since I already have GDAL working on my Debian, and don't want to mess with it, I followed the "Install in non-root directory" instructions, with some adjusts from the last item in the "Some caveats" section:
cd /home/rodrigo/Downloads/gdal232/gdal-2.3.2
mkdir build
./configure --prefix=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/ --without-ld-shared --disable-shared --enable-static
make
make install
export PATH=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/bin:$PATH
export LD_LIBRARY_PATH=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib:$LD_LIBRARY_PATH
export GDAL_DATA=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/share/gdal
/usr/bin/gdalinfo --version
build/bin/gdalinfo --version
The first /usr/bin/gdalinfo --version gives 2.1.2 (the previous installed version). The second, build/bin/gdalinfo --version, gives 2.3.2 (the version just built).
By now, my program only uses the ogrsf_frmts.h header, which is in /usr/include/gdal/ or /home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/include/ directory, depending on the build. There's no ogrsf_frmts.a file, but only a libgdal.a. Is this the file I should be linking against? If so, how? I've tried so far:
gcc geofragc.cpp -l:libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l:libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l:/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp /home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -l/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -l:/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
but nothing works. What am I missing?
EDIT
The second trial (gcc geofragc.cpp -Wl,-Bstatic -l:libgdal.a) is giving the following error:
/usr/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-linux-gnu/6/../../../../lib/libgdal.a(gdalclientserver.o): In function `GDALServerSpawnAsync()':
(.text+0x1f5e): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
You can use the gdal-config program to get correct options for compilation and linking. This program is a part of the GDAL library and it has its own options:
hekto#ubuntu:~$ gdal-config --help
Usage: gdal-config [OPTIONS]
Options:
[--prefix[=DIR]]
[--libs]
[--dep-libs]
[--cflags]
[--datadir]
[--version]
[--ogr-enabled]
[--gnm-enabled]
[--formats]
You have to make sure this program is on your search path, or you can create an alias - for example:
alias gdal-config='/home/rodrigo/Downloads/gdal232/gdal-2.3.2/bin/gdal-config'
Now your compilation and linking command becomes the following one:
g++ `gdal-config --cflags` geofragc.cpp `gdal-config --libs` `gdal-config --dep-libs`
You have to use the g++ compiler to link with C++-built libraries.
Another option is to create a Makefile with these lines:
CXXFLAGS += ${shell gdal-config --cflags}
LDLIBS += ${shell gdal-config --libs}
LDLIBS += ${shell gdal-config --dep-libs}
geofragc: geofragc.cpp
and just call make with this Makefile.
I hope, it'll help.

Using c++ library on linux [duplicate]

This question already has answers here:
How to install c++ library on linux
(2 answers)
Closed 4 years ago.
I'm new to c++ and don't understand how to install a library on Linux (Mint). I want to use the GNU GMP library:https://en.wikipedia.org/wiki/GNU_Multiple_Precision_Arithmetic_Library
I downloaded the tar.lz file and installed it with
./configure
make
sudo make install
If I try to compile it, I get the error message that the header file "gmpxx.h" wasn't found. Where can I find this file? How do I compile it with the -lgmpxx -lgmp flags? I tried something like:
g++ test.cpp -o test -lgmpxx -lgmp
If the library is using the Autoconf system (which your does) then the default installation prefix is /usr/local.
That means libraries are installed in /usr/local/lib, and header files in /usr/local/include. Unfortunately few Linux systems have those added for the compiler to search by default, you need to explicitly tell the compiler to do it.
Telling the compiler to add a header-file path is done using the -I (upper-case i) option. For libraries the option is -L.
Like so:
g++ test.cpp -I/usr/local/include -L/usr/local/lib -lgmpxx -lgmp
The above command will allow your program to build, but it's unfortunately not enough as you most likely won't be able to run the program you just built. That's because the run-time linker and program loader doesn't know the path to the (dynamic) libraries either. You need to add another linker-specific flag -rpath telling the build-time linker to embed the path inside your finished program. The front-end program g++ doesn't know this option, so you need to use -Wl,-rpath:
g++ test.cpp -I/usr/local/include -L/usr/local/lib -lgmpxx -lgmp -Wl,-rpath=/usr/local/lib
The options can be found in the GCC documentation (for the -I and -L and -Wl options), and the documentation for ld (the compile-time linker) for the -rpath option.
If you install a lot of custom-build libraries, you might add the path /usr/local/lib to the file /etc/ld.so.conf and then run the ldconfig command (as root). Then you don't need the -rpath option.
Now with all of that said, almost all libraries you would usually use for development will be available in your distributions standard repository. If you use them the libraries will be installed with paths that means you don't have to add flags.
So I recommend you install your distributions development packages for the libraries instead.

Clang linker does not look into LD_LIBRARY_PATH

I am trying to build and link a C++, cmake-based project with clang (3.0). This project links to several libraries that are installed in a custom directory /my/dir/. This directory is included in the LD_LIBRARY_PATH and LIBRARY_PATH environment variables. Project builds and links fine with g++.
The link command generated and executed by cmake looks like the following:
/usr/bin/clang++ -O3 stuff.cpp.o -o stuff -rdynamic -lmylib
ld then complains with the following message:
/usr/bin/ld: cannot find -lmylib
The link command above runs fine whenever I manually add -L/my/dir/. Is there a way to link without specifying the -L flag?
The $LD_LIBRARY_PATH environment variable (and its various alternatives on other UNIX-based platforms) is used at runtime, not link time, to find libraries.
Using -L is the correct approach and cannot be avoided.
Note: A better approach under Linux (you don't specify your platform so I'm guessing) is to correctly configure a file in /etc/ld.so.conf.d/ and avoid using $LD_LIBRARY_PATH altogether.

libboost-system linker errors when cross-compiling to x86

I'm trying to build a 32-bit application on Ubuntu 11.04 x64. I'm having some issues with the build because of linker errors with libboost. The build statement has -lboost_system in it, but when I try to build I get a bunch of these:
CommunicationModule.cpp:(.text+0x68c1): undefined reference to boost::system::generic_category()
CommunicationModule.cpp:(.text+0x68d7): undefined reference to boost::system::system_category()
Everything I've found on google says I need to link to the boost_system library. One place I found says to try linking to it directly, but when i do locate boost_system the result is empty. When I try doing a sudo apt-get install libboost-system-dev it tells me that it's already installed. I'm kind of at a loss here. The library is installed, but it's not being found by locate?
Can anyone tell me what I need to do to properly link to boost::system? I'm fairly new to linux and the complexities of compilers so any help here would be appreciated.
Update:
Here is the output of dpkg -L libboost-system1.42-dev:
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libboost-system1.42-dev
/usr/share/doc/libboost-system1.42-dev/copyright
/usr/share/doc/libboost-system1.42-dev/NEWS.Debian.gz
/usr/share/doc/libboost-system1.42-dev/README.Debian.gz
/usr/lib
/usr/lib/libboost_system.a
/usr/lib/libboost_system-mt.so
/usr/lib/libboost_system-mt.a
/usr/lib/libboost_system.so
Is there a flag I can use to link to one of these directly? I tried using -L /usr/lib/libboost_system.so and -L /usr/lib/libboost_system-mt.so and neither of those fixed the issue. Same with just adding /usr/lib/libboost_system.a and /usr/lib/libboost_system-mt.a to the build statement.
Here is the compilation line:
g++ -m32 -Wl,-O1 -o UTNaoTool [.o files] -L/usr/lib32 -lqglviewer-qt4 -lqwt-qt4 -lboost_system -lboost_thread -lQtXml -lQtOpenGL -lQtGui -lQtNetwork -lQtCore -lGLU -lpthread
Update 2:
I downloaded boost 1.49 and built everything for 32-bit and that seemed to help. A lot of the errors went away, but now I still have these:
CommunicationModule.cpp:(.text+0x68c1): undefined reference to
boost::system::get_generic_category()
Note that the function is different. So all of my errors are regarding undefined references to get_system_category() and get_generic_category() now. I tried adding a -lboost_filesystem to the build command but that didn't fix this, and I made sure it was referencing the 32-bit library that I built when I built libboost_system.
Looking at my own installation, it seems libboost-system-dev does not install the libraries. Using dpkg to tell me what was installed bz libboost-system-dev I get:
$ dpkg -L libboost-system-dev
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libboost-system-dev
/usr/share/doc/libboost-system-dev/copyright
/usr/share/doc/libboost-system-dev/changelog.gz
Poking around, I think you need to install libboost-system1.48.1 (or some other version).
sudo apt-get install libboost-system1.XX.Y
You can also search fo rthe libraries using the find command, for example, search under /usr for all files starting with libboost_system:
find /usr -name "libboost_system*"
Edit: Since you are cross-compiling from a 64 bit OS to a 32 bit one, you need 32 bit versions of the boost libraries. I would be tempted to set up a small 32 bit virtual machine to do this, rather than cross-compiling all the dependencies.
I had the same problem with boost_serialization here is what i found out after couple of googling..
first this library need to be compiled separately :
so after downloading the boost library ,extract it and execute sudo ./bootstrap.sh' then
sudo ./b2 --with-system
after this step you should be find a result when executing locate boost_system
then to link it manually I did:
both should work
g++ boostexample.cpp -o run /PATH/libboost_serialization.a
g++ boostexample.cpp -o run -L/PATH/ -lboost_serialization
well this is a little work around and I'm still looking for how to link the library properly
I hope this helped :)

GNU ld cannot find library which is there

The packages I'm toying with here are rather unknown, but nevertheless the problem is rather generic. Basically, I'm trying to compile Python module (called rql) with C++ extension. The extension uses external framework called gecode, which contains several libraries. I compiled gecode and installed locally. Now, let the output speak for itself:
red#devel:~/build/rql-0.23.3$ echo $LD_LIBRARY_PATH
/home/red/usr/lib
red#devel:~/build/rql-0.23.3$ ls $LD_LIBRARY_PATH | grep libgecodeint
libgecodeint.so
libgecodeint.so.22
libgecodeint.so.22.0
red#devel:~/build/rql-0.23.3$ python setup.py build
running build
running build.py
package init file './test/__init__.py' not found (or not a regular file)
running build_ext
building 'rql_solve' extension
g++ -pthread -shared build/temp.linux-i686-2.5/gecode-solver.o -lgecodeint -lgecodekernel -lgecodesearch -o build/lib.linux-i686-2.5/rql_solve.so
/usr/bin/ld: cannot find -lgecodeint
collect2: ld returned 1 exit status
error: command 'g++' failed with exit status 1
LD_LIBRARY_PATH is for runtime linker/loader (same effect could be achieved with ldconfig ). What you need is the -L flag:
-L/home/red/usr/lib
on the compiler command line.
Edit:
And - thanks to #bjg for reminding me - you can use LIBRARY_PATH if you don't want to mess with compiler options.
You've apparently modified LD_LIBRARY_PATH to point to a non-standard location in your home directory. Do you know if LD_LIBRARY_PATH in the environment used to call g++ in setup.py matches your shell's environment?
See if you can pass arguments to setup.py to modify the library path or simply pass -L/home/red/usr/lib to g++.