Undefined reference to curlpp using code::blocks on kubuntu - c++

I have a problem with curlpp library. I'll explain the steps that I followed.
Step 1: download and installation
download website: Download
$./configure
$make
$sudo make install
curlpp header files are located in /usr/local/include/
curlpp library files are located in /usr/local/lib/
Step 2: I used the following code:
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
using namespace std;
int main()
{
char *url = (char*) "http://dbpedia.org/sparql";
string queryString = "PREFIX dbp: <http://dbpedia.org/resource/> "
"PREFIX dbp2: <http://dbpedia.org/ontology/> "
"SELECT ?abstract "
"WHERE { "
"dbp:Nikola_Tesla dbp2:abstract ?abstract . "
"FILTER langMatches(lang(?abstract), 'en')"
"}";
try
{
curlpp::Easy request;
string parameters = "query=" + curlpp::escape(queryString);
request.setOpt(new curlpp::options::Url(url));
request.setOpt(new curlpp::options::Verbose(true));
request.setOpt(new curlpp::options::PostFields(parameters));
request.perform();
}
catch (curlpp::RuntimeError & e)
{
std::cout << e.what() << std::endl;
}
catch (curlpp::LogicError & e)
{
std::cout << e.what() << std::endl;
}
return 0;
}//end function main
Errors
Undefined reference to curlpp ::Easy:Easy()
Undefined reference to curlpp ::escape (const std :: string &)
Undefined reference to curlpp ::Easy::setopt (curlpp OptionBase :: *)
Undefined reference to curlpp curlpp::Easy::setopt(curlpp OptionBase:: *)
etc.
After adding -lcurlpp as the picture shows: I got the following errors:
g++ -LSQLiteCpp-master/debug -o bin/Debug/EntityLinking
obj/Debug/DataLoader.o obj/Debug/Entity.o obj/Debug/Fact.o
obj/Debug/FactClass.o obj/Debug/Link.o obj/Debug/main.o
obj/Debug/ManageDb.o obj/Debug/SQLiteCpp-master/sqlite3/sqlite3.o
obj/Debug/tinyxml/tinystr.o obj/Debug/tinyxml/tinyxml.o
obj/Debug/tinyxml/tinyxmlerror.o obj/Debug/tinyxml/tinyxmlparser.o
-lpthread -ldl -lcurlpp SQLiteCpp-master/debug/libSQLiteCpp.a /usr/bin/ld: obj/Debug/main.o: référence au symbole non défini
«curl_easy_setopt##CURL_OPENSSL_3»
//usr/lib/x86_64-linux-gnu/libcurl.so.4: error adding symbols: DSO
missing from command line collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s)) 0
error(s), 0 warning(s) (0 minute(s), 0 second(s))

You need to link with -lcurlpp when compiling & linking your code.

Using Eclipse, I never got curlpp example01 working. Even after trying to set up the linkers and the includes. But I was able to compile it from a terminal:
g++ -o exe_name exe_name.cpp -L/usr/local/lib -lcurl -lcurlpp -I/usr/local/include
So I would suggest trying the terminal if Eclipse doesn't work for you.

Related

Unable to create an OpenSSL project with CLion

