Unable to link created .so file to main.cpp - c++

Good day! I am running a c++ project main.cpp in eclipse. i want to use my existing .so file and link it in main.cpp. I am searching about this but cant find the right method for me. I'm using ubuntu. Thanks for any help!

To link against a library you need to supply the compiler(or the linker) a -l flag and the library name. For example, if you want to link against a library called libjustine.so, you would supply your compiler(gcc if C, g++ if C++) a flag -ljustine and then the compiler would instruct the linker to link against a file libjustine.so in the library path (usually at least /usr/lib/ and /usr/local/lib/).
However, if you want to define your own library directory, for example a lib/ directory inside your project directory, you need to use the -L flag and supply the desired library directory(relative to the current directory) that way, for example -L/lib/ so the compiler can instruct the linker to look for the desired library from lib/ instead of for example /usr/lib/ where it looks for the file by default.

Related

How to add a c++ library on ubuntu

I have downloaded a repository from github with a library with git clone
then I typed in terminal "make"
now I have a libmylibrary++.so file in folder mylibrary/src
Now I want to run a c++ file which uses the library but I can't compile it because of this error: "mylibrary.hpp: No such file or directory"
How to add it to PATH? yet I don't understand what exactly I need to add
Whether it is folder "/home/mylibrary" or folder with the .so file
Your C++ compiler only knows to search for header files and libraries in standard locations and directories. Just because you downloaded another library in some directory, somewhere, doesn't mean that your C++ compiler will automatically find it.
You will need to change your Makefile and compile your program with the additional compilation options that instruct your C++ compiler to search for header files and libraries in other directories -- typically -I and -L option.
Additionally, you will probably need to use either -R or -Wl,-rpath options in order for the compiled code to load the shared libraries from the non-standard locations.
You will find additional information in the gcc manual and documentation.

C++ Eclipse use libraries

I am currently encountering a few errors with Eclipse c++. I am a complete newbie to c++ and because I knew Eclipse from java I thought it might be ok. Now to the issue:
I want to use the curl library, but I have no idea how to include it correct. Everywhere around the internet I found that I have to go to the compiler settings, include the library there, go to the linker settings and include it there as well. Now the linker has 2 different panels, one for Libraries (-l) and one for Library paths (-L).
I have downloaded the curl library to this directory:
C:\Users\Hannes\Desktop\eclipseC\curl-7.45.0
How do I have to include the Library correctly now?
Here is the error:
g++ "-LC:\Users\Hannes\Desktop\eclipseC\curl-7.45.0\include\curl" -o HelloWorld.exe HelloWorld.o -lcurl
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lcurl
In the editor the automatic completion for curl methods does work.
EDIT: Removing \include\curl from the Library path (-L) didn't work as well.
You have included what looks to be an include path not the library path. Is there a /lib directory under the curl path?
The c/c++ includes should be the path to the header files for curl, the linker path should be to the libraries not the headers, so .a and .dll files normally (normally under a /lib directory).

How to add a library to Eclipse C

I have seen several other answers in order to add a library to my C+= project in eclipse.I have tried to add the path to the linker in Miscellaneous section using -L"and the path of the folder" and -l"the name without the lib prefix in the begging and the .so at the end"
I try to add libxl library so i use -lxl (for libxl.so) and -L/home/username/libxl3.5.3.0/lib/ (which the location of the lib file).
I have also tried to give it under the Linker menu and adding the name and the path in the Libraries section.
I get error that: /usr/bin/ld does not find -lxl file and it returns error
I am using -static to linker in order to make an executable that has the all the libs included but when i do not use -static the problem with the lib resolves from build but still when i try to run the program i get error that i the program can not open shared file libxl.so cause the file does not exist.How can i fix this?
When you add the library name to a C++ project in eclipse, do not prefix it with -l. Eclipse will do this for you when it invokes the compiler. For example if you want the boost_regex library, just input boost_regex not lboost_regex. Eclipse will do the rest for you. Or in your specific case, just use xl not lxl. You don't need the - either, nor the -L before paths as erenon points out in the comment below. Note that the above applies to the method of adding libraries using the Project->Properties->C/C++ General->Paths and Symbols dialog form for adding libraries using the Libraries and Library Paths tabs.
You are trying to link statically to a shared library. In my experience I have always used *.a files rather than *.so files to employ static linkage. This other answer Static link of shared library function in gcc seems to suggest that you are not actually able to link statically to *.so files.

