6

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.

Community
  • 1
  • 1
Mendes
  • 17,489
  • 35
  • 150
  • 263

4 Answers4

2

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.

Mendes
  • 17,489
  • 35
  • 150
  • 263
2

This solution not working for me. (With PHP7 extension) I found another solution

if test $PHP_VEHICLES != "no"; then
   CXXFLAGS="-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

So basically The only change is CXX_FLAGS="-std=c++0x" change to CXXFLAGS="-std=c++11"

David
  • 1,177
  • 3
  • 11
  • 26
  • That is the solution for PHP7 indeed. Also makes you realize how poorly documented is extension writing for PHP – Ad N Jun 05 '18 at 16:48
0

I recently compiled my PHP extension for PHP-7.3. The configuration is similar to the snippet shown below:

PHP_ARG_ENABLE(vehicles, whether to enable vehicles support,
dnl Make sure that the comment is aligned:
[  --enable-vehicles          Enable vehicles support], no)

if test "$PHP_VEHICLES" != "no"; then
    CXXFLAGS="-std=c++11" //other C++ linker flags go here
    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

Also, take a look at the Makefile generated from your compilation attempt. It will most likely contain information about any additional configuration required.

Loki
  • 1
  • 1
0

Note that ext/intl uses C++, so it's a good place to look for inspiration. For PHP 7.4 the C++ macros got some attention; ext/intl uses a new macro PHP_CXX_COMPILE_STDCXX with the existing PHP_REQUIRE_CXX().

  PHP_REQUIRE_CXX()
  PHP_CXX_COMPILE_STDCXX(11, mandatory, PHP_INTL_STDCXX)

The 11 stands for the C++ version, and it supports 11, 14, and 17 at this time. The PHP_INTL_STDCXX variable should then be added to the link flags.

Levi Morrison
  • 19,116
  • 7
  • 65
  • 85