Compile C++ and OpenSSL on Ubuntu 11.10 - c++

I got a serious problem compiling my C++ and OpenSSL project on my Ubuntu 11.10.
The compiling command is:
g++ -Wall -lssl -lm -lcrypto -I ./src ./src/server.cpp -o ./bin/server
I receive these errors:
server.cpp:(.text+0x8ff): undefined reference to `RSA_new'
server.cpp:(.text+0x92d): undefined reference to `PEM_read_RSAPrivateKey'
server.cpp:(.text+0xa85): undefined reference to `RSA_size'
server.cpp:(.text+0xaa1): undefined reference to `RSA_size'
server.cpp:(.text+0xae7): undefined reference to `RSA_private_decrypt'
server.cpp:(.text+0xd84): undefined reference to `BF_set_key'
server.cpp:(.text+0xf1d): undefined reference to `BF_ecb_encrypt'
server.cpp:(.text+0x13c6): undefined reference to `BF_ecb_encrypt'
collect2: ld returned 1 exit status
make: *** [server] Error 1
I successfully installed openssl and libssl-dev but the problem persists.
I tried to compile the project on Linux Mint 12 with the kernel 3.0 and I had the same problem.
On my old Linux OS with the kernel 2.6 the project compiled and worked fine (using the same Makefile and the same sources).
Please help me!

Generally you need to have the -l link flags after the code that references them. Try
g++ -Wall -I ./src ./src/server.cpp -o ./bin/server -lssl -lm -lcrypto

As the comment to this answer states, the linker only looks for undefined symbols to include in the order the parameters are listed.
That is, if your cpp file uses the libraries, the libraries have to be listed after the cpp file.

Those error are from crypto library, check whether ssl and crypto libraries are available in /usr/lib or where ever u installed if not install them and have u set the library search path for libssl and libcrypto in your compiling command?

Related

C++ Boost: undefined reference to `boost::system::detail::system_category_ncx()`

I am trying to use Microsoft REST SDK to create a server listening to HTTP requests. My current computer is Ubuntu 18.04, the server code is pretty simple.
I have already installed the library by following the instructions on libboost official website and I have checked files in /usr/local/lib/, there are libboost_random.so.1.68.0 so I presume that I have libboost library.
However when I try to compile the code with the following command:
g++ server.cpp -o server -lpthread -lcpprest -lcrypto -lssl -lboost_system
It shows:
/tmp/ccKWADuo.o: In function `boost::system::system_category()':
server.cpp:(.text._ZN5boost6system15system_categoryEv[_ZN5boost6system15system_categoryEv]+0x7): undefined reference to `boost::system::detail::system_category_instance'
/tmp/ccKWADuo.o: In function `boost::system::generic_category()':
server.cpp:(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x7): undefined reference to `boost::system::detail::generic_category_instance'
collect2: error: ld returned 1 exit status
Thanks for any advices.
Boost consists of several independent libraries. Your linker error suggests you are missing libboost_system.so. You just mentioned libboost_random.so to be in your /usr/local/lib but is there also the required libboost_system.so?
[Problem solved] Reinstalling library libboost 1.68.0 helps. Error occurs when building from source, fixing those building errors will solve the problem.

g++ building c++ program with boost dependencies

I want to compile and run a simple c++ websocket application with g++ on windows.
Boost was installed like this:
./bootstrap.bat mingw
./b2.exe install --prefix=C:/boostLibs toolset=gcc
My c++ includes look like this:
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
This websocket sample was provided by https://github.com/zaphoyd/websocketpp
In order to build the project I issue this command:
g++ -Wno-deprecated -I ./cppServer/libs/ -I C:\boostLibs\include\boost-1_55 -L C:\boostLibs\lib -g ./cppServer/server.cpp -lboost_system
Which leeds me to this error message:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lboost_system
collect2.exe: error: ld returned 1 exit status
If I try to build without -lboost_system, I get a very long exception, starting with:
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:222: undefined reference to boost::system::generic_category()'
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:223: undefined reference toboost::system::generic_category()'
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:224: undefined reference to boost::system::system_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost6system10error_codeC1Ev':
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:323: undefined reference to boost::system::system_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost6system4errc20make_error_conditionENS1_6errc_tE':
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:488: undefined reference to boost::system::generic_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost16thread_exceptionC2EiPKc'
So what am I missing? I can't figure it out right now.
Link to boost_system as -lboost_system-mgw63-mt-1_55, because this is what those files are called. See boost library naming for more details.
When you build boost you may like to specify --layout=system to b2.exe so that your files do not have that -mgw63-mt-1_55 in the filename and then just use -lboost_system when linking against it.

Program with protocol-buffers don't compile with MinGW-w64: "undefined reference to google::protobuf:: ..."

