How to use OpenSSL with GCC? - c++

I'm trying to use openssl in a gcc program but it isn't working.
g++ server.cpp /usr/lib/libssl.a -o server
gives an error message, as does anything with the -l option. What must I type on the command line to link with openssl? The file /usr/lib/libssl.a exists, but nevertheless I still get the linker error no such function MD5() exists.

Without knowing the exact errors you are seeing, it is difficult to provide an exact solution. Here is my best attempt.
From the information you provided, it sounds as though the linker is failing because it cannot find a reference to the md5 function in libssl.a. I believe this function is actually in libcrypto so you may need to specify this library as well.
g++ server.cpp -L/usr/lib -lssl -lcrypto -o server

You or others may find this article developerWorks article helpful.
Secure programming with the OpenSSL API
https://developer.ibm.com/technologies/linux/tutorials/l-openssl
It describes most things you need to know to get off the ground with OpenSSL and C/C++. If you find you are following most of the same steps, it might help you see what needs doing.
Good luck.
update
revised link: https://developer.ibm.com/technologies/linux/tutorials/l-openssl
Which has been shuffled around
original link: http://www.ibm.com/developerworks/linux/library/l-openssl.html
Which now goes to a digest page including the article.
Note: keeping both links because they be used to find new discoveries.

In Eclipse IDE select Your Project property --> c/c++ Build --> Settings gcc c linker(from tools settings)--> add to Library Search Path (-L)
/usr/lib -lssl -lcrypto

The location of the library is not fixed. In my case (Ubuntu 18.04), the .a files are located in /usr/lib/x86_64-linux-gnu/. So here are the complete steps:
1) install the library,
sudo apt install libss-dev
2) check the installed files,
dpkg-query -L libssl-dev
3) change the gcc flags -L(library directory) -l(library name), e.g.,
gcc XXX.c XXXXX.c -L/usr/lib/x86_64-linux-gnu/ -lcrypto -lssl

On top of the accepted answers, I could not make compile the OpenSSL example for AES-CCM:
https://github.com/openssl/openssl/blob/master/demos/evp/aesccm.c
To make it work I needed to add two more things:
The Dinamic Linking Library : -ldl
The PThread library to use POSIX threading support: -pthread (Adding directly the library with -lpthread is not recommended )

Related

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.

Linking against a c/c++ library

I have some basic questions regarding linking against a C/C++ library. I am trying to understand the difference in using the two different usages -L/usr/local/lib -lm usage and /usr/local/lib/libm.a usage. E.g., when I compile and link an example from the [SUNDIALS] library, both of the following work
gcc -Wall cvRoberts_dns.c -o cvRoberts_dns.exe -I/usr/local/include -L/usr/local/lib/ -lsundials_cvode -lsundials_nvecserial -lm
OR
gcc -Wall cvRoberts_dns.c -o cvRoberts_dns.exe /usr/local/lib/libsundials_cvode.a /usr/local/lib/libsundials_nvecserial.a
However, to compile and link an example from the library [libsbml], the following works
g++ -Wall readSBML.cpp -o readSBML.exe -I/usr/local/include -L/usr/local/lib -lsbml
but the this does not
g++ -Wall readSBML.cpp -o readSBML.exe /usr/local/lib/libsbml.a
If required, I can post the complete error message I get, but the last line of the message is as follows
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My questions are as follows:
In the second style of linking (of the first example), there is no information regarding where to find the include files (header files), how does the compiler know the information supplied in -I/usr/local/include which is provided in the first style of the first example?
In the second style of first example there is no /usr/local/lib/libm.a (it actually gives an error message that libm.a cannot be found if I try to include it), then why -lm is required in the first style?
How do I compile the second example in the second style (i.e., using /usr/local/lib/libsbml.a)? I do see that there are files - libsbml.a and libsbml-static.a in the /usr/local/lib folder, but none of them work.
If it helps, I am on an OS X machine.
I would be very thankful if any one could help in this regard.
Just an update - I tried
g++ -Wall readSBML.cpp -o readSBML.exe /usr/local/lib/libsbml.5.dylib
and that compiled and linked just fine.
Thanks
SN
In general
The -L option is meant to find where the libraries themselves are. Each library is a collection of one or more object code (machine language) files. There is no need to find the include files.
The -I option has nothing to with linker, it helps the compiler resolve the header files used in your driver programme( eg Roberts_dns.c). This happens during the pre-processing stage.
In the second style of linking (of the first example), there is no
information regarding where to find the include files (header files),..
If the compilation worked as you expected,it may be because /usr/local/include is in the default include path for gcc. To check the default include path for gcc do gcc -xc -E -v -.
In the second style of first example there is no
/usr/local/lib/libm.a(it actually gives an error message that libm.a
cannot be found if I try to include it), then why -lm is required in
the first style?
In Linux, some libraries like libc.a are directly linked to your execultable by default while libm.a is not. In Mac (your environment), though, libm is directly link to the executable by default. So you don't have to explicitly link it. It is less likely that libm.a is located in /usr/local/lib/. So you got an error. But why link it in the first place?

