How do you load a library with cling? - c++

This might not even be a cling question, I'm a C++ newbie.
I am trying to play around with a library called QuantLib in the cling REPL.
I'm able to load the library in GCC by doing
#include "ql/quantlib.hpp"
and then compiling with -lQuantLib.
In cling I've been trying permutations of the 3 lines below:
.I "ql/quantlib.hpp"
#include "ql/quantlib.hpp"
.L QuantLib
If I run the #include first, I get a very long error, including stuff like
You are probably missing the definition of
QuantLib::AbcdAtmVolCurve::accept(QuantLib::AcyclicVisitor&) Maybe you
need to load the corresponding shared library?
But if I run
.I "ql/quantlib.hpp"
#include "ql/quantlib.hpp"
then all seems well.
.L Quantlib results in
input_line_4:1:10: fatal error: 'QuantLib' file not found
#include "QuantLib"
regardless of when it's run.
I tried the following after kfsone's comment
.L /usr/lib/libQuantLib.so
#include "ql/quantlib.hpp"
This gives a short error!
IncrementalExecutor::executeFunction: symbol '_ZN8QuantLib5ErrorC1ERKSslS2_S2_' unresolved while linking function '__cxx_global_var_init34'!
You are probably missing the definition of QuantLib::Error::Error(std::string const&, long, std::string const&, std::string const&)
Maybe you need to load the corresponding shared library?

Cling needs to know both the syntax of the structures/functions you want to use and have the binary code that executes.
For the syntax you have to add the include, for example like this:
#include "myfile.hpp"
For the binary code, you have to load the library like this:
#pragma cling load("myfile.so.9.220.0")

Related

Compile C++ with static lib

this will probably a dumb question for you guy's but I have no experience in C++ what so ever. I'm using an open source project osrm (which is awesome). Still to request a route, you have make an http request. To reduce the running time, I would like to build a wrapper around the code and call it using the command line. So I googled a bit and found that osrm already creates a static lib (.a file) when compiling the project. I also found a piece of code that points me in the right directions for building a wrapper. So to begin I build a simple hello world program (see below) that includes some files from that static lib. To compile I followed this tutorial.
My directory structure looks like this:
./helloWorld.cpp
./libs/libOSRM.a
And the command to compile is this:
gcc –static helloworld.cpp –L ./libs –l libOSRM.a
The code it selve:
#include "Router.h"
#include "boost/filesystem/path.hpp"
#include "ServerPaths.h"
#include "ProgramOptions.h"
#include <InternalDataFacade.h>
#include <viaroute.hpp>
#include <iostream.h>
main()
{
cout << "Hello World!";
return 0;
}
the exact error I got:
fatal error: ServerPaths.h: No such file or directory #include "ServerPaths.h"
Add the -IPathToTheHeaderFiles to the compiler options. So it will find the files to be included. Replace PathToTheHeaderFiles with the path where your file ServPaths.h resides.
Edit: Add as many -I as you need for further header files.
Additionally it would be worth to read a book about C++ or/and the GCC manual1
1 Section 3.11 will help.

How do I include Boost.Interprocess and Boost.DateTime properly?

This is a really basic question because I am a C++ newbie. I want to use the Boost.Interprocess library, but am having trouble building it. I'm trying to follow these instructions, but it's not working for me. Here is what I have:
#define BOOST_DATE_TIME_NO_LIB
#include <boost/interprocess/shared_memory_object.hpp>
#include <iostream>
using namespace std;
int main() {
cout << "Hello, beautiful world!\n";
}
But I get this error:
boost_1_55_0\boost\date_time\gregorian_calendar.hpp(63) : fatal error C1083: Cannot open include file: 'boost/date_time/gregorian_calendar.ipp': No such file or directory
I know Boost is able to load properly, because I can get an example that uses #include <boost/lambda/lambda.hpp> to work just fine. It's just when I try to include the Boost.Interprocess library that I am having trouble. The cause is clearly because it's having trouble including the Boost.DateTime library properly, but according to the documentation (linked above) I should be able to get by without separately compiling Boost.DateTime if I define BOOST_DATE_TIME_NO_LIB, right?
What am I missing here?
You need to add it to the preprocessor
In VS go to - Project >> properties >> C/C++ >> Preprocessor in the 'Preprocessor Definitions' paste BOOST_DATE_TIME_NO_LIB.
You can download boost libraries here: https://www.boost.org/users/download/
After that, you can include them in your projects. Also, you can check this video on how to add boost libraries in eclipse IDE on Ubuntu: https://www.youtube.com/watch?v=gN8zrnWxFeI