I am trying to create a simple OpenSSL poject in CLion but it can suceed in linking it.
This is my final CMakeLists.txt file (after many tries) which gives less errors:
cmake_minimum_required(VERSION 3.16)
project(duplicates_finder)
set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L . -lssl -lcrypto")
set(SOURCE_FILES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
find_package(OpenSSL REQUIRED)
if (OPENSSL_FOUND)
# Add the include directories for compiling
target_include_directories(${PROJECT_NAME} PUBLIC ${OPENSSL_INCLUDE_DIR})
# Add the static lib for linking
target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto)
message(STATUS "Found OpenSSL ${OPENSSL_VERSION}")
else()
message(STATUS "OpenSSL Not Found")
endif()
include_directories(C:\\OpenSSL-Win32\\include)
link_directories(C:\\OpenSSL-Win32\\lib\\MinGW)
This is the file I am trying to compile:
#include <string>
#include <iostream>
#include <filesystem>
#include <string>
#include <openssl/sha.h>
#include "openssl/ssl.h"
#include <sstream>
#include <iomanip>
using namespace std;
namespace fs = std::filesystem;
//string sha256(const string& str)
//{
// unsigned char hash[SHA256_DIGEST_LENGTH];
// SHA256_CTX sha256;
// SHA256_Init(&sha256);
// SHA256_Update(&sha256, str.c_str(), str.size());
// SHA256_Final(hash, &sha256);
// stringstream ss;
// for(unsigned char i : hash)
// {
// ss << hex << setw(2) << setfill('0') << (int)i;
// }
// return ss.str();
//}
int main() {
std::cout << "SSLeay Version: " << SSLeay_version(SSLEAY_VERSION) << std::endl;
SSL_library_init();
auto ctx = SSL_CTX_new(SSLv23_client_method());
if (ctx) {
auto ssl = SSL_new(ctx);
if (ssl) {
std::cout << "SSL Version: " << SSL_get_version(ssl) << std::endl;
SSL_free(ssl);
} else {
std::cout << "SSL_new failed..." << std::endl;
}
SSL_CTX_free(ctx);
} else {
std::cout << "SSL_CTX_new failed..." << std::endl;
}
}
And these are the errors:
====================[ Build | all | Debug ]=====================================
"C:\Program Files\JetBrains\CLion 2020.1\bin\cmake\win\bin\cmake.exe" --build C:\Users\USERNAME\CLionProjects\duplicates_finder\cmake-build-debug --target all -- -j 8
[ 50%] Linking CXX executable duplicates_finder.exe
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\duplicates_finder.dir/objects.a(main.cpp.obj): in function `main':
C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:42: undefined reference to `SSLeay_version'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:43: undefined reference to `SSL_library_init'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:44: undefined reference to `SSLv23_client_method'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:44: undefined reference to `SSL_CTX_new'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:46: undefined reference to `SSL_new'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:48: undefined reference to `SSL_get_version'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:49: undefined reference to `SSL_free'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:53: undefined reference to `SSL_CTX_free'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [duplicates_finder.exe] Error 1
CMakeFiles\duplicates_finder.dir\build.make:87: recipe for target 'duplicates_finder.exe' failed
mingw32-make.exe[1]: *** [CMakeFiles/duplicates_finder.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/duplicates_finder.dir/all' failed
Makefile:82: recipe for target 'all' failed
I restared my PC.
I also added the line:
target_link_libraries(${PROJECT_NAME} libeay32.lib) //also .a file the result is the same.
And it throws the error that it can't find that file.
Also added the files: libeay32.a, libeay32.def, libeay32.lib and ssleay32.a, ssleay32.def, ssleay32.lib to the project folder and also to the cmak-build-debug folder. => Still not working.
Also renamed libeay32.a to libeay32.dll.a and ssleay32.a to ssleay32.dll.a as on the another topic from stackoverflow says but it is still not working too.
No matter I do it is not compiling at all.
I spent all day searching for a solution but in vain.
I am using Windows 7 x64 and OpenSSL 1.1.1m 14 Dec 2021.
Thank you in advance!

g++ linker can not find library

I want to use this library in my c++/test.cpp file.
#include "omp/HandEvaluator.h"
#include <iostream>
using namespace omp;
int main()
{
HandEvaluator eval;
Hand h = Hand::empty(); // Final hand must include empty() exactly once!
h += Hand(51) + Hand(48) + Hand(0) + Hand(1) + Hand(2); // AdAs2s2h2c
std::cout << eval.evaluate(h) << std::endl; // 28684 = 7 * 4096 + 12
}
I downloaded the source code from github and placed into the OMPEval folder. After make the ompeval.a library appeared.
Here is the folder structure:
Now I try to build it:
projects/c++$ g++ -Wall -g -L /home/a/projects/c++/OMPEval/lib/ -l ompeval -I /home/a/projects/c++/OMPEval/ test.cpp -v
but the linker has error:
/usr/bin/ld: cannot find -lompeval
collect2: error: ld returned 1 exit status
Here is the whole build log:

cpp-netlib complains about missing lboost-thread

I'm learning cpp-netlib and I tried running the exmaple client given on the official website. The code is very simple:
#include <boost/network/protocol/http/client.hpp>
#include <iostream>
int main(int argc, char *argv[]) {
using namespace boost::network;
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " [url]" << std::endl;
return 1;
}
http::client client;
http::client::request request(argv[1]);
request << header("Connection", "close");
http::client::response response = client.get(request);
std::cout << body(response) << std::endl;
return 0;
}
And here is my makefile for this c++ application:
CC = g++ -std=c++11
CFLAG = -I/usr/local/Cellar/boost/1.57.0/include
LIBFLAG = -L/usr/local/Cellar/boost/1.57.0/lib
all: client
client: client.o
$(CC) $(LIBFLAG) -lboost_system -lboost_thread client.o -o client
client.o: client.cpp
$(CC) -c $(CFLAG) client.cpp
clean:
rm -rf *.o client
It complains about not finding lboost_thread library after compilation:
ld: library not found for -lboost_thread
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [client] Error 1
In my boost library directory, the boost_thread library shows up like this:
libboost_thread-mt.a libboost_thread-mt.dylib
Why isn't it able to find this library? Did I make any mistake in the linking?
Try changing your makefile to link to -lboost-thread-mt instead of -lboost-thread.
You seems to be missing libboost_thread for some reason

