How I can include OpenSSL in my Qt program on Linux? - c++

In my Qt program I have to convert a string to a hash. I want to use OpenSSL on Debian. Little brief of my code:
#include <openssl/sha.h>
...
unsigned char data[] = "data to hash";
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(data, sizeof(data) - 1, hash);
I have installed openssl and libssl-dev. And I also have changed my .pro document, looks like that:
TEMPLATE = app
QT = core gui
HEADERS += window.h
PKGCONFIG += openssl //found in another solution, but it does not work
LIBS += -L/usr/share/doc libssl-dev
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp window.cpp
In LIBS I have tried all possible locations that I get with sudo dpkg -L libssl-dev . Does anyone what I am missing? It doesn't compile and I sure is about that.
Thanks.
Here is the error when linking:
/usr/lib/x86_64-linux-gnu/qt4/bin/qmake -o Makefile App.pro
g++ -m64 -Wl,-O1 -o App main.o window.o moc_window.o -L/usr/lib/x86_64-linux-gnu -L/usr/share/doc/libssl-dev -libssl-dev -lQtGui -lQtCore -lpthread
/usr/bin/ld: cannot find -libssl-dev
collect2: error: ld returned 1 exit status
Makefile:105: recipe for target 'App' failed
make: *** [App] Error 1

In general if the library is libsomelib.so you must import it using -lsomelib, and in your case assuming that you have installed it using the system's tools it is not necessary to pass it a path, just add the following since the library is libssl.so:
LIBS += -lssl -lcrypto
or you can also use pkg-config:
PKGCONFIG += libssl
where /usr/lib/pkgconfig/libssl.pc is:
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: OpenSSL-libssl
Description: Secure Sockets Layer and cryptography libraries
Version: 1.1.0g
Requires.private: libcrypto
Libs: -L${libdir} -lssl
Libs.private: -ldl
Cflags: -I${includedir}

Related

How to solve compile error in gRPC generated files?

