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

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.

Related

What causes the following linking error with the boost c++ libraries?

Hi I get a linking error when compiling my program with the gcc compiler on cygwin. The first picture is a simple sample program from the boost filesystem libraries tutorial page where I have included filesystem.hpp in the boost folder. Beneath that is the picture of my linker error when I try to compile with the following command:
g++ -I C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0 -I C:/Users/Ejer/Desktop/c++Dep/eigen-3.4.0 -L C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0/stage/lib test.cpp -o ser
Here I try to compile my program test.cpp with the eigen and boost libraries and set the includer path that they tell me to set as the path after I have built the library with b2.exe. I have also linked to the lib files for boost. I have also tried linking to the different filesystem lib files specifically. Thanks in advance
#include <iostream>
#include <boost/filesystem.hpp>
using std::cout;
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: tut3 path\n";
return 1;
}
path p (argv[1]);
try
{
if (exists(p))
{
if (is_regular_file(p))
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p))
{
cout << p << " is a directory containing:\n";
for (directory_entry& x : directory_iterator(p))
cout << " " << x.path() << '\n';
}
else
cout << p << " exists, but is not a regular file or directory\n";
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}
I get a linking error when compiling my program
No, you don't. You are getting a linking error when linking your program, not when compiling it.
The reason: you didn't supply the library (-L C:/Users/.... tells the linker where to search for libraries; not which libraries to link). Your command line should look something like:
g++ -I ... -L ... test1.cpp -o ser -lboost_filesystem

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.

Compiling simple C++ SFML file with network code using MinGW throws error "undefined reference to IpAddress"

I keep getting errors when compiling a C++ project using SFML 2 on Windows using MinGW.
At C:\MinGW\include\ there is the SFML header files folder including Network.hpp and I also copied the libsfml-[...].a files into C:\MinGW\lib\ as well as the sfml-[...]-2.dll files into C:\MinGW\bin\.
This is my sample file:
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
int main(int argc, char** argv)
{
sf::TcpListener listener;
listener.listen(4444);
sf::TcpSocket client;
std::cout << "Waiting for client to connect..." << std::endl;
if (listener.accept(client) == sf::Socket::Done)
{
std::cout << "Client connected: " << client.getRemoteAddress() << std::endl;
listener.close();
}
else
{
std::cout << "Client didnt connect" << std::endl;
}
return 0;
}
If I try to compile it using the following command
g++ test.cpp -lsfml-system -lsfml-network
I get these errors:
C:\Users\me\AppData\Local\Temp\cc6XzH1R.o:test.cpp:(.text+0x28): undefined reference to `_imp___ZN2sf9IpAddress3AnyE`
C:\Users\me\AppData\Local\Temp\cc6XzH1R.o:test.cpp:(.text+0x3a): undefined reference to `_imp___ZN2sf11TcpListener6listenEtRKNS_9IpAddressE`
Thanks for any help :D
Matze
Simple , try to link correct version of library.
If you are making x86 app use the 32bit version of library and if you are making x64 app use 64bit version. I also got the same error and this fixed.

C++ cpp-net lib not found

Here is a piece of code which is an example from cpp-netlib
#include <boost/network/protocol/http/server.hpp>
#include <string>
#include <iostream>
namespace http = boost::network::http;
struct hello_world;
typedef http::server<hello_world> server;
struct hello_world {
void operator() (server::request const &request,
server::response &response) {
std::string ip = source(request);
response = server::response::stock_reply(
server::response::ok, std::string("Hello, ") + ip + "!");
}
};
int
main(int argc, char * argv[]) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
return 1;
}
try {
hello_world handler;
server server_(argv[1], argv[2], handler);
server_.run();
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
But on compiling that is g++ main.cpp -o socke.exe -lboost_system I get the following errors
main.cpp:1:50: error: boost/network/protocol/http/server.hpp: No such file or directory
main.cpp:5: error: âboostâ has not been declared
I have installed the cpnet-lib libraries and cmake for build them. I cant get to understand why couldnt the compiler find the libraries.
You didn't specify include path where Boost and cpp-netlib headers are located. The first error line tells which header is missing. Assuming your Boost headers are installed under /a/my_boost (i.e. there is a /a/my_boost/boost subdirectory with headers) and cpp-netlib under /a/my_cpp-netlib, you need to add -I command line options for your compiler:
g++ main.cpp -o socke.exe -I/a/my_boost -I/a/my_cpp-netlib -lboost_system
If you're using a graphical IDE or a build system, there should be an option in the project settings to add include directories.

How to compile C++ mongo project

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