How to resolve compilation errors for boost multithreading simple program

I have a problem with compiling a boost integrated source code in my PC. I am working on below environment
OS: CentOs 6.3
Boost version: 1.41
Boost installation header file directory: /usr/include/boost/
IDE: code::block
Program for the compilation:
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
void ThreadFunction()
{
int counter = 0;
for(;;)
{
std::cout << "thread iteration " << ++counter << " Press Enter to stop" << std::endl;
try
{
// Sleep and check for interrupt.
// To check for interrupt without sleep,
// use boost::this_thread::interruption_point()
// which also throws boost::thread_interrupted
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
}
catch(boost::thread_interrupted&)
{
std::cout << "Thread is stopped" << std::endl;
return;
}
}
}
int main()
{
// Start thread
boost::thread t(&ThreadFunction);
// Wait for Enter
char ch;
std::cin.get(ch);
// Ask thread to stop
t.interrupt();
// Join - wait when thread actually exits
t.join();
std::cout << "main: thread ended" << std::endl;
return 0;
}
Error output:
Build: Debug in testPro (compiler: GNU GCC Compiler) ===|
obj/Debug/main.o||In function `main':|
<path to code>/main.cpp|40|undefined reference to `boost::thread::interrupt()'|
<path to code>/main.cpp|43|undefined reference to `boost::thread::join()'|
<path to code>/main.cpp|47|undefined reference to `boost::thread::~thread()'|
<path to code>/main.cpp|47|undefined reference to `boost::thread::~thread()'|
obj/Debug/main.o||In function `thread_data_base':|
/usr/include/boost/thread/pthread/thread_data.hpp|65|undefined reference to `vtable for boost::detail::thread_data_base'|
obj/Debug/main.o||In function `void boost::this_thread::sleep<boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000l> >(boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000l> const&)':|
/usr/include/boost/thread/pthread/thread_data.hpp|122|undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'|
obj/Debug/main.o||In function `thread<void (*)()>':|
/usr/include/boost/thread/detail/thread.hpp|191|undefined reference to `boost::thread::start_thread()'|
obj/Debug/main.o||In function `~thread_data':|
/usr/include/boost/thread/detail/thread.hpp|40|undefined reference to `boost::detail::thread_data_base::~thread_data_base()'|
]+0x10)||undefined reference to `typeinfo for boost::detail::thread_data_base'|
||=== Build failed: 9 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
As in the error console compiler cannot find the methods declared in boost library. I tried the several sample applications but result was the same. Please help me to find out the problem or suggest me a way to check the status of boost installation.
You need to compile your code with boost_thread library -lboost_thread
for example
gcc test.c -o test -lboost_thread

How to compile Apache Avro C++ example

Probably a newbie mistake, but can anyone please tell me what I'm doing wrong here?
Any help is much appreciated.
I wrote this simple Makefile:
CC=g++
INC=-I/usr/local/avro-cpp-1.7.2 -I/usr/local/boost_1_53_0
cpx : generated.cc
$(CC) -o cpx generated.cc $(INC)
Which generates these the errors:
g++ -o cpx generated.cc -I/usr/local/avro-cpp-1.7.2 -I/usr/local/boost_1_53_0
/tmp/ccYymUVo.o: In function `main':
generated.cc:(.text+0x84): undefined reference to `avro::memoryOutputStream(unsigned long)'
generated.cc:(.text+0xb0): undefined reference to `avro::binaryEncoder()'
generated.cc:(.text+0x11e): undefined reference to `avro::memoryInputStream(avro::OutputStream const&)'
generated.cc:(.text+0x150): undefined reference to `avro::binaryDecoder()'
collect2: ld returned 1 exit status
make: *** [cpx] Error 1
Here's the source as supplied in the Avro examples directory:
#############################
# cpx.hh
#############################
#ifndef CPX_HH_1278398428__H_
#define CPX_HH_1278398428__H_
#include "boost/any.hpp"
#include "avro/Specific.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
namespace c {
struct cpx {
double re;
double im;
};
}
namespace avro {
template<> struct codec_traits<c::cpx> {
static void encode(Encoder& e, const c::cpx& v) {
avro::encode(e, v.re);
avro::encode(e, v.im);
}
static void decode(Decoder& d, c::cpx& v) {
avro::decode(d, v.re);
avro::decode(d, v.im);
}
};
}
#endif
and
#############################
# generated.cc
#############################
#include "cpx.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
int
main()
{
std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream();
avro::EncoderPtr e = avro::binaryEncoder();
e->init(*out);
c::cpx c1;
c1.re = 1.0;
c1.im = 2.13;
avro::encode(*e, c1);
std::auto_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
avro::DecoderPtr d = avro::binaryDecoder();
d->init(*in);
c::cpx c2;
avro::decode(*d, c2);
std::cout << '(' << c2.re << ", " << c2.im << ')' << std::endl;
return 0;
}
Thanks.
1)
in your output, you don't link against the avrocpp library, although your comments about the LD_LIBRARY_PATH suggest that you got the link to work, but the example program failed to find the library when executing. Your compile line needs the linker option: -l avrocpp
2)
If the avrocpp library .so is not installed in a path that your runtime linker is configured to search, you can add the library path into the binary itself with the linker option -rpath.
Example 1: Assuming your avrocpp library is installed in /usr/local/lib (i.e. /usr/local/lib/libavrocpp.so), and your runtime linker doesn't look in /usr/local/lib, add the rpath by adding this option to your compiler command line:
-Wl,-rpath,/usr/local/lib
Example 2: Assuming your avrocpp library is installed in /usr/local/avro-cpp-1.7.2/lib (i.e. /usr/local/avro-cpp-1.7.2/lib/libavrocpp.so), add the rpath by adding this option to your compiler command line:
-Wl,-rpath,/usr/local/avro-cpp-1.7.2/lib
Try using avrogencpp with options -i < schema_file > -o < header_file >
Download and compile the Avro c++ sources https://stackoverflow.com/questions/40889705/how-to-compile-apache-avro-c-on-windows (this will generate the "avrogencpp.exe")
Download avro-tools http://avro.apache.org/releases.html#Download, and (with java installed)
run:
java -jar avro-tools-1.8.1.jar idl file.avdl > file.json
Edit the generated json file and delete the following
If you have any lines preceding the required structs at the beginning, delete them. (I had "protocol": "file", "namespace" : "some_optional_namespace", "types" :)
messages entry (at the bottom)
If you have multiple structs defined in the same json file, surround them all with "[ ]" (you should have commas between the different structs and make sure the last struct as not proceeded with a ","
Run
avrogencpp.exe -p - -n some_optional_namespace -U --input file.json --output file.hh