I apologize that this is redundant, but none of the answers available seem to be able to solve my problem. I am attempting to compile an executable using a shared object library. The shared object library is called libsession.so and is found in the same directory that I am compiling the executable. To compile and link, I use the following command
g++ test_main.cpp -o program -std=c++11 -I ../src/base -L. -lsession
Unforutanely, I get the cannot find -lsession error when linking. If I change the command to directly reference the shared library as follows
g++ test_main.cpp -o program -std=c++11 -I ../src/base libsession.so
then the executable compiles/links and all is well. Does anyone have any thoughts as to what I may be doing wrong?
The only difference between using an '-l' option and specifying a file
name is that '-l' surrounds library with 'lib' and `.a' and searches
several directories.
https://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_3.html#SEC16
Related
I want to use a shared library (resides in a custom directory) into an executable.
I've created this makefile
all: SayHello
SayHello: compiledObjects/SayHello.o myLib/libNames.so
g++ compiledObjects/SayHello.o -o SayHello -Icommons -LmyLib -lNames
compiledObjects/SayHello.o: SayHello.cpp
g++ -c SayHello.cpp -o compiledObjects/SayHello.o
myLib/libNames.so: commons/Names.cpp commons/Names.h
g++ -shared -fPIC commons/Names.cpp -o myLib/libNames.so
That create correctly the executable and shared library infact I can Execute the program using this command
LD_LIBRARY_PATH=/custom/path/to/lib/myLib/libNames.so
./SayHello
How can I execute ./SayHello without specify LD_LIBRARY_PATH?
I'm not using any IDE and I'm on linux.
Use the -rpath option to link your executable. See the ld(1) manual page for more information.
P.S. Your makefile appears to have a bug. If you successfully make your program, and immediately run make again, looks like your makefile will attempt to recompile the program again, even though nothing has changed.
After all, the whole purpose of a makefile is to avoid doing unneeded compilations.
The SayHello.o build target should be compiledObjects/SayHello.o.
You need to tell g++ to pass the -rpath option to the linker using -Wl,-rpath. Also, you need to specify a path to the -rpath option.
Putting it all together your last build step should look like this:
SayHello: compiledObjects/SayHello.o myLib/libNames.so
g++ compiledObjects/SayHello.o -o SayHello -Icommons -LmyLib -lNames -Wl,-rpath=/custom/path/to/lib/myLib/
Relative RPATH:
If you want to specify an RPATH relative to your binary you should use
$ORIGIN as a placeholder: -rpath='$ORIGIN/rel/path'.
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.
I am compiling a C program with g++ and linking it to a library mylib.lib which is located in the same folder as the sourcecode by:
user$ g++ myprog.c -o mylib.lib
and the compiler behaves in a strange way. first of all it gives the error 'undefined reference to fun1' . this should not happen because fun1 is in mylib.lib. secondly it deletes mylib.lib . I also tried a different way:
user$ g++ myprog.c mylib.lib
In this case I get the same error: 'undefined reference to fun1'
Finally I tried to add mylib by:
renamed mylib.lib to libmylib.lib
2.
user$ g++ myprog.c -L/Dima/Tests -l mylib
In this case the error is 'cannot find lmylib' although it is located in /Dima/Tests. how do I compile it correctly?
You're not building a library, you are trying to create an executable program named mylib.lib. That's why the linker is invoked.
If you want to create a library, it depends on the toolchain you target, but generally speaking you only create object files, and create an archive of those object files.
For the GNU toolchain (GCC and the GNU binutils linker) you use the ar program to create this archive.
$ g++ mylib.c -c # Create object file named `mylib.o`
$ ar crv libmylib.a mylib.o # Create the static library
Now you have a static library containing the object file myprog.o which is compiled from myprog.c. To use it do something like
$ g++ myprog.c -L. -lmylib -o myprog
The above command tells the linker to look in the current directory (-L.) and link with the library mylib (the linker automatically looks for libmylib.a).
Also, when using the GNU linker it's important that you put the libraries after any source or object files.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Can I mix static and shared-object libraries when linking?
I want to compile my app, linking statically only boost_system library. Other(glibc and etc) should be linked dynamically. How can i do it?
My command to compile dynamically:
g++ -o newserver server.cpp ... -lboost_system -std=c++0x
Command to compile statically:
g++ -static -o newserver server.cpp ... -pthread -lboost_system -std=c++0x
But this command link statically all! And app weights 2mb more!
Can you advice me what command to compile statically only boost lib?
Thanks!
Replace -lboost_system with -Wl,-Bstatic -lboost_system -Wl,-Bdynamic. The -Wl option sends the thing after it to the linker, in the order it appears on the command-line.
There are two solutions. You can specify -Bstatic and
-Bdynamic in the command line, each affects all of the
libraries which follow it. Or you can arrange it that the
static versions of the libraries which you want to be linked
statically are present in a directory which is searched before
the directory which contains the dynamic version. This allows
you to make some sort of global decision: you create the
directory once, and all users you do a -L for it before the
-L general will use the static versions.
In practice, I can't think of a case where you'ld want to link
the Boost libraries other than statically, so the simplest
solution might just be to remove the .so files. The only time
g++ will make a decision (and take into account the -Bstatic
and -Bdynamic) is if it finds both in the same directory. It
searches the directories in the given order, and when it finds
a directory which has either the static or the dynamic version
of the library, it stops. And if only one version is present,
it uses that one, regardless.
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