C++ cpp-net lib not found - c++

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.

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

luaL_dofile fails on known-good bytecode, works with uncompiled version

I've put together a very simple Lua engine but it seems to reject bytecode which works in the lua console. The uncompiled version works in the engine. Am I using luac wrong somehow?
I compile using the given command and run as './a.out'.
res/default.lua:
print("Setting up world structure.")
luac command:
luac -o res/default.lux res/default.lua
MWE:
#define SCRIPTDIR "res/"
#define THROW_IF_NONZERO(x,m) if((x)!=0) throw std::runtime_error(m);
#define THROW_IF_ZERO(x,m) if((x)==0) throw std::runtime_error(m);
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include "sys/stat.h"
#include <string>
#include <system_error>
#include <iostream>
using std::string;
class Entity {
private:
lua_State *m_lua;
public:
Entity() : Entity(nullptr) { }
Entity(lua_State *lua) : m_lua{lua} { }
virtual ~Entity() { }
void load_and_run(string);
};
class WorldEntity : public Entity {
public:
WorldEntity(lua_State *lua) : Entity(lua) {
luaL_openlibs(lua);
}
~WorldEntity() { }
};
int main() {
lua_State *lua{nullptr};
try {
lua = luaL_newstate();
WorldEntity eWorld{lua};
eWorld.load_and_run("default"); // load default.lua/lux
} catch(std::exception &e) {
if (lua != nullptr) {
lua_close(lua);
}
std::cout << "Error: " << e.what() << std::endl;
}
return 0;
}
void Entity::load_and_run(string filename) {
THROW_IF_ZERO(m_lua, "Lua not started.");
filename = SCRIPTDIR + filename + ".lux";
struct stat sb;
int rc = stat(filename.c_str(), &sb);
if (rc == -1) {
filename.pop_back();
filename += "a";
rc = stat(filename.c_str(), &sb);
THROW_IF_NONZERO(rc, "File not found!");
}
std::cout << "File: " << filename << std::endl;
// Currently won't run compiled Lua scripts, not sure why.
rc = luaL_dofile(m_lua, filename.c_str());
THROW_IF_NONZERO(rc, "Could not load lua file.");
}
compile command:
gcc src/bug001mwe.cpp -std=c++14 -llua -lstdc++
correct output from script:
File: res/default.lua
Setting up world structure.
wrong output from bytecode:
File: res/default.lux
Error: Could not load lua file.
both files, output from lua console:
Setting up world structure.
What confused me was that it worked in the lua console but not in my program. I added a call to lua_tostring after the call to luaL_dofile, like this:
rc = luaL_dofile(m_lua, filename.c_str());
std::ostringstream ostr;
ostr << "Could not load lua file. ";
ostr << lua_tostring(m_lua, -1);
THROW_IF_NONZERO(rc, ostr.str());
The error string became:
Error: Could not load lua file. res/default.lux: version mismatch in precompiled chunk
What the heck?
Long story short, I had a previous version of Lua installed due to out of date package dependencies in some unrelated stuff. The older luac was intercepting the luac command and compiling to valid but incompatible bytecode. Uninstalled the unrelated packages which I didn't really need, and now everything works.
Moral of the story: always check for an error string on the Lua stack, it will (probably) tell you what's wrong.

MySQL C++ connector fails without an error message

I'm having trouble with the simple task of creating a connection to a MySQL database in C++. No errors are printed except for the program return value, which is -1.
I'm using Eclipse Oxygen as the IDE, and the OS is Ubuntu 16.04. The MySQL connector version is 1.1.9., and it's the generic Linux version (glibc 2.12).
The connector files reside in my home folder. I've added the directory path of the "include" folder to Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Includes. I've also added the "lib" path and "mysqlcppconn" library name to Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Libraries and Library search path, respectively. Finally, I've added the "lib" path into the LD_LIBRARY_PATH environment variable in Properties -> Run/Debug Settings -> (Launch configuration) -> Environment.
DBManager.cpp:
DBManager::DBManager() {
try {
std::cout << "Getting driver instance" << std:endl;
driver = sql::mysql::get_mysql_driver_instance();
std::cout << "Creating connection" << std::endl;
connection = driver->connect("tcp://127.0.0.1:3306", "username", "password");
std::cout << "Connected!" << std::endl; // NOT REACHING THIS LINE
} catch(const SQLException& ex) {
// these lines are not printed
std::cout << "Exception!" << std::endl;
std::cout << ex.what() << std::endl;
exit(1);
}
}
DBManager::~DBManager() {
std::cout << "Deleting connection" << std::endl;
delete connection;
}
DBManager.h
#ifndef DBMANAGER_H_
#define DBMANAGER_H_
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/exception.h>
class DBManager {
public:
DBManager();
virtual ~DBManager();
private:
sql::mysql::MySQL_Driver* driver;
sql::Connection* connection;
};
#endif
main.cpp
#include <iostream>
#include "DBManager.h"
int main(int argc, char* argv[]) {
std::cout << "Starting" << std::endl;
DBManager* dbManager = new DBManager();
// ... program logic not reached
delete dbManager; // not reached
return 0;
}
I finally got it to work, I was using the generic Linux binaries first, which did not work. When I switched to using the binaries for Ubuntu, the program started to work.

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.

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.