在Visual Studio 2017中将PHP-CPP库链接到Linux项目

时间:2018-03-13 17:21:02

标签: c++ visual-studio php-cpp

我有一个小的C ++测试项目,它应该在Windows的Solaris子系统上产生一个.so-library(WSL,或多或少= Ubuntu 14.x)。为了获得PHP扩展,当前版本的PHP-CPP库链接到项目。编译器在Linux上是g ++ 4.8,从Visual Studio远程操作。在属性中,我添加-fpic进行编译,使用-lphpcpp链接libphpcpp.so。使用旧版本的Visual Studio 2017(直到02/2018),一切都表现良好,构建很好。

更新到当前版本的Visual Studio 2017(15.5.6,02 / 2017)后,构建以相同类型的多个链接错误结束: / usr / bin / ld:错误:重定位x无效符号索引y ,其中数字x和y不同。 我不知道这里发生了什么。

小项目包括使用此处的想法:

DLL in Visual Studio ......而且...... PHP-CPP first extension

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

ld / g ++错误消息的最后几行:

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.

我的代码在这里: 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;
}

...和main.cpp:

#include <iostream>
#include <phpcpp.h>
#include "MathFuncs.h"

Php::Value  phpAdd(Php::Parameters &params)
{

    auto a(0.0);
    a = params[0];

    auto b(0.0);
    b = params[1];

    return MathFuncs::Add(a, b);
}

Php::Value phpSubtract(Php::Parameters &params)
{
    auto a(0.0);
    a = params[0];

    auto b(0.0);
    b = params[1];

    return MathFuncs::Subtract(a, b);
}

Php::Value phpMultiply(Php::Parameters &params)
{
    auto a(0.0);
    a = params[0];

    auto b(0.0);
    b = params[1];

    return MathFuncs::Multiply(a, b);
}

Php::Value phpDivide(Php::Parameters &params)
{
    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;
    }
}

1 个答案:

答案 0 :(得分:0)

在项目属性/ general中,已经是“application(.out)”。将其更改为“动态库(.so)”解决了这个问题。