Boost build error with websocketpp and MySQL on Windows

I am trying to build a C++ app that uses both websocketpp and MySQL. I have encountered 2 build problems using VS 2010 C++ Express.
1) A problem with the boost libraries. It produces many errors like this:
1>c:\program files (x86)\boost\boost_1_50\boost\thread\win32\thread_data.hpp(210): error C2146: syntax error : missing ')' before identifier 'rel_time'
Here's the relevant snippet from thread_data.hpp starting with line 210:
inline BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
{
interruptible_wait(detail::pin_to_zero(rel_time.total_milliseconds()));
}
2) A conflict with the word VERSION which is documented here and I believe is independent.
To make a clear and simple example of the boost build problems, I'm using the websocketpp example: echo_server.cpp to which I added these includes:
#include "stdafx.h"
Boost lib includes recommended by "Building a program with websocketpp" on the websocketpp site.
#include <boost/regex.hpp>
#include <boost/random.hpp>
#include <boost/system/api_config.hpp>
#include <boost/system/config.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/system/windows_error.hpp>
and the MySQL header includes. Adding these 2 boostincludes triggers the build errors. If I comment out these 2 includes, it builds without errors:
#include <my_global.h>
#include <mysql.h>
Any suggestions on how to deal with the boost problems?
I don't think this is the same build problem as this one, "Trying to build websocket++ with MinGW: last few linker errors — what could it be?"
Concerning the first error, check if there are any macros interfering with the code. Right-click and go to definition or #define the macro yourself at the beginning of the file and see where it gets redefined. In really hard cases, look at the preprocessor output via a compiler flag.
Concerning the rest, you don't provide any versions for Boost and MySQL. Then, there is my_global.h (or is that part of MySQL?) and stdafx.h, which are both under your control but you don't show them here. Also, try to reduce the issue to the smallest possible piece of code. In short, provide a reproducible example.

Error with zip_open from libzip

I am learning C++, and I decided to make a little program that zip/unzip files to train me.
I downloaded libzip and zlib and linked them to my compiler (MinGW with Code::Blocks on Windows). So I tried to open my zip file with zip_open() and got an error :
undefined reference to _imp__zip_open
Here is the code:
#include <zip.h>
#include <zlib.h>
int main()
{
int error(0);
zip *foo = zip_open("foo.zip", 0, &error);
return 0;
}
I don't know where this is coming from and I would really like some help, because I don't find anything on Google (surely cause the problem is simple).
Thanks in advance!
It looks like you haven't linked to libzip. Make sure you are infact linking to it, and that the path to the lib is in your link path.
Judging by the discussion on this thread from the libzip-discuss list it looks like you are trying to link against a static version of libzip but with the preprocessor symbol ZLIB_DLL defined. You should only have ZLIB_DLL defined if linking against the dll version.

using curlpp on Windows. how to start

I'm newbie to c++, and I'm using mingw compiler. the last line
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
....
string url ("http://www.google.com");
curlpp::Cleanup myCleanup;
curlpp::options::Url myUrl(url);
throws the following long error while compilation
C:\Users\UserName\AppData\Local\Temp\ccpkmZ1C.o:test.cpp:(.text$_ZN6curlpp11Option
TraitISsL10CURLoption10002EED1Ev[curlpp::OptionTrait<std::basic_string<char, std
::char_traits<char>, std::allocator<char> >, (CURLoption)10002>::~OptionTrait()]
+0xb): undefined reference to `_imp___ZTVN6curlpp11OptionTraitISsL10CURLoption10
002EEE'
...
...
What's wrong here? Do I need to do something more then copying include folders from curlpp and libcurl to use curlpp?
Thank you in advance!
This is a linker error.
You need to add the libcurl libraries to the build command.
Something like:
g++ test.cpp -o test -llibcurlpp
I am not sure of the exact name of the libcurl library.
In your curlpp distribution (.tar.gz file, usually) you can find all the documentation you need. Basically, you may want to take a look at README, doc\guide.pdf and include\curlpp\ directory for class/function definitions.
If you don't have any of these, make sure you get the latest sources, as today.
Hope it helps.