How to include a dynamic library in C++ - c++

If I search for files that include file libusb.h,
$grep -r "libusb.h" /usr/local/lib/
I get:
Binary file /usr/local/lib//libusb-1.0.0.dylib matches
Binary file /usr/local/lib//libusb-1.0.a matches
Binary file /usr/local/lib//libusb-1.0.dylib matches
But when I compile my class I get:
test.cpp:2:10: fatal error: 'libusb.h' file not found
#include <libusb.h>
Now I know this is because the /usr/local/lib folder isn't properly included. I tried things like the following, etc., but nothing seems to fix it.
gcc -lusb test.cpp
C_INCLUDE_PATH=/usr/local/lib
export C_INCLUDE_PATH
Update
Thanks to some of the help, I have come up with this command...
gcc test.cpp -I/usr/local/include -L/usr/local/lib -lusb-1.0
But now I get...
ld: symbol(s) not found for architecture x86_64
I tried adding
-stdlib=libstdc++
But that doesn't seem to help either.

Including the lib path won't help you here. The lib path contains the path of the binary files you link with.
You need to find the include path which provides the declarations for the exported symbols of the lib that you link against.
A common distribution (not set in stone!), is:
lib/ (binaries to link against)
include/ (declarations are here!)
bin/ (.so on *nix or .dll or Windows)

I may be beating a dead horse here. However I had the same issue and the solutions listed did not work for me. If you are in the same boat, this is what ended up working for me:
gcc -I /usr/include/libusb-1.0/ -lusb-1.0 example.c

This works...
gcc -std=c++0x -stdlib=libc++ -I/usr/local/include -L/usr/local/lib -lusb-1.0 -lstdc++ test.cpp
You can also switch to Clang. This works:
clang++ -std=c++0x -stdlib=libc++ -I/usr/local/include -L/usr/local/lib -lusb-1.0 test.cpp
See comments for more information.

Related

Program compiles, but cannot run because of missing library that exists

I have an OpenGL program. I have all the include directories, and everything.
Directory Structure:
Main.cpp
Lib/
GL/
GLEW/
glm/
I compile the program by running:
g++ main.cpp -lGL -lm -lX11 -lpthread -lXi -lXrandr -ldl -I. -lglfw -Llib/ -o main -lGLEW
The error is on -lGLEW. The program compiles with no errors, but when I run ./main, it gives me this:
Error while loading shared libraries: libGLEW.so.2.2: No such file or directory.
This is confusing, as in my lib/ directory, libGLEW.so.2.2 is present, so is libGLEW.so and libGLEW.a.
Can someone please help me?
When running a dynamic linked executable, the linker must be able to find all the libraries it needs. It always searches
a list of fixed default paths like /lib and /usr/lib
additional paths defined by the environment variable LD_LIBRARY_PATH
any non-standard paths hard-coded in the binary by the -Wl,-rpath g++ option.
These are your options for making this non-standard library known to the runtime linker.
So either use export LD_LIBRARY_PATH=<somepath>/lib when running or use -Wl,-rpath=<somepath>/lib (with comma and without spaces) when building.

g++ compilation with gsl: library not found

Hi I'm trying to install and use GSL library following this guide: Install GSL on Mac. The brew installation works fine and I can see the file in path: /usr/local/include. Then I tried with the sample c code, but when I compile it gives me this error: symbol(s) not found for architecture x86_64. I searched and referred to this answer here: g++ error, so I added -lgsl to the command, sth like:
g++ -I/usr/local/include -lgsl main.c
But it gives: ld: library not found for -lgsl error. How can I resolve this? Thank you!
Yeah the solution should be this:
g++ -I/usr/local/include -L/usr/local/lib -lgsl main.c
This works. Refer to the answer here:Install GSL. Basically what -I gives you is the headers, while we also need the -L statement to link the library.

Linking libraries in GCC

