cppcms can't work - c++

My OS: Mac OSX
My gcc version: 4.2.1
My clang version: llvm 6.1.0
cppcms version: 1.0.5
I download the cppcms framework and install the framework.The commands:
cd cppcms-1.0.5
mkdir build & cd build
cmake ..
make
make test
make install
No error found.
The I write a cpp file named hello.cpp. The program is like this:
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>
using namespace std;
class hello : public cppcms::application{
public:
hello(cppcms::service &srv) : cppcms::application(srv){
}
virtual void main(std::string url);
};
void hello::main(std::string /*url*/)
{
response().out() <<
"<html>\n"
"<body>\n"
" <h1>Hello World</h1>\n"
"</body>\n"
"</html>\n";
}
int main(int argc,char ** argv) {
try {
cppcms::service srv(argc,argv);
srv.applications_pool().mount(
cppcms::applications_factory<hello>()
);
}
catch (std::exception const &e){
std::cerr << e.what() << std::endl;
}
return 0;
}
My config.js:
{
"service" : {
"api" : "http",
"port" : 8008
},
"http" : {
"script_names" : [ "/hello" ]
}
}
Compile commands:
c++ hello.cpp -lcppcms -o hello
./hello -c config.js
I visited the url "http://localhost:8008/hello",then web browser show me
"This webpage is not available".
What's wrong? How to fix the problem.

Most importantly you have to start your service after mounting it:
cppcms::service srv(argc,argv);
srv.applications_pool().mount(
cppcms::applications_factory<hello>()
);
srv.run();
I would also include the following two header files (at least on Linux
I got a compilation error otherwise):
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>

Related

fatal error: 'grpcpp/grpcpp.h' file not found C++