I am creating client-server application, using gRPC. So far I was not using TLS encryption. Now I want to enable it, and strangely I get this error. It seems, at least to me, that this is a linker error. What would be best ways to solve it?
CLion doesn't highlight anything, so I assumed everything was syntactically OK, but when compiling I got that error.
/usr/bin/ld: CMakeFiles/projectname.dir/main.cpp.o: in function `grpc::SslServerCredentials(grpc::SslServerCredentialsOptions const&)':
/home/username/projectname/third_party/grpc/include/grpcpp/security/server_credentials.h:60: undefined reference to `grpc_impl::SslServerCredentials(grpc::SslServerCredentialsOptions const&)'
collect2: error: ld returned 1 exit status
This is a makefile that I use to generate C++ code from gRPC specification:
HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
CXX = g++
CPPFLAGS += `pkg-config --cflags protobuf grpc`
CXXFLAGS += -std=c++11
ifeq ($(SYSTEM),Darwin)
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\
-lgrpc++_reflection\
-ldl
else
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
-ldl
endif
PROTOC = protoc
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
PROTOS_PATH = ./
vpath %.proto $(PROTOS_PATH)
%.grpc.pb.cc: %.proto
$(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
%.pb.cc: %.proto
$(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
clean:
rm -f *.o *.pb.cc *.pb.h
Then problem was solved like this:
I had to chage grpc++_unsecure to grpc++ under target_link_libraries in CMakeLists.txt cmake build configuration file.
I had forgotten/not thought about this at first.
target_link_libraries(bita_server
pqxx
sodium
protobuf::libprotobuf
# grpc++_unsecure
grpc++
SQLiteCpp
sqlite3
pthread
dl
${_PROTOBUF_LIBPROTOBUF}
)
In CMakeLists.txt under grpc folder, there are many target_link_libraries. which one to edit in order to include the change.

protobuf and pkg-Config missing libdir and includedirs

I was trying to build a binary for the protocol buffers C++ AddressBook tutorial. I compiled the protocol buffers successfully, but when I was building the binary with g++, I used pkg-config to get cflags and libs, and it failed to locate the necessary headers. I manually passed all the necessary flags, include directories, library directories, etc. to g++ and it worked.
pkg-config --cflags protobuf returns "-pthread"
pkg-config --libs protobuf returns "-lprotobuf -pthread -lpthread"
Meanwhile, protobuf.pc lists:
Cflags: -I${includedir} -pthread
Libs: -L${libdir} -lprotobuf -pthread -lpthread
I confirmed that the variables are set correctly with --variable=(include/libdir), and when using the --debug option, it confirms variables are set. However, it also shows that the -I and -L options are removed. The following is truncated debug output.
...Pasing pacage file protobuf.pc...
...Variable declarations...
Unknown keyword 'Libs.private' in protobuf.pc
...More variable declarations...
Adding 'protobuf' to list of known packages
Package protobuf has -I/usr/include in Cflags
Removing -I/usr/include from cflags for protobuf
Package protobuf has -L /usr/lib/x86_64-linux-gnu in Libs
Removing -L /usr/libx86-64-linux-gnu from libs for protobuf

gcc linker can't find library

I get a strange error while building my Qt C++ project on Ubuntu Linux using GCC 5.2.1:
/usr/bin/ld: cannot find -llibmath
I include external dynamic library to maky qmake project using command:
LIBS += -L/home/rem -llibmath
and I have library file at path /home/rem/libmath.so
As I can see from compiler output:
g++ -Wl,-rpath,/home/rem/Qt/5.5/gcc_64 -Wl,-rpath,/home/rem/Qt/5.5/gcc_64/lib -o Bazis main.o builder.o -L/home/rem -llibmath -L/home/rem/Qt/5.5/gcc_64/lib -lQt5OpenGL -L/usr/lib64 -lQt5QuickWidgets -lQt5Widgets -lQt5Quick -lQt5Gui -lQt5Sql -lQt5Test -lQt5Qml -lQt5Network -lQt5Core -lGL -lpthread
all parameters are correctly send by qmake to g++.
What is the source of my problem?
The solution is simple:
I changed my .pro file from:
LIBS += -L/home/rem -llibmath
to:
LIBS += -L/home/rem -lmath

How to supply compiler options in Qt?

I am trying to learn Qt, I have file test.cpp that I run via the terminal using the following command:
g++ `pkg-config --cflags --libs libsbml` test.cpp -L /usr/local/lib -lsbml -lstdc++ -lm
How can I suppl the same options to Qt?
Thank you.
You could write the qmake snippet below. In short, you would need to take a look at the following qmake variables:
LIBS
INCLUDEPATH
TEMPLATE
TARGET
HEADERS
SOURCES
CONFIG
PKGCONFIG
test.pro
TEMPLATE = app
TARGET = test
INCLUDEPATH += .
LIBS += -L /usr/local/lib -lsbml -lstdc++ -lm
unix {
CONFIG += link_pkgconfig
PKGCONFIG += libsbml
}
HEADERS += test.h
SOURCES += test.cpp
In .pro file add:
LIBS += -L /usr/local/lib -lsbml -lstdc++ -lm
look at the Makefile to figure out what variables are used. The makefile is in the build folder made by Qt.

qmake doesn't add libraries from .pro file to the makefile

I try to build an QT project using qmake. For this I need the boost library.
LIBS += -L/usr/lib/ \
-lboost_system \
-lboost_filesystem
But after running qmake, these libraries are not added to the makefile:
LIBS = $(SUBLIBS) -L/usr/X11R6/lib64 -lQt5MultimediaWidgets
-L/build/buildd/qtmultimedia-opensource-src-5.0.1/lib -L/usr/lib/x86_64-linux-gnu
-L/usr/lib/x86_64-linux-gnu/x86_64-linux-gnu -lQt5OpenGL -lQt5Multimedia -lpulse
-lQt5Widgets -lQt5Network -lQt5Gui -lQt5Core -lGL -lpthread
As expected, the linker prints many error like
/usr/include/boost/system/error_code.hpp:214: error: undefined reference to boost::system::generic_category()
If you want to take a look to the whole .pro file, go to https://raw.github.com/francisengelmann/FabScan100/master/qtTest/qtTest.pro
I am also having a similar problem with opencv. Does anyone know how to solve this issue ?
You need to run qmake again after changing the .pro file. Just removing your build directory is not enough.
Also are you sure your qmake target is linux-g++? Does the INCLUDEPATH work?