I have downloaded and compiled poco 1.8.1 on win7 with mingw32 in qtcreator with cmake, using these options:
ENABLE_DATA, ENABLE_JSON, ENABLE_NET, ENABLE_UTIL, POCO_STATIC
Then it generated to me these files:
libPocoFoundationd.a, libPocoJSONd.a, libPocoNetd.a, libPocoUtild.a
Then, I want to use it in executable project. This is how its .pro file is:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
QMAKE_CXXFLAGS += -std=c++0x
LIBS += -L"$$PWD/../../../poco/build/Debug/lib" -lPocoUtild -lPocoJSONd -lPocoNetd -lPocoFoundationd
INCLUDEPATH += $$PWD/../../../poco/build/Debug
DEPENDPATH += $$PWD/../../../poco/build/Debug
INCLUDEPATH += $$PWD/../../../poco/net/include
INCLUDEPATH += $$PWD/../../../poco/foundation/include
INCLUDEPATH += $$PWD/../../../poco/json/include
INCLUDEPATH += $$PWD/../../../poco/util/include
INCLUDEPATH += $$PWD/''
DEPENDPATH += $$PWD/''
I want to compile an example:
#include <iostream>
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Util/ServerApplication.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Format.h"
#include <iostream>
using Poco::Net::ServerSocket;
using Poco::Net::WebSocket;
using Poco::Net::WebSocketException;
using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPRequestHandlerFactory;
using Poco::Net::HTTPServer;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPServerResponse;
using Poco::Net::HTTPServerParams;
using Poco::Timestamp;
using Poco::ThreadPool;
using Poco::Util::ServerApplication;
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using namespace std;
class PageRequestHandler: public HTTPRequestHandler
/// Return a HTML document with some JavaScript creating
/// a WebSocket connection.
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr << "<html>";
ostr << "<head>";
ostr << "<title>WebSocketServer</title>";
ostr << "<script type=\"text/javascript\">";
ostr << "function WebSocketTest()";
ostr << "{";
ostr << " if (\"WebSocket\" in window)";
ostr << " {";
ostr << " var ws = new WebSocket(\"ws://" << request.serverAddress().toString() << "/ws\");";
ostr << " ws.onopen = function()";
ostr << " {";
ostr << " ws.send(\"Hello, world!\");";
ostr << " };";
ostr << " ws.onmessage = function(evt)";
ostr << " { ";
ostr << " var msg = evt.data;";
ostr << " alert(\"Message received: \" + msg);";
ostr << " ws.close();";
ostr << " };";
ostr << " ws.onclose = function()";
ostr << " { ";
ostr << " alert(\"WebSocket closed.\");";
ostr << " };";
ostr << " }";
ostr << " else";
ostr << " {";
ostr << " alert(\"This browser does not support WebSockets.\");";
ostr << " }";
ostr << "}";
ostr << "</script>";
ostr << "</head>";
ostr << "<body>";
ostr << " <h1>WebSocket Server</h1>";
ostr << " <p><a href=\"javascript:WebSocketTest()\">Run WebSocket Script</a></p>";
ostr << "</body>";
ostr << "</html>";
}
};
class WebSocketRequestHandler: public HTTPRequestHandler
/// Handle a WebSocket connection.
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
try
{
WebSocket ws(request, response);
app.logger().information("WebSocket connection established.");
char buffer[1024];
int flags;
int n;
do
{
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
app.logger().information(Poco::format("Frame received (length=%d, flags=0x%x).", n, unsigned(flags)));
ws.sendFrame(buffer, n, flags);
}
while (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
app.logger().information("WebSocket connection closed.");
}
catch (WebSocketException& exc)
{
app.logger().log(exc);
switch (exc.code())
{
case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
// fallthrough
case WebSocket::WS_ERR_NO_HANDSHAKE:
case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
response.send();
break;
}
}
}
};
class RequestHandlerFactory: public HTTPRequestHandlerFactory
{
public:
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
Application& app = Application::instance();
app.logger().information("Request from "
+ request.clientAddress().toString()
+ ": "
+ request.getMethod()
+ " "
+ request.getURI()
+ " "
+ request.getVersion());
for (HTTPServerRequest::ConstIterator it = request.begin(); it != request.end(); ++it)
{
app.logger().information(it->first + ": " + it->second);
}
if(request.find("Upgrade") != request.end() && Poco::icompare(request["Upgrade"], "websocket") == 0)
return new WebSocketRequestHandler;
else
return new PageRequestHandler;
}
};
class WebSocketServer: public Poco::Util::ServerApplication
/// The main application class.
///
/// This class handles command-line arguments and
/// configuration files.
/// Start the WebSocketServer executable with the help
/// option (/help on Windows, --help on Unix) for
/// the available command line options.
///
/// To use the sample configuration file (WebSocketServer.properties),
/// copy the file to the directory where the WebSocketServer executable
/// resides. If you start the debug version of the WebSocketServer
/// (WebSocketServerd[.exe]), you must also create a copy of the configuration
/// file named WebSocketServerd.properties. In the configuration file, you
/// can specify the port on which the server is listening (default
/// 9980) and the format of the date/time string sent back to the client.
///
/// To test the WebSocketServer you can use any web browser (http://localhost:9980/).
{
public:
WebSocketServer(): _helpRequested(false)
{
}
~WebSocketServer()
{
}
protected:
void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
ServerApplication::initialize(self);
}
void uninitialize()
{
ServerApplication::uninitialize();
}
void defineOptions(OptionSet& options)
{
ServerApplication::defineOptions(options);
options.addOption(
Option("help", "h", "display help information on command line arguments")
.required(false)
.repeatable(false));
}
void handleOption(const std::string& name, const std::string& value)
{
ServerApplication::handleOption(name, value);
if (name == "help")
_helpRequested = true;
}
void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("A sample HTTP server supporting the WebSocket protocol.");
helpFormatter.format(std::cout);
}
int main(const std::vector<std::string>& args)
{
if (_helpRequested)
{
displayHelp();
}
else
{
// get parameters from configuration file
unsigned short port = (unsigned short) config().getInt("WebSocketServer.port", 9980);
// set-up a server socket
ServerSocket svs(port);
// set-up a HTTPServer instance
HTTPServer srv(new RequestHandlerFactory, svs, new HTTPServerParams);
// start the HTTPServer
srv.start();
// wait for CTRL-C or kill
waitForTerminationRequest();
// Stop the HTTPServer
srv.stop();
}
return Application::EXIT_OK;
}
private:
bool _helpRequested;
};
POCO_SERVER_MAIN(WebSocketServer)
But of course, nothing works. I got undefined references. Actually, every thing is undefined:
Timestamp.h:256:28: warning: 'static Poco::Timestamp::TimeDiff Poco::Timestamp::resolution()' redeclared without dllimport attribute after being referenced with dll linkage
inline Timestamp::TimeDiff Timestamp::resolution()
^ ^
g++ -Wl,-subsystem,console -mthreads -o debug\use_poco.exe debug/main.o - -lPocoUtild -lPocoJSONd -lPocoNetd -lPocoFoundationd
debug/main.o: In function `wmain':
use_poco/main.cpp:260: undefined reference to `_imp___ZN4Poco4Util17ServerApplication3runEiPPw'
use_poco/main.cpp:260: undefined reference to `_imp___ZNK4Poco9Exception11displayTextEv'
debug/main.o: In function `ZN4Poco6Logger3logERKSsNS_7Message8PriorityE':
Poco/Logger.h:671: undefined reference to `_imp___ZN4Poco7MessageC1ERKSsS2_NS0_8PriorityE'
debug/main.o: In function `ZN4Poco4Util11Application8instanceEv':
Poco/Util/Application.h:453: undefined reference to `_imp___ZN4Poco4Util11Application10_pInstanceE'
Poco/Util/Application.h:454: undefined reference to `_imp___ZN4Poco4Util11Application10_pInstanceE'
debug/main.o: In function `ZN18PageRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE':
use_poco/main.cpp:49: undefined reference to `_imp___ZN4Poco3Net11HTTPMessage14setContentTypeERKSs'
use_poco/main.cpp:59: undefined reference to `_imp___ZNK4Poco3Net13SocketAddress8toStringEv'
debug/main.o: In function `ZN23WebSocketRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE':
...
...
poco/Util/src/Application.cpp:82: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
poco/Util/src/Application.cpp:83: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
poco\build\Debug\lib/libPocoUtild.a(Application.cpp.obj): In function `ZN4Poco4Util11ApplicationD2Ev':
...
...
I looked this, this, this, and this with no result.
How could I use poco?