I have installed the libprotobuf-dev=2.6.0-4 and protobuf-compiler=2.6.0-4 packages from Debian Jessie repository. Now I'm trying to compile a program that use the 'addressbook.proto' file from the Google Developers example with the MinGW-w64 compiler. I'm using Ubuntu 14.04.
With this command the program works:
$ g++ main.cpp addressbook.pb.cc -lprotobuf
But I want to compile for Windows too.
I added the symlink: /usr/include/google -> /usr/i686-w64-mingw32/include/google.
$ i686-w64-mingw32-g++ main.cpp addressbook.pb.cc -lprotobuf
/usr/bin/i686-w64-mingw32-ld: cannot find -lprotobuf
collect2: error: ld returned 1 exit status
With the library location still not working:
$ i686-w64-mingw32-g++ -L /usr/lib/i386-linux-gnu/ main.cpp addressbook.pb.cc -lprotobuf
/tmp/ccB1VJyR.o:main.cpp:(.text$_ZN6google8protobuf8internal27GetEmptyStringAlreadyInitedEv[__ZN6google8protobuf8internal27GetEmptyStringAlreadyInitedEv]+0x7): undefined reference to `google::protobuf::internal::empty_string_'
/tmp/ccB1VJyR.o:main.cpp:(.text$_ZN6google8protobuf8internal27GetEmptyStringAlreadyInitedEv[__ZN6google8protobuf8internal27GetEmptyStringAlreadyInitedEv]+0x2f): undefined reference to `google::protobuf::internal::empty_string_'
/tmp/ccPz4uiI.o:addressbook.pb.cc:(.text+0x78): undefined reference to `google::protobuf::DescriptorPool::generated_pool()'
/tmp/ccPz4uiI.o:addressbook.pb.cc:(.text+0x87): undefined reference to `google::protobuf::DescriptorPool::FindFileByName(std::string const&) const'
...
libprotobuf-dev contains library headers and pre-built binaries for your system. If you are cross-compiling you need to compile library from sources to target system as well. Something like "./configure CC=i686-w64-mingw32-g++" in library sources directory should do the job.

Linker error in g++ while trying to link to SDL_ttf shared object files

I am trying to use the SDL_ttf development library in an Ubuntu environment. I started by downloading the dev libraries via: sudo apt-get install libsdl-ttf2.0-dev.
Next, I imported the header file in my code as so:
#include <SDL_ttf.h>
When I compile with the command:
g++ -g -I /usr/include/SDL Main.cpp -lSDL -lSDL_image -lSDL_ttf land.cpp PerlinNoise.cpp Util.cpp org.cpp Init.cpp `sdl-config --libs
I get the following output:
/tmp/ccpLKljA.o: In function `init()':
/home/zoo/Desktop/World-A/Util.cpp:39: undefined reference to `TTF_Init'
/home/zoo/Desktop/World-A/Util.cpp:42: undefined reference to `TTF_Quit'
/home/zoo/Desktop/World-A/Util.cpp:48: undefined reference to `TTF_OpenFont'
/home/zoo/Desktop/World-A/Util.cpp:52: undefined reference to `TTF_Quit'
collect2: ld returned 1 exit status
I understand this is a linker error however I do not know what to do to fix it. I know that the -lSDL_ttf command is not doing anything because removing it does not change anything. I tried to reference the .so file however that didn't change the results. I did this reference via the command: -LlibSDL_ttf-2.0.so.0.6.3. I am able to view the .so file and it does contain the commands listed above.
You have to put land.cpp, and the rest of the CPP files, before the libraries on the command line.

Trouble with the compilation using libcurl on Windows XP

I downloaded the libcurl curl-7.34.0-devel-mingw32.zip from http://curl.haxx.se/gknw.net/7.34.0/dist-w32/curl-7.34.0-devel-mingw32.zip.
I use Eclipse Kepler with MinGW as the toolchain (GCC compiler). I created html.cpp and then test the code from the example http://curl.haxx.se/libcurl/c/simple.html:
I included the headers:
#include<iostream>
#include<curl/curl.h>
using namespace std;
There are 5 cURL functions. Only 2 of them (curl_easy_init,curl_easy_setopt)are said to be 'undefined reference'.
The compiler message:
Info: Internal Builder is used for build
g++ -O3 -Wall -c -fmessage-length=0 -o html.o "..\\html.cpp"
g++ -static-libgcc -static-libstdc++ -o HTML.exe html.o
html.o:html.cpp:(.text.startup+0x12): undefined reference to `_imp__curl_easy_init'
html.o:html.cpp:(.text.startup+0x1e): undefined reference to `_imp__curl_easy_setopt'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: html.o: bad reloc address 0x1e in section `.text.startup'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
The other 3 functions said to be fine:
curl_easy_perform
curl_easy_strerror
curl_easy_cleanup
I have tried to set to properties to tell the MinGW linker to include the headers and library files. The same situation as the above or the compiler simply says the .a files not found. Adding -lcurl or -DCURL_STATICLIB to the linker's flag is useless as well.
I have also tried to copy the libraries(.a files) and the headers(.h files) of libcurl to the folders bin, include and lib inside C:\MinGW and C:\gcc-4.8.1. It turns out to be no change.
Sadly, the first challenge of using cURL is not coding but the compilation. How can I solve this problem?
Thanks you guys!