add armadillo libraries to g++ compiler in linux

I am trying to install a C++ library (armadillo) in a unix cluster where I do not have root permissions.
I managed to compile the C++ libraries without user permissions by running the following make command:
make install DESTDIR=my_usr_dir
But then in the armadillo readme file it says:
where "my_usr_dir" is for storing C++ headers and library files. Make sure your C++ compiler is configured to use the sub-directories present within this directory.
The compiler the armadillo uses to install the libraries is gcc-4.8.1. I am not sure where the compiler was installed but it's loaded when I start my session in the unix cluster.
After installing armadillo I am trying to compile open source code that uses the armadillo libraries. This open source code also has a makefile.
However, when I go to the open source code and I type in:
make
it calls g++. How can I make sure g++ will recognize the armadillo libraries previously installed in my_usr_dir?
Currently I get the following error if I go to src and then type make:
opencode.cpp:28:21: fatal error: armadillo: No such file or directory
#include <armadillo>
^
compilation terminated.
make: *** [mmcollapse] Error 1
you can use
alias gcc="gcc -I./my_usr_dir/include -L./my_usr_dir/lib"
and so on in your .bashrc file. In that way, whenever you invoke gcc on the command line, the flags will be added before every other argument you enter on the command line
I think the readme file refers to the usage of the library headers and library files from applications. For those to be useful, the compiler/linker/loader (usually all driven by the "compiler") have to know where to find them. The compiler always looks in some default directories, such as /usr/include (for headers) and /usr/lib/ (for libraries), but they require root permission to write into. However, you can tell the compiler with the flag -Idirectory to search in directory directory for headers. For libraries use -l and -L (check the manual page of your compiler). You may also need to consider the LD_LIBRARY_PATH and LD_RUN_PATH environment variables, if you're using dynamic linking (shared object files).
This question looks similar to
How to specify non-default shared-library path in GCC Linux? Getting "error while loading shared libraries" when running
If you dont want to change the .bashrc
use -rpath as suggested in the post above.
gcc XXX.c -o xxx.out -Lmy_usr_dir -lXX -Wl,-rpath=my_usr_dir
-L is for static linking
-rpath for adding the directory to the linker search path
more on -rpath here
I don't understand -Wl,-rpath -Wl,
Dont bother to upvote the answer because this is really not an answer. I would have commented but i could not locate the add comment for your post.

C++ linking boost library

First I built the Boost libraries that require building by going to /usr/local/boost_1_49_0/ and running the bootstrap.sh. This went off alright.
Step (1) created all the .so and .a files in /usr/local/boost_1_49_0/stage/lib
I tested linking against a library, say lboost_regex and #include <boost/regex> in my source code. This also went off alright.
Finally trying out the example on asio, I tried:
g++ -I/usr/local/boost_1_49_0 MAIN.cpp -o MAIN -L/usr/local/boost_1_49_0/stage/lib -lboost_thread -lboost_system -lpthread
(4) compiled alright. But when I run the program with ./MAIN, I get the following error:
./MAIN: error while loading shared libraries: libboost_system.so.1.49.0: cannot open shared object file: No such file or directory
The -L option only sets a compile-time library search path; if you want a shared library to be found at runtime then its directory must be known at runtime.
One way to set this with g++ is to pass -rpath to the linker, via the compiler; in your case you could say -Wl,-rpath -Wl,/usr/local/boost_1_49_0/stage/lib. (This embeds the directory in the executable.)
Another way is to install the libraries in a place that the linker searches by default (e.g. /usr/local/lib might be such a place, depending on how the system is configured).
Yet another way is to set an environment variable such as LD_LIBRARY_PATH (Linux or Solaris) or DYLD_LIBRARY_PATH (Mac OS X), to tell the linker where to search when launching executables from the shell where the variable is set.
Are you sure the shared library is in a place where the loader can find it? Either put it in a system wide directory or the same directory as the executable.
Here's a link with more information about the loader.