Compiling C++ code on a system with two different Boost versions installed

I am trying to compile on a Ubuntu system where I have two different versions of boost instaled: 1.46.1 in /usr/lib/ and 1.61.0 in /usr/local/lib/.
I have stated that 1.61.0 is giving me some compilation issues, but I prefer not removing it to study the problem when I have more time. I supposed I could refer at the makefile to 1.46.1, using -I"/usr/include/boost" for 1.46.1 source, and then -L /usr/lib for the libraries. But still not working.
My suspicion is that the usage of -lboost_filesystem -lboost_system -lboost_date_time are somehow referencing to 1.61.0, even when used together with -L /usr/lib (which points to 1.46.1). But I have been unable to find information about this compilation flags for the linker.
How can I make sure those -l are referenced to the Boost version I want?
Here is an example of what the makefile is doing while linking (the part where it is failing):
Invoking: Cygwin C++ Linker
g++ -O0 -g3 -Wall -Wextra -o"../bin/MY_APP.exe" ../bin/objs/main.o ../bin/objs/FileLoad.o ../bin/objs/DatabaseLoad.o -L /usr/lib/mysql -L /usr/lib -lmysqlclient -lboost_filesystem -lboost_system -lboost_date_time
You can confirm or disprove your suspicions by getting the linker to confess which libraries it is actually using. Add the option -Wl,--verbose to your g++ link command line (in the makefile or try this directly). The linker will then spit out which exact file was matched for everything it was trying to link, including your boost libraries.
If it turns out it is in fact linking the wrong versions, it also gives you a clue as to why exactly, by specifying the exact order of paths which the linker tries in locating a given library. This should give you some ammo should you need to change around some of your options (e.g. ordering and/or content of -L... and -l... options)
Should this fail, you can also use the option -l:/path/to/exact/libboost_whatever.so. This will force to use the linker the given version. I would try this last though.

How to link with libtiff

How do I specify in my makefile that I want to link with the libtiff library. Just specifying -ltiff in LDFLAGS is not working.
Either you can use the -L flag (see either manual for the compiler - I am assuming that you are using gcc or g++
or
Set the environment variable LD_LIBRARY_PATH to include the path for the library
But you should also consider static or dynamic linking. The documentation for both compilers are very good to explain how to do either.
Try giving the following options for compilation.
g++ -L[path of libtiff on your machine] -ltiff [your remaining options] ...
-L option is used to tell gcc, where to find the librarires you are trying to link with.

compiling Boost linked libraries (Ubuntu)

I installed Boost via sudo apt-get install libboost-all-dev on the most recent version of Ubuntu. Now I want to compile a project that uses the Boost.Serialization library, which needs to be linked.
I've tried many variants of the following, without success:
gcc -I /usr/lib code.cpp -o compiled /usr/lib/libboost_serialization.a
and
gcc -I /usr/lib code.cpp -o compiled -l libboost_serialization
The error message is:
error: ‘split_member’ is not a member of ‘boost::serialization
`
What am I missing?
You are having troubles with compiling your code, not linking. On that stage it has nothing to do with libraries. At that point the fact that you have to link against something is irrelevant.
Make sure you are including boost/serialization/split_member.hpp directly or indirectly and get your code compiled first.
On a side note, -I flag is used to specify path to include files and not libraries. For libraries, use -L. But if you have installed Boost from apt, then it should already be in the path and so no additional -I or -L should be required. And when you specify -l, you have to emit lib from the beginning of library name and not put a space between a flag and its argument. Assuming working code, something like this should do:
g++ code.cpp -o compiled -lboost_serialization
I'd also recommend you pass -Wall flag to make compiler be more verbose and warn you about possible mistakes in your code.
split member is a problem with compiling where boost is assuming there are split calls for serialize and deserialize.
http://www.ocoudert.com/blog/2011/07/09/a-practical-guide-to-c-serialization/