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 Answers