Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C/C++ library function from PHP - How to send a -std=c+11 to compiler

Tags:

c++

php

c++11

I´m building a C++ extension to PHP, using a template of config.m4 from this post and this article.

I need to use the C++11 in standard to compile my classes, so I´ve used the EXTRA_FLAGS clause as:

EXTRA_FLAGS="-std=c++11"

in my config.m4. The final code:

PHP_ARG_ENABLE(vehicles,
    [Whether to enable the "vehicles" extension],
    [  --enable-vehicles      Enable "vehicles" extension support])

if test $PHP_VEHICLES != "no"; then
    EXTRA_FLAGS="-std=c++11"
    PHP_REQUIRE_CXX()
    PHP_SUBST(VEHICLES_SHARED_LIBADD)
    PHP_ADD_LIBRARY(stdc++, 1, VEHICLES_SHARED_LIBADD)
    PHP_NEW_EXTENSION(vehicles, vehicles.cc car.cc, $ext_shared)
fi

This is not working at all (the compiler does not receive the extra flags). I then assume that this EXTRA_FLAGS parameter is not related to the compiler at all, but to the script....

How can I send a flag to the g++ compiler to them him to use C++11 ?

Thanks for helping.

like image 856
Mendes Avatar asked Aug 03 '15 20:08

Mendes


1 Answers

I´ve found a solution. Here is the definitive code:

PHP_ARG_ENABLE(vehicles,
    [Whether to enable the "vehicles" extension],
    [  --enable-vehicles      Enable "vehicles" extension support])

if test $PHP_VEHICLES != "no"; then
    CXX_FLAGS="-std=c++0x"
    PHP_REQUIRE_CXX()
    PHP_SUBST(VEHICLES_SHARED_LIBADD)
    PHP_ADD_LIBRARY(stdc++, 1, VEHICLES_SHARED_LIBADD)
    PHP_NEW_EXTENSION(vehicles, vehicles.cc car.cc, $ext_shared)
fi

Make sure the CXX_FLAGS goes before PHP_REQUIRE_CXX() otherwise it won´t work.

There is also a macro called X_CXX_COMPILE_STDCXX_11([noext], [mandatory]) whose code is here that automates that process.

like image 115
Mendes Avatar answered Sep 30 '22 13:09

Mendes