Configuring nginx with g++

Maxim Dounin mdounin at
Thu Nov 11 07:11:30 MSK 2010


On Wed, Nov 10, 2010 at 09:35:43PM -0500, Decimus Phostle wrote:


> > While this particular error probably should be fixed (it's as
> > trivial as including stdio.h into test in auto/types/sizeof) - in
> > general using C++ compiler to compile C code isn't going to
> > produce anything good.  These are two different languages.
> Yes. (That and I got a warning about the format/int type). Trivial as
> it may seem to fix, I was wondering if anyone has pointers on how to
> fix it as the file in which the error occurs (objs/autotest.c) is
> auto-generated.

Well, you got pointer.  But as I already said - even when fixed 
you won't see anything good trying to build nginx with g++ set 
as C compiler.

> >> Q2) The above issue was encountered while trying to write a
> >> handler/module in C++. Is there another way I should be approaching
> >> this?
> >
> > Just create source file with appropriate extension and include
> > nginx header files in an extern "C" block, then write C++ code.
> > See src/misc/ngx_cpp_test_module.cpp for an example (really simple
> > one though).
> I did look at this. [Naive question follows: ] However as the
> modules/handlers get compiled/linked along with the server in nginx, I
> was under the assumption that if I need to write a C++ module/handler
> then I must compile nginx with g++ (instead of cc). (As the usual way
> to compile build the server + modules is "./configure
> --add-module=foobar && make && make install"). Am I missing something
> or is there another way to do this? Please let me know.

As it was already pointed out - you don't want to compile nginx 
with g++, as it's written in C, not C++, while gcc invoked as g++ 
will assume .c files are in C++.

What you want is to compile *your module* with C++ compiler.  This 
is easy (even keeping in mind that nginx's build doesn't 
distinguish between source files and always calls ${CC}): gcc will 
recognize your module is in C++ as long as it's named 
appropriately named[1] (e.g. *.cpp as in case of test module).


The only thing you have to do is to include C++ standard library 
during linking.  This may be done e.g. with --with-ld-opt 
configure argument.

E.g. the following will compile nginx with test cpp module:

./configure --with-ld-opt="-lstdc++" --with-cpp_test_module && make

Maxim Dounin

More information about the nginx mailing list