I have a small C++ test project which should result in a .so-library on Windows Subsystem for Linux (WSL, more or less = Ubuntu 14.x). In order to get a PHP extension the current version of the PHP-CPP library is linked to the project. The compiler is g++ 4.8 on Linux and remotely operated from Visual Studio. In the properties I add -fpic for compiling and -lphpcpp to link libphpcpp.so. Using the old version of Visual Studio 2017 (until 02/2018) everything behaved well and the build was fine.
After updating to the current version of Visual Studio 2017 (15.5.6, 02/2018) the build ends in numerous linking errors of the same type: /usr/bin/ld : error : relocation x has invalid symbol index y, where the numbers x and y vary. I have no idea what happens here.
The small project consists uses the ideas from here:
DLL in Visual Studio ... and ... PHP-CPP first extension
The linking options recorded in Visual Studio:
-o"C:\Users\Robert\Documents\Lightning Talk\MathFuncs\MathFuncs\bin\x64\Release\MathFuncs.out" "3600000"
-Wl,-z,relro -Wl,--print-map -Wl,-z,noexecstack -Wl,--trace -Wl,
--verbose -Wl,--no-undefined "g++" -l"phpcpp" -Wl,-z,now
The last lines of the ld/g++ error messages:
1>/usr/bin/ld : error : relocation 15 has invalid symbol index 13
1>/usr/bin/ld : error : relocation 16 has invalid symbol index 13
1>/usr/bin/ld : error : relocation 17 has invalid symbol index 13
1>/usr/bin/ld : error : relocation 18 has invalid symbol index 13
1>/usr/bin/ld : error : relocation 19 has invalid symbol index 21
1>/usr/bin/ld : error : relocation 0 has invalid symbol index 2
1>/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o : error :
1>collect2 : error : ld returned 1 exit status
1>Die Erstellung des Projekts "MathFuncs.vcxproj" ist abgeschlossen -- FEHLER.
My code is here: MathFuncs.h:
#pragma once
#include <stdexcept>
using namespace std;
// This class is exported from the MathFuncsDll.dll
class MathFuncs
{
public:
// Returns a + b
static double Add(double a, double b);
// Returns a - b
static double Subtract(double a, double b);
// Returns a * b
static double Multiply(double a, double b);
// Returns a / b
// Throws const std::invalid_argument& if b is 0
static double Divide(double a, double b);
};
double MathFuncs::Add(double a, double b)
{
return a + b;
}
double MathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
... and main.cpp:
#include <iostream>
#include <phpcpp.h>
#include "MathFuncs.h"
Php::Value phpAdd(Php::Parameters ¶ms)
{
auto a(0.0);
a = params[0];
auto b(0.0);
b = params[1];
return MathFuncs::Add(a, b);
}
Php::Value phpSubtract(Php::Parameters ¶ms)
{
auto a(0.0);
a = params[0];
auto b(0.0);
b = params[1];
return MathFuncs::Subtract(a, b);
}
Php::Value phpMultiply(Php::Parameters ¶ms)
{
auto a(0.0);
a = params[0];
auto b(0.0);
b = params[1];
return MathFuncs::Multiply(a, b);
}
Php::Value phpDivide(Php::Parameters ¶ms)
{
auto a(0.0);
a = params[0];
auto b(0.0);
b = params[1];
return MathFuncs::Divide(a, b);
}
extern "C" {
/**
* Function that is called by PHP right after the PHP process
* has started, and that returns an address of an internal PHP
* strucure with all the details and features of your extension
*
* @return void* a pointer to an address that is understood by PHP
*/
PHPCPP_EXPORT void *get_module()
{
// static(!) Php::Extension object that should stay in memory
// for the entire duration of the process (that's why it's static)
static Php::Extension extension("phpMathFuncs", "1.0"); // To be humble, we can change the version number to 0.0.1
extension.add<phpAdd>("Add", {
Php::ByVal("a", Php::Type::Float),
Php::ByVal("b", Php::Type::Float)
});
extension.add<phpSubtract>("Subtract", {
Php::ByVal("a", Php::Type::Float),
Php::ByVal("b", Php::Type::Float)
});
extension.add<phpMultiply>("Multiply", {
Php::ByVal("a", Php::Type::Float),
Php::ByVal("b", Php::Type::Float)
});
extension.add<phpDivide>("Divide", {
Php::ByVal("a", Php::Type::Float),
Php::ByVal("b", Php::Type::Float)
});
// return the extension
return extension;
}
}