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

I am creating a C ++ extension for PHP using the config.m4 template from this post and this article .

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

EXTRA_FLAGS="-std=c++11" 

in my config.m4. 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 does not work at all (the compiler does not receive additional flags). Then I assume that this EXTRA_FLAGS parameter is not associated with the compiler at all, but with the script ....

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

Thanks for the help.

+5
source share
4 answers

I have found a solution. Here is 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 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 that CXX_FLAGS comes before PHP_REQUIRE_CXX() , otherwise it will not work.

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

+1
source

This solution does not work 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: CXX_FLAGS="-std=c++0x" change to CXXFLAGS="-std=c++11"

+2
source

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 by your compilation attempt. Most likely, it will contain information about any additional necessary configuration.

0
source

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

  PHP_REQUIRE_CXX() PHP_CXX_COMPILE_STDCXX(11, mandatory, PHP_INTL_STDCXX) 

11 denotes a C ++ version, and it currently supports 11, 14, and 17. Then the PHP_INTL_STDCXX variable should be added to the link flags.

0
source

All Articles