Is it possible to use C++ in handlers?

332 views
Skip to first unread message

Tolga Hoşgör

unread,
Jul 1, 2013, 9:38:19 AM7/1/13
to nx...@googlegroups.com
Is it possible to use C++ in handlers? Also, maybe in modules also?

If yes, how can I do it? where do I need to put 'extern "C"' and such exactly?

Thanks,
Tolga.

Tolga Hoşgör

unread,
Jul 1, 2013, 9:42:28 AM7/1/13
to nx...@googlegroups.com
Also, how to compile it? I am not good with makefiles. I have modified Makefile.am under src/bin and managed to get my handlers to work by removing the .o file and calling 'make' on each change but there is no option about gcc | g++ in that file.

I would prefer modifying the makefile under src/bin to use g++ but I am okay if there a way for me to just manually compile my handler with g++ and generate a nxweb executable? I will write a short script for it and go with that.

Yaroslav

unread,
Jul 1, 2013, 10:16:25 AM7/1/13
to nx...@googlegroups.com
There is "extern C" already in place in header files. You can build nxweb library and link to it as you usually link froim C++ to C. I have no experience with this myself but somebody here might help, I think.


--
You received this message because you are subscribed to the Google Groups "nxweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nxweb+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted

Art Golf

unread,
Nov 9, 2013, 8:57:20 PM11/9/13
to nx...@googlegroups.com

1. edit ./nxweb-3.2.0-dev/src/bin/Makefile.am:

nxweb_SOURCES = main.c modules/mymodule1.c modules/mymodule2.cpp

nxweb_LDADD = $(top_srcdir)/src/lib/.libs/libnxweb.a $(NXWEB_EXT_LIBS)


2. edit ./nxweb-3.2.0-dev/configure.ac,add one line:

AC_PROG_CC([gcc])

AC_PROG_LIBTOOL

AC_PROG_CXX([g++])


3. build all:

# aclocal

# autoconf

# automake --add-missing

# ./configure

# make

# autoreconf -ivf(if make report that lib tool not match,run this command)

# make

# make install


Cheers

kaga...@gmail.com

unread,
Jan 24, 2016, 9:24:20 PM1/24/16
to nxweb
Based on hello.c, I have accomplished this as follows:

1. Add #include <stdbool.h> before the #include "nxweb/nxweb.h" in your c++ file

This solves the c++ errors about the _Bool type, which you get otherwise.

2. Put the extern "C" { AFTER any includes, which use templates. E.g. <string> . Close the curly brace at the end of the c++ file.

3. Use explicit type conversion for nxweb_handler_flags in the NXWEB_DEFINE_HANDLER

Here is a complete example.

#include <string>

extern "C" {

#include <stdbool.h>
#include "nxweb/nxweb.h"

static nxweb_result main_on_request(nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) {

  nxweb_set_response_content_type
(resp, "text/html");
  std
::string someText = "Hello, world";
 
const char* page_content = someText.c_str();
  nxweb_response_printf
(resp, "%H", page_content);
 
return NXWEB_OK;
}
NXWEB_DEFINE_HANDLER
(main, .on_request=main_on_request,
   
.flags = (nxweb_handler_flags) (NXWEB_HANDLE_ANY|NXWEB_PARSE_PARAMETERS|NXWEB_PARSE_COOKIES));

}

Save this file as main.cpp
Compile it on at the teminal with using clang version 3.6

clang++ -shared -fPIC main.cpp -o main.so

g++ version 4.8 gives me an error, which I don't understand. Maybe someone else can help here. So I stick to clang++

Replace hello.c with main.cpp in config.json.
Restart nxweb
Done.

Yaroslav

unread,
Jan 25, 2016, 2:54:19 AM1/25/16
to nxweb
Hi! Thanks for sharing this. Could you please copy the error message g++ gives.

I am not sure about clang as nxweb uses some gcc extensions (perhaps clang implements them too). Does your handler actually work?

For more options, visit https://groups.google.com/d/optout.

kaga...@gmail.com

unread,
Jan 25, 2016, 4:59:37 PM1/25/16
to nxweb
Hi Yaroslaw,

I believe (not 100% sure) it doesn't matter that you and me use different compilers, because I tested this only as a shared library. In other words, I have not compiled it "into" nxweb

When I use g++ version 4.8, without taking care of the type "_Bool" I get the following error message:

In file included from /usr/local/include/nxweb/nx_buffer.h:32:0,
                 
from /usr/local/include/nxweb/nxweb.h:39,
                 
from main.cpp:9:
/usr/local/include/nxweb/misc.h:56:3: error: _Bool does not name a type
   _Bool error_log_level_set
:1;
   
^


I have googled about this error and found that _Bool is a reserved keyword in C99, but it is unknown to C++.

Cheers,

Kagan
Message has been deleted

kaga...@gmail.com

unread,
Feb 15, 2016, 4:23:59 PM2/15/16
to nxweb
Hi Yaroslaw,


> Hi! Thanks for sharing this. Could you please copy the error message g++ gives

Sorry, I misunderstood your question. Please find below the error messages
I get from g++ 4.8.4 under Ubuntu 14.04.1.


In file included from /usr/local/include/nxweb/nxweb.h:229:0,
 from main.cpp:6:
main.cpp:16:1: sorry, unimplemented: non-trivial designated initializers not
supported
 NXWEB_DEFINE_HANDLER(main, .on_request=main_on_request,
 ^
main.cpp:16:1: sorry, unimplemented: non-trivial designated initializers not
supported

Yaroslav

unread,
Feb 15, 2016, 11:55:50 PM2/15/16
to nxweb
This message basically means that NXWEB_DEFINE_HANDLER macro does not work, so the handler cannot be defined.

I suggest you put NXWEB_DEFINE_HANDLER and C code in separate compilation unit (file) and compile it with gcc. Then link to C++ implementation compiled from other file(s).

On Tue, Feb 16, 2016 at 12:11 AM, <kaga...@gmail.com> wrote:
Hi Yaroslaw,


> I am not sure about clang as nxweb uses some gcc extensions (perhaps clang implements them too). Does your handler actually work?

Sorry, I misunderstood your question. Please find below the error messages I get from g++ 4.8.4 under Ubuntu 14.04.1.


In file included from /usr/local/include/nxweb/nxweb.h:229:0,
 
from main.cpp:6:
main
.cpp:16:1: sorry, unimplemented: non-trivial designated initializers not supported
 NXWEB_DEFINE_HANDLER
(main, .on_request=main_on_request,
 
^
main
.cpp:16:1: sorry, unimplemented: non-trivial designated initializers not supported


kaga...@gmail.com

unread,
Mar 2, 2016, 4:50:32 PM3/2/16
to nxweb
Since I am quite happy with clang++, I didn't try to make that work with g++.

But, I have created new tool and published at github, which creates C++ modules for nxwb from HTML Templates. It is different than the nxweb templates in many ways and serves a different purpose.

If you are interested in having a look or spreading the word, please find it here:

https://github.com/kkayal/ktemplate

I am developing this to build https://milonga.tips. (Currently there is not much to see...)

Reply all
Reply to author
Forward
0 new messages