drfu29983 2019-05-04 23:05
浏览 115

使用SWIG编译PHP扩展

I am trying to compile the swig interface of crfsuite as a PHP module. So far I've managed to actually compile a crfsuite.so file, load it as an extension in PHP and call methods on it. As far as I can see any method call that was wrapped by SWIG works fine out of the box. Unfortunately I seem to be missing some symbols:

❯ nm crfsuite.so | grep crfsuite
                 U crfsuite_attribute_set
                 U crfsuite_create_instance
                 U crfsuite_create_instance_from_file
                 U crfsuite_create_instance_from_memory
                 U crfsuite_data_append
                 U crfsuite_data_finish
                 U crfsuite_data_init
00000000000281b3 T crfsuite_dictionary_create_instance
                 U crfsuite_evaluation_accmulate
                 U crfsuite_evaluation_finalize
                 U crfsuite_evaluation_init
                 U crfsuite_evaluation_output
0000000000256460 B crfsuite_globals
                 U crfsuite_instance_finish
                 U crfsuite_instance_init_n
                 U crfsuite_interlocked_decrement
                 U crfsuite_interlocked_increment
                 U crfsuite_item_append_attribute
                 U crfsuite_item_init
                 U crfsuite_item_init_n

I have generated the swig wrapper like so, using the default export.i:

swig -c++ -php7 -Icrfsuite/include -o export_wrap.cpp export.i

After that I've compiled the code with the following commands:

gcc -fpic -Icrfsuite/include -Icrfsuite/lib/cqdb/include -Iliblbfgs/include \ 
    `php-config --includes` -c crfsuite.cpp export_wrap.cpp crfsuite/lib/crf/src/*.c \ 
    crfsuite/swig/*.cpp crfsuite/lib/cqdb/src/cqdb.c crfsuite/lib/cqdb/src/lookup3.c \
    liblbfgs/lib/*.c
gcc -shared *.o -o crfsuite.so

I've played with many gcc flags such as -L/usr/local/lib, -lcrfsuite, -Wl,-undefined,dynamic_lookup but stopped as they didn't seem to affect the result.

Running a basic example errors with the following message:

 /usr/bin/php: symbol lookup error: /usr/lib/php/20170718/crfsuite.so: undefined symbol: crfsuite_data_init

The example correctly calls the Trainer constructor.

This all is quite new so I am lost between the many steps. Am I missing something obvious? Why are the symbols not linked correctly? And how can it be fixed?

Update:

I've set the $LD_LIBRARY_PATH and $LD_RUN_PATH to /usr/local/lib. That is also where the compiled and installed version of the crfsuite lies as a shared object:

ll $LD_LIBRARY_PATH/libcrfsuite.so
/usr/local/lib/libcrfsuite.so -> libcrfsuite-0.12.so

I have also changed the order of .o files. Didn't change anything.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
    • ¥20 Python安装cvxpy库出问题
    • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
    • ¥15 python天天向上类似问题,但没有清零
    • ¥30 3天&7天&&15天&销量如何统计同一行
    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 C#调用python代码(python带有库)
    • ¥15 活动选择题。最多可以参加几个项目?
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题