1

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.

ferdynator
  • 6,245
  • 3
  • 27
  • 56
  • The order you link things in matters (see: https://stackoverflow.com/a/8140599/168175) - make sure that the .o file for export_wrap.cpp comes last in your command line when linking the shared object. (The error message you quoted at the end isn't a segfault) – Flexo May 04 '19 at 23:09
  • I've shortened the commands for the question. Is it even the correct way to compile everything together? Do I need the whole library C files when compiling my shared object? – ferdynator May 04 '19 at 23:24
  • The shared object you compile for SWIG needs to be linked against the implementation of the functions it wraps. For most libraries the easiest way to do that is to take the static or shared library that gets built in the normal process and link the SWIG generated code against that. – Flexo May 05 '19 at 07:57
  • Okay so I've compiled crfsuite the normal way and installed it with `make install`. The [docs](http://www.swig.org/Doc3.0/SWIGDocumentation.html#Tcl_nn6) state that I need to _ Make sure you compile both the SWIG wrapper file and your original program into a shared library file_. Does that mean the `crfsuite.cpp` or more than that? (Btw updated question with additional try&error) – ferdynator May 05 '19 at 12:25

0 Answers0