Context
I have been working with C++ for about the past 5-6 months and I'm beginning to learn gRPC. I have followed many tutorials online to get started, but I want to build a client-server communication app from scratch. Probably a bit too much, but I'm doing my best to understand how to get it all to work from the ground up rather than downloading, typing 'make', and then having a working product that I don't know how to implement into my own projects.
Goal: Create and run a simple C++ gRPC client-server communication
Versions
Using VSCode IDE.
Protoc = libprotoc 3.17.3
gRPC = 1.41.1
make = 3.81
Files
mathtest.proto
syntax = "proto3";
option java_package = "ex.grpc";
package mathtest;
// Defines the service
service MathTest {
// Function invoked to send the request
rpc sendRequest (MathRequest) returns (MathReply) {}
}
// The request message containing requested numbers
message MathRequest {
int32 a = 1;
int32 b = 2;
}
// The response message containing response
message MathReply {
int32 result = 1;
}
server.cpp
#include <string>
#include <grpcpp/grpcpp.h>
#include "mathtest.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using mathtest::MathTest;
using mathtest::MathRequest;
using mathtest::MathReply;
class MathServiceImplementation final : public MathTest::Service {
Status sendRequest(
ServerContext* context,
const MathRequest* request,
MathReply* reply
) override {
int a = request->a();
int b = request->b();
reply->set_result(a * b);
return Status::OK;
}
};
void Run() {
std::string address("0.0.0.0:5000");
MathServiceImplementation service;
ServerBuilder builder;
builder.AddListeningPort(address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on port: " << address << std::endl;
server->Wait();
}
int main(int argc, char** argv) {
Run();
return 0;
}
client.cpp
#include <string>
#include <grpcpp/grpcpp.h>
#include "mathtest.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using mathtest::MathTest;
using mathtest::MathRequest;
using mathtest::MathReply;
class MathTestClient {
public:
MathTestClient(std::shared_ptr<Channel> channel) : stub_(MathTest::NewStub(channel)) {}
int sendRequest(int a, int b) {
MathRequest request;
request.set_a(a);
request.set_b(b);
MathReply reply;
ClientContext context;
Status status = stub_->sendRequest(&context, request, &reply);
if(status.ok()){
return reply.result();
} else {
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
return -1;
}
}
private:
std::unique_ptr<MathTest::Stub> stub_;
};
void Run() {
std::string address("0.0.0.0:5000");
MathTestClient client(
grpc::CreateChannel(
address,
grpc::InsecureChannelCredentials()
)
);
int response;
int a = 5;
int b = 10;
response = client.sendRequest(a, b);
std::cout << "Answer received: " << a << " * " << b << " = " << response << std::endl;
}
int main(int argc, char* argv[]){
Run();
return 0;
}
Steps taken for compilation
Use mathtest.proto to create the necessary files via 'protoc' (or protobuf) by executing these: protoc --grpc_out=. --plugin=protoc-gen-grpc=/opt/homebrew/bin/grpc_cpp_plugin mathtest.proto & protoc --cpp_out=. mathtest.proto
This creates the following files:
mathtest.pb.h
mathtest.pb.cc
mathtest.grpc.pb.h
mathtest.grpc.pb.cc
Compile client.cpp & server.cpp files to create executable binaries using these commands: g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client 'pkg-config --libs protobuf grpc++' (NOTE: in this post, I use a single quote in the command line, but in the actual command I use a backtick; just wanted to make that clear)
Errors
As you may notice, I can't get to compiling the server because I can't get past the client compilation first. After executing the above command in step 2 of compilation, this is my output:
g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client `pkg-config --libs protobuf grpc++`
client.cpp:4:10: fatal error: 'grpcpp/grpcpp.h' file not found
#include <grpcpp/grpcpp.h>
^~~~~~~~~~~~~~~~~
1 error generated.
In file included from mathtest.pb.cc:4:
./mathtest.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
In file included from mathtest.grpc.pb.cc:5:
./mathtest.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [client] Error 1
Here's my real confusion...
C++ intellisense has no issues finding these files. My $PATH variables point to these folders, and my VS Code include path also point to these folders. I'm unsure where I am going wrong here...
echo $PATH returns this:
/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/include:/opt/homebrew/Cellar:/opt/homebrew/opt/libtool/libexec/gnubin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/tzeller/.local/bin
The folders in question ('google' & 'grcpp') live within /opt/homebrew/include and they hold the necessary files as well...
What am I missing??
Change your compile command to
g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client `pkg-config --libs --cflags protobuf grpc++`
The --cflags bit asks pkg-config to spit out the necessary parameters for setting the header search path (on my system -I/opt/homebrew/Cellar/grpc/1.41.1/include and others)

fatal error: Wt/WApplication.h: No such file or directory

I'm following along with this tutorial for Webtoolkit: https://www.webtoolkit.eu/wt/doc/tutorial/wt.html
I am running all this on a Rasbian/Debian on a Virtual Machine and I am using C++14
I decided to copy paste the hello.cpp code onto my Desktop and am compiling it like this on my terminal as I was facing linking errors and thus followed along with command line examples in the tutorial:
g++ -std=c++14 -o hello hello.cpp -I/usr/include -L/usr/lib
However I still get:
hello.cpp:1:29: fatal error: Wt/WApplication.h: No such file or directory
#include <Wt/WApplication.h>
My Wt files are located in /usr/include and /usr/lib which is why I used them.
This stackoverflow did not solve my issue: How to Install Wt into a Custom Folder Without "fatal error: Wt/WApplication: No such file or directory"
EDIT: I was able to run the example files located in the Wt folders in /usr/lib/Wt/examples but can't run it on Desktop, I followed the command line examples on the tutorial for linking errors
EDIT 2: The cpp code if it helps, same as tutorial, just copy pasted:
#include <Wt/WApplication.h>
#include <Wt/WBreak.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WText.h>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
setTitle("Hello world");
root()->addWidget(std::make_unique<Wt::WText>("Your name, please? "));
nameEdit_ = root()->addWidget(std::make_unique<Wt::WLineEdit>());
Wt::WPushButton *button = root()->addWidget(std::make_unique<Wt::WPushButton>("Greet me."));
root()->addWidget(std::make_unique<Wt::WBreak>());
greeting_ = root()->addWidget(std::make_unique<Wt::WText>());
auto greet = [this]{
greeting_->setText("Hello there, " + nameEdit_->text());
};
button->clicked().connect(greet);
}
int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, [](const Wt::WEnvironment& env) {
return std::make_unique<HelloApplication>(env);
});
}
The Wt include files do not have the .h extension on some OSs.
Try #include <Wt/WApplication>
root#08c7a05c8129:/usr/lib/Wt/examples/hello# ls /usr/include/Wt
Auth WBatchEditProxyModel WDateValidator WHTML5Audio WLocalizedStrings WPopupWidget WSslInfo WTimePicker
Chart WBoostAny WDefaultLayout WHTML5Video WLogger WProgressBar WStackedWidget WTimeValidator
Dbo WBootstrapTheme WDefaultLoadingIndicator WIOService WMatrix4x4 WPushButton WStandardItem WTimer
Ext WBorder WDialog WIcon WMeasurePaintDevice WRadioButton WStandardItemModel WTimerWidget
Http WBorderLayout WDllDefs.h WIconPair WMediaPlayer WRandom WStatelessSlot WToolBar
Json WBoxLayout WDoubleSpinBox WIdentityProxyModel WMemoryResource WRasterImage WStreamResource WTransform
Mail WBreak WDoubleValidator WImage WMenu WReadOnlyProxyModel WString WTree
Payment WBrush WEnvironment WInPlaceEdit WMenuItem WRectArea WStringListModel WTreeNode
Render WButtonGroup WEvent WIntValidator WMessageBox WRectF WStringStream WTreeTable
Utils WCalendar WException WInteractWidget WMessageResourceBundle WRegExp WStringUtil WTreeTableNode
WAbstractArea WCanvasPaintDevice WFileResource WItemDelegate WMessageResources WRegExpValidator WSubMenuItem WTreeView
WAbstractGLImplementation WCheckBox WFileUpload WItemSelectionModel WModelIndex WResource WSuggestionPopup WVBoxLayout
WAbstractItemDelegate WCircleArea WFitLayout WJavaScript WNavigationBar WScrollArea WSvgImage WValidationStatus
WAbstractItemModel WClientGLWidget WFlags WJavaScriptPreamble WObject WScrollBar WTabWidget WValidator
WAbstractItemView WColor WFlashObject WJavaScriptSlot WOverlayLoadingIndicator WSelectionBox WTable WVector3
WAbstractListModel WCombinedLocalizedStrings WFont WLabel WPaintDevice WServer WTableCell WVector4
WAbstractMedia WComboBox WFontMetrics WLayout WPaintedWidget WServerGLWidget WTableColumn WVectorImage
WAbstractProxyModel WCompositeWidget WFormModel WLayoutItem WPainter WShadow WTableRow WVideo
WAbstractSpinBox WConfig.h WFormWidget WLayoutItemImpl WPainterPath WSignal WTableView WViewWidget
WAbstractTableModel WContainerWidget WGLWidget WLength WPanel WSignalMapper WTemplate WVirtualImage
WAbstractToggleButton WCssDecorationStyle WGenericMatrix WLengthValidator WPdfImage WSlider WTemplateFormView WVmlImage
WAccordionLayout WCssStyleSheet WGlobal WLineEdit WPen WSocketNotifier WText WWebWidget
WAggregateProxyModel WCssTheme WGoogleMap WLineF WPoint WSortFilterProxyModel WTextArea WWidget
WAnchor WDate WGradient WLink WPointF WSound WTextEdit WWidgetItem
WAnimation WDateEdit WGridLayout WLoadingIndicator WPolygonArea WSpinBox WTheme
WApplication WDatePicker WGroupBox WLocalDateTime WPopupMenu WSplitButton WTime
WAudio WDateTime WHBoxLayout WLocale WPopupMenuItem WSslCertificate WTimeEdit
so use without extension:
#include <Wt/WApplication>
Try :
g++ -std=c++14 -o hello hello.cpp -I/usr/local/include/Wt -L/usr/local/lib64/
OR
g++ -std=c++14 -o hello hello.cpp -I/usr/local/include/Wt -L/usr/local/lib/

