How to compile C++ mongo project - c++

I need help! How to compile c++ mongo project in linux?
I'm doing this:
1) Install boost
2) Compile mongodb driver
3) Try to compile example (fail)
My compile mongodb drivers exist in /home/developer/documents/drivers/mongo-cxx-driver-v2.4/build
I'm trying to compile this file
#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h"
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
And execute this command: g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -lboost_system -o tutorial
This command fail. Error message - "mongo/client/dbclient.h" not found. How to compile this example? Help me, please!

You need to use -I and -L to specify where you have installed your mongo header(s) and library(ies):
g++ tutorial.cpp -I/path/to/mongo/include/ -pthread -L/path/to/libmongoclient
-lboost_thread-mt -lboost_filesystem -lboost_program_options
-lboost_system -o tutorial

Related

Eclipse issue linking sqlite3 library

Installed https://www.mingw-w64.org/ and added the libs but still getting this error:
Info: Internal Builder is used for build
g++ -std=c++11 -O3 -Wall -c -fmessage-length=0 -o main.o "..\\main.cpp"
g++ -std=c++11 "-LF:\\MinGW\\lib" -o test.exe main.o -lpsapi -lpthread -lm -ldl
main.o:main.cpp:(.text.startup+0x1f): undefined reference to `sqlite3_open'
main.o:main.cpp:(.text.startup+0x36): undefined reference to `sqlite3_errmsg'
f:/mingw/bin/../lib/gcc/i686-w64-mingw32/4.8.3/../../../../i686-w64-mingw32/bin/ld.exe: main.o: bad reloc address 0x36 in section `.text.startup'
collect2.exe: error: ld returned 1 exit status
Used the installer and choosed "Native Windows" + "i686".
The code:
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
#include <sqlite3.h>
sqlite3 *db;
int main() {
int open = sqlite3_open("test.db", &db);
if( open ) {
cerr << "Can't open database: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
exit(1);
}
return 0;
}

Linker cannot find local shared library

I'm trying a very simple exmaple to create a shared library and link to it. The shared library is as follows:
#ifndef ARDUGRAB_H_
#define ARDUGRAB_H_
#include <iostream>
using namespace std;
namespace ArduGrabLibrary{
class ArduGrab{
public:
ArduGrab();
virtual void initCamera();
virtual void setSim(bool sim);
virtual void setDebug(bool debug);
private:
bool debug = false;
bool sim = false;
};
}
Then the source code file is just as simple:
#include "ardugrab.h"
namespace ArduGrabLibrary
{
ArduGrab::ArduGrab(){
std::cout << "IMX298 Constructor" << std::endl;
}
void ArduGrab::initCamera(){
if (this->debug){
cout << "init camera" << std::endl;
}
}
void ArduGrab::setSim(bool sim){
this->sim = sim;
if (this->debug){
cout << "set sim to " << std::boolalpha << this->sim << std::endl;
}
}
void ArduGrab::setDebug(bool debug){
this->debug = debug;
if (this->debug){
cout << "set debug to " << std::boolalpha << this->sim << std::endl;
}
}
}
I'm then compiling that into a shared library with:
g++ -fPIC -shared -o ardugrab.so ardugrab.cpp
All good, we get an ardgrab.so library so to test it, with the following code in teh same directory as the .so and .h files from above:
#include "ardugrab.h"
using namespace ArduGrabLibrary;
int main() {
std::cout << "starting program" << std::endl;
ArduGrab* ardu = new ArduGrab();
ardu->setDebug(true);
//imx298->setSim(true);
//imx298->initCamera();
return 0;
}
So now we need to compile this into an executable with:
g++ -L. -lardugrab -o testardugrab testardugrab.cpp
This however fails to find the ardugrab.so file, the follow error message appears:
pi#raspberrypi:~/ArduMipiGrab $ g++ -L. -lardugrab -o testardugrab testardugrab.cpp
/usr/bin/ld: cannot find -lardugrab
collect2: error: ld returned 1 exit status
I've tried setting LD_LIBRARY_PATH to . export LD_LIBRARY_PATH=. but still nothing.
As you can see I'm a bit new with compiling c++, can someone please advise me as to what I'm doing wrong?
Thanks.
Reagrds,
Neil
This is becuase you are using the -l flag.
When you use this flag (Rather than specify a library specifically) it assumes a certain naming convention.
-lX
The linker assumes the file name is
libX.so (or libX.a)
So the commands you want are:
> g++ -fPIC -shared -o libardugrab.so ardugrab.cpp
> # ^^^
> g++ -L. -lardugrab -o testardugrab testardugrab.cpp
Note: The environment variable LD_LIBRARY_PATH is used at runtime when the standard library tries to find and load required shared libraries. I.E. it is not used during compilation to find shared libraries to link with.

Parsing LLVM IR from bitcode file

I am trying to parse LLVM IR from a bit code file. I went through the following steps.
hello.cpp
#include <iostream>
int main() {
std::cout << "Hello world!" << "\n";
return 0;
}
dump.cpp
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
using namespace llvm;
int main()
{
LLVMContext context;
SMDiagnostic error;
Module *m = parseIRFile("hello.bc", error, context).get();
if(m)
{
m->dump();
}
return 0;
}
$ clang++ -O3 -emit-llvm hello.cpp -c -o hello.bc
$ clang++ -g -O3 dump.cpp llvm-config --cxxflags --ldflags --system-libs --libs all -o dump
$ ./dump
Assertion failed: (Val && "isa<> used on a null pointer"), function doit, file /Users/chamibuddhika/Builds/llvm/include/llvm/Support/Casting.h, line 106.
Abort trap: 6
So I get the above error at the end. What may be causing this? I am on llvm-6.0 rc2.

Lua shared object loading with C++ segfaults

For a project I'm writing I need to write a custom Lua module loading system, and I've done it before on my Raspberry Pi, but not on my Mac. The problem is that as soon as I try to access the lua_State in the shared object, the program segfaults.
main.cpp
#include <lua.hpp>
#include <dlfcn.h>
#include <iostream>
typedef void Register(lua_State*);
int main(){
lua_State* L = luaL_newstate();
void* lib = dlopen("module.so", RTLD_NOW);
if(!lib){
std::cerr << "Error opening module \"" << "\": " << dlerror() << std::endl;
return;
}
Register* loadFunc = (Register*)dlsym(lib, "RegisterModule");
if(!loadFunc){
std::cerr << "Error loading symbols from module \"" << "\": " << dlerror() << std::endl;
return;
}
loadFunc(L);
for(;;){}
return 1;
}
module.cpp
#include <lua.hpp>
#include <iostream>
static int Foo(lua_State* L){
std::cout << "Hello World!" << std::endl;
}
extern "C" void RegisterModule(lua_State* L){
lua_pushcfunction(L, Foo);
lua_setglobal(L, "Foo");
}
Makefile
lua = -L /usr/lib/lua5.2 -I /usr/include/lua5.2 -llua
luaHeaders = -I /usr/include/lua5.2
all: main module.so
rm -f main.o
main: main.o
clang++ main.o -o main $(lua) -ldl
main.o: main.cpp
clang++ -c main.cpp $(luaHeaders)
module.so: module.cpp
clang++ -fPIC -shared module.cpp -o module.so $(lua)
My setup is:
Mac OS X 10.9 Mavericks, and Elementary OS Luna
Lua 5.2
Clang
Output from the debugger (lldb)
Process 19943 stopped
* thread #2: tid = 0x23ec1c, 0x0000000100295c31 myModule.so`luaH_newkey + 913, stop reason = EXC_BAD_ACCESS (code=2, address=0x100073db0)
frame #0: 0x0000000100295c31 myModule.so`luaH_newkey + 913
myModule.so`luaH_newkey + 913:
-> 0x100295c31: movq %rax, 16(%r12)
0x100295c36: movl 8(%rbx), %eax
0x100295c39: movl %eax, 24(%r12)
0x100295c3e: testb $64, 8(%rbx)

Why is the mongo C++ driver giving me compilation errors?

I have installed mongo straight from github using
sudo scons --full install
and have the following example source file
#include <cstdlib>
#include <iostream>
#include <mongo/client/dbclient.h>
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
When I run
g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem
-lboost_program_options -lboost_system -o tutorial
I am given the error
In file included from /usr/local/include/mongo/util/net/hostandport.h:21:0,
from /usr/local/include/mongo/util/net/message.h:24,
from /usr/local/include/mongo/client/dbclientinterface.h:30,
from /usr/local/include/mongo/client/connpool.h:23,
from /usr/local/include/mongo/client/dbclient.h:32,
from tutorial.cpp:3:
/usr/local/include/mongo/db/server_options.h:34:51: fatal error:
mongo/util/options_parser/environment.h: No such file or directory
compilation terminated.
I looked into /usr/local/include/mongo/util, but the options_parser folder is not in there.
I had the same error myself, after following the write-up on MongoDB's website. What I ended up doing was copying the headers from the download directory to my include directory. I.e.
sudo cp -R ~/Downloads/mongo-master/src/mongo/util/options_parser /usr/local/include/mongo/util/
Where mongo-master is the name of the extracted directory from MongoDB's GitHub. Hopefully this helps you.