Mendes
Mendes

Reputation: 18561

Calling C/C++ library function from PHP - with multiple classes in a shared library

Reading this post and this article I got my first PHP extension up and running.

Here is the config.m4 file from the article that I used (to keep it clear I kept the same vehicles and car class names from the article, although my real classes has different names):

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

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

What I need now is to move o another level of code organization as follow:

a) Moving the carclass to a common vehicle class folder with other vehicle classes (truck, bus, etc.)

b) Build a shared library will all of those classes

c) Call that shared library class from PHP

So, I would have the following directory structure like:

vehicles -> src = The .cpp classes and include files

vehicles -> lib = The vehicle.so shared library

How do I modify the config.m4 to work with this structure, considering all the vehicle´s classes include files from the original paths and including the shared .so library to the final built.

Thanks for helping.

Upvotes: 2

Views: 973

Answers (1)

Mendes
Mendes

Reputation: 18561

PHP_ADD_LIBRARY_WITH_PATHdid the trick. Use it as many times as you need to (for adding multiple libraries).

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

    PHP_ADD_LIBRARY_WITH_PATH(libraryname1, /etc/whatever_path_to_library, VEHICLES_SHARED_LIBADD)
    PHP_ADD_LIBRARY_WITH_PATH(libraryname2, /etc/whatever_path_to_library, VEHICLES_SHARED_LIBADD)
    PHP_ADD_LIBRARY_WITH_PATH(libraryname3, /etc/whatever_path_to_library, VEHICLES_SHARED_LIBADD)

    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

Upvotes: 1

Related Questions