mongodb c++ driver- error: "mongo::client" has not been declared

-compiled and installed successfully mongo-cxx-driver (mongo db c++ driver - 26Compat - all test ok passed). directory /usr, so /usrmongo/client/dbclient.h exists.
-running cmd:
g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_system -lboost_regex -lboost_filesystem -lboost_program_options -o tutorial
-file tutorial.cpp
#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
mongo::client::initialize();
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
results - error:
tutorial.cpp: In function ‘int main()’:
tutorial.cpp:11:12: error: ‘mongo::client’ has not been declared
any hint?
Not sure this helps but I got a similar error after installing the mongo-dev package using apt-get. This should not be done after mongo 2.6; it only works up to mongo 2.4 or something. It ended up corrupting my 2.6, so I had to clean up everything, reinstall mongo and then build mongo-cxx-driver from the github repo https://github.com/mongodb/mongo-cxx-driver according to their instructions.
Afterward eclipse still gave an error for the tutorial, but strangely it did build the thing. I had to clean up both Debug and Release there and ended up with only a warning, because the includes were messed up. So finally I just scrapped the eclipse project, copied the tutorial file to a new project and now it builds clean.

How to launch a Wt Client?

I have just started a development using wt(with c++ binding). And i have could done till now, is reading a very few documentation and a little no of sample programs(written in c++ and wt).
After that I installed wt on my machine amd tried to run one one of those demo programs.
hello.cc
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
setTitle("Hello world");
root()->addWidget(new Wt::WText("Your name, please ? "));
nameEdit_ = new Wt::WLineEdit(root());
Wt::WPushButton *button = new Wt::WPushButton("Greet me.", root());
root()->addWidget(new Wt::WBreak());
greeting_ = new Wt::WText(root());
button->clicked().connect(this, &HelloApplication::greet);
}
void HelloApplication::greet()
{
greeting_->setText("Hello there, " + nameEdit_->text());
}
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
return new HelloApplication(env);
}
int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, &createApplication);
}
I complied this code
g++ -o hello hello.cc -lwthttp -lwt
It was compiled successfully.Then I could run this server application successfully to run it on localhost
[manmatha#manmatha Lab]$ su
Password:
[root#manmatha Lab]# ./hello --docroot . --http-address 127.0.0.1 --http-port 9090
[2013-Jun-14 13:58:08.585419] 5066-[info] "WServer/wthttp:initializing built-in wthttpd"
[2013-Jun-14 13:58:08.590955] 5066-[info] "wthttp:started server: http://127.0.0.1:9090"
problem is when I type
localhost::9090
on the address bar of the internet browser on local machine., anything does not show up.
In this context , my specific question is how to start a wt client??
Thanx in advannce
try 127.0.0.1:9090
You specified 127.0.0.1 on the command line so type it in the browser's address bar.
This is a specific of the Wt embedded http server.
You have to mention the --deploy-path variable in your command line arguments. Try this
--http-address 127.0.0.1 --http-port 9090 --deploy-path=/hello --docroot=.
In the browser type http://localhost:9090/hello

include mysql into cmake

I'm writing a console application in kdevelop (integrated with cmake) in which I want to connect to mysql. I have installed libmysqlclient16-dev. My main.cpp file looks like this:
#include <stdlib.h>
#include <iostream>
#include <mysql/mysql.h>
int main(int argc, char **argv) {
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
std::cout << "mysql init failed\n";
exit(1);
}
conn_ptr = mysql_real_connect (conn_ptr, "localhost", "user", "pass", "db", 0, NULL, 0);
if (conn_ptr) {
std::cout << "connection success\n";
} else {
std::cout << "connection failed\n";
}
mysql_close(conn_ptr);
return 0;
}
and it compiles and works correctly, when I compile it manually:
g++ main.cpp -lmysqlclient -o main
But I want to include it into cmake somehow. The CMakeLists.txt, generated by kdevelop, looks like the following:
project(finances)
add_executable(finances main.cpp)
What should I add to cmake to make it include mysqlclient library?
target_link_libraries(finances mysqlclient)
Seems to work.
target_link_libraries(projectName mysqlclient)
Change projectName with your current project name