there are some files that I am trying to compile in ubuntu using makefile.
I have added the following lines in my makefile after several searches on web.
run: hellocode.cpp
g++ -c hellocode.cpp -lssl -lcrypto
Still while compiling it creates the object files and then throws this error:
undefined reference to 'SSL_write'....
on the contrary if remove the '-c' and use it like this
run: hellocode.cpp
g++ hellocode.cpp -lssl -lcrypto
Then I dont see the previous errors of linking but it shows different errors not related to openssl linking but related to other files in the code. I have already browsed through many questions on this forum related to this none seem to have helped me in this.
Kindly tell me whether my makefile is wrong or is there some problem with my machine that its not able to link to my library.
Here's a simple Makefile that you could adopt. Note that compilation and linking are 2 steps. If needed you can use -I for additional include paths and -L for additional link paths.
.PHONY : all
all : hellocode
hellocode : hellocode.o
g++ -o hellocode hellocode.o -lssl -lcrypto
hellocode.o : hellocode.cpp
g++ -c hellocode.cpp -o hellocode.o
Here are some basics of makefiles if it helps.
library linking should be done at final stage - linking :)
-c means "compile only" - it just builds .o object file, without any reference resolution (so -lXXX is just ignored there).
-lXXX options should be added to last call to gcc (without -c) which produces executable, where all .o files are gathered to link together with libraries to resolve all references.

mingw g++ is unable to link with libraries

I'm trying to use X86_64-w64-mingw32-g++ (packaged in Archlinux's MingW package) to cross compile some C++ code into an Windows executable, however I'm having trouble getting past some issues.
I'm calling
x86_64-w64-mingw32-g++ -o build_win/asm build_win/asm.o build_win/asm_lib.o build_win/socket_boost.o -I../extra/etc -fopenmp -lrt -std=c++11 -g -lboost_system -lboost_serialization
from a makefile, but I get thrown the errors:
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lrt
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_system
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_serialization
This works fine with native g++, so exactly do I have to change for mingw to compile?
EDIT: I have mingw-w64-boost package installed, which includes boost libraries pre-compiled and ready to be linked. However, it seems the naming convention is a bit different, and -lboost_system for example becomes -llibboost_system-mt (not exactly sure what the -mt suffix entails).
Problem is I can't find the mingw counterpart for -lrt. I've tried with both -lrtm and -lrtutils but in both cases I get:
[...]
undefined reference to `__imp_getsockopt'
Are you sure that -lboost_system and other libraries are present in the same directory as makefile ?
If not then please include -L flag which indicates the location of your library.
For example:
-L /path_openmp -fopenmp -L /path_boost_system/ -lboost_system -L /path_serialization -lboost_serialization
Moreover, you need not include -I and -g flag when creating an executable from .o files. These are needed when you create .o from .cpp files.
There is no rt library on Windows.
You are missing -lws2_32.
$ x86_64-w64-mingw32-nm -A /usr/x86_64-w64-mingw32/lib/*.a 2>/dev/null | grep getsockopt | grep " T "

compiling a c++ program including mysql

I'm new to gcc, and trying to compile a c++ program which includes mysql.h using the command:
g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
It works without issue, but I was wondering if someone could explain the arguments to me. I don't like using commands I don't understand.
Thanks
-o test means the output file is to be named "test".
test.cpp is your source file, of course.
-L/usr/include/mysql means to look for libraries in /usr/include/mysql, as well as in the usual link path. (It probably isn't finding any libraries here; my libmysqlclient.a is in the standard library directory /usr/lib. So I don't think you need this option.)
-lmysqlclient means to link with the mysqlclient library (actually named libmysqlclient.a)
-I/usr/include/mysql means to look for #include files in /usr/include/mysql, as well as in the usual include path.
try "man g++" for a full description of what the various options mean.
man gcc will give you the details of all these options.
g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
g++ : the compiler
-o test : name the resulting binary "test"
test.cpp : your source file
-L : the directory to look in for libraries (that are specified by -l)
-l : named library to link against (looks for it in -L)
-I : the directory to look in for #included header files