# Configuring nginx with g++

**URL:** <https://www.ruby-forum.com/t/configuring-nginx-with-g/198940>\
**Category:** NGINX\
**Created:** [November 11, 2010, 1:22am UTC](https://www.ruby-forum.com/t/configuring-nginx-with-g/198940 "2010-11-11T01:22:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [November 11, 2010, 1:22am UTC](https://www.ruby-forum.com/t/configuring-nginx-with-g/198940/1 "2010-11-11T01:22:59Z")

</div>

Hello,

Q1) I have been trying to compile nginx using the C++ compiler as  
follows:

$ ./configure --with-cc=g++ --add-module=/path/to/module  
–prefix=/some/prefix

(Also, tried “CC=g++ ./configure …”).

Both these commands result in this error:

…  
creating objs/Makefile  
checking for int size …  
./configure: error: can not detect int size  
$

On tail’ing the objs/autoconf.err, the error is obvious(details  
below). How do I go about fixing this? And/or is this fixed in a later  
release? (I am using 0.8.52). Please let me know.

Q2) The above issue was encountered while trying to write a  
handler/module in C++. Is there another way I should be approaching  
this?

Please let me know. Any help would be much appreciated.

Thanks,  
DP

* * *

PS:

Details of int size error:

## $ tail -23 objs/autoconf.err

checking for int size

## objs/autotest.c: In function ‘int main()’: objs/autotest.c:11: error: ‘printf’ was not declared in this scope

#include \<sys/types.h\>  
#include \<sys/time.h\>  
#include \<unistd.h\>  
#include \<signal.h\>  
#include \<sys/resource.h\>  
#include \<inttypes.h\>

int main() {  
printf("%d", sizeof(int));  
return 0;  
}

---

<div class="post-metadata">

**Author:** ![Decimus\_Phostle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@Decimus\_Phostle](https://www.ruby-forum.com/u/Decimus_Phostle)\
**Post date:** [November 11, 2010, 2:44am UTC](https://www.ruby-forum.com/t/configuring-nginx-with-g/198940/2 "2010-11-11T02:44:58Z")

</div>

Hello!

On Wed, Nov 10, 2010 at 06:59:44PM -0500, Decimus Phostle wrote:

> …  
> creating objs/Makefile  
> checking for int size …  
> ./configure: error: can not detect int size  
> $
> 
> On tail’ing the objs/autoconf.err, the error is obvious(details  
> below). How do I go about fixing this? And/or is this fixed in a later  
> release? (I am using 0.8.52). Please let me know.

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.

> 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).

Maxim D.

---

<div class="post-metadata">

**Author:** ![Decimus\_Phostle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@Decimus\_Phostle](https://www.ruby-forum.com/u/Decimus_Phostle)\
**Post date:** [November 11, 2010, 5:11am UTC](https://www.ruby-forum.com/t/configuring-nginx-with-g/198940/3 "2010-11-11T05:11:55Z")

</div>

Hi,

> 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.

This patch should fix your problems:  
[http://builds.frickle.com/nginx/patches/nginx\_\_clang\_Werror\_Wall\_fixes.patch](http://builds.frickle.com/nginx/patches/nginx__clang_Werror_Wall_fixes.patch)

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![Decimus\_Phostle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@Decimus\_Phostle](https://www.ruby-forum.com/u/Decimus_Phostle)\
**Post date:** [November 11, 2010, 3:36am UTC](https://www.ruby-forum.com/t/configuring-nginx-with-g/198940/4 "2010-11-11T03:36:59Z")

</div>

Hi there!

Thanks for your responses. My comments are inline.

On Wed, Nov 10, 2010 at 8:43 PM, Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > (Also, tried “CC=g++ ./configure …”).  
> > below). How do I go about fixing this? And/or is this fixed in a later  
> > release? (I am using 0.8.52). Please let me know.
> 
> 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.

> > 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.

Thanks,  
DP

---

<div class="post-metadata">

**Author:** ![Decimus\_Phostle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@Decimus\_Phostle](https://www.ruby-forum.com/u/Decimus_Phostle)\
**Post date:** [November 12, 2010, 12:05am UTC](https://www.ruby-forum.com/t/configuring-nginx-with-g/198940/5 "2010-11-12T00:05:28Z")

</div>

Piotr:

> This patch should fix your problems:  
> [http://builds.frickle.com/nginx/patches/nginx\_\_clang\_Werror\_Wall\_fixes.patch](http://builds.frickle.com/nginx/patches/nginx__clang_Werror_Wall_fixes.patch)

Thanks! That did fix the configure issue. But it threw up a bunch of  
warnings/errors in the nginx code. I guess I will take Maxim’s  
suggestion of opting for just compiling the handler via g++.

Maxim:

> E.g. the following will compile nginx with test cpp module:
> 
> ./configure --with-ld-opt=“-lstdc++” --with-cpp\_test\_module && make

Thanks that worked! I had a sort of code-organization/build follow-up  
question: as I write this module which has a bunch of source  
files/external dependencies, I was wondering if there would be a more  
modular way to organize code, say one package or directory per logical  
unit (and compile them independently). As opposed to having all the  
source in one directory and then have them all listed in  
NGX\_ADDON\_SRCS in ‘my\_module/config’. Please let me know.

Thanks,  
DP

2010/11/10 Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Decimus\_Phostle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@Decimus\_Phostle](https://www.ruby-forum.com/u/Decimus_Phostle)\
**Post date:** [November 11, 2010, 5:12am UTC](https://www.ruby-forum.com/t/configuring-nginx-with-g/198940/6 "2010-11-11T05:12:33Z")

</div>

Hello!

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.

> 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).

[1]

> **[Invoking G++ - Using the GNU Compiler Collection (GCC)](https://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/Invoking-G_002b_002b.html)**
>
> Using the GNU Compiler Collection (GCC)

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 D.
