0

I have a program that dlopen a .so library I made that uses another .so libary

  • my program (that uses libctp.so by dlopen at runtime)
  • libctp.so (that I made)
  • thostmduserapi.so (made by others, libctp.so uses it)

but when it runs, it shows this error:

symbol lookup error: ./libctp.so: undefined symbol: _ZN15CThostFtdcMdApi15CreateFtdcMdApiEPKcbb

Then I found out my .so doesn't actually link with another .so I used ldd to examine it:

 $ldd libctp.so
        linux-vdso.so.1 =>  (0x00007fff2b7db000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f736b082000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f736b649000)

no thostmduserapi.so there! It doesn't make scene to me, so I tried to NOT using GYP to generate the makefile. Instead, I type command by hand to compile it as below:

$ g++ -fPIC -shared main.cpp -l:thostmduserapi.so -Wl,-rpath,. -o libctp.so

It works!

$ ldd libctp.so
    linux-vdso.so.1 =>  (0x00007ffc0f6cb000)
    thostmduserapi.so (0x00007fb3cde36000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb3cda71000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb3cd853000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fb3cd64b000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb3cd447000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb3cd143000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb3cce3d000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb3ccc27000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fb3ce5dd000)

thostmduserapi.so is there!!

But how come makefile generated by GYP doesn't link with thostmduserapi.so!?? It seems very strange to me. Does anyone know what happen to GYP's project?

Thanks in advance for your help!

Here is my GYP file:

{
'targets': [
    {
        'target_name': 'ctp',
        'type': 'shared_library',
        'include_dirs': [
            '../../../',
        ],
        'sources': [
            'main.cpp',
        ],
        'product_dir': '../../../deploy',
        'ldflags': [
            '-l:thostmduserapi.so',
            '-L../../../third_party/ctp/linux64',
            '-Wl,-rpath,.',
        ],
        'cflags': [
            '-fPIC',
            '-std=c++11',
        ],
    },
],

}

Here is the code of my .so library

#include <third_party/ctp/linux64/include/ThostFtdcMdApi.h>
#include <stdio.h>

void* get_exchange_api(){
    printf("test\n");
    CThostFtdcMdApi::CreateFtdcMdApi(); // <-- function in thostmduserapi.so
    return 0;
}
kchkg
  • 147
  • 3
  • 13
  • I've found the problem is caused by the order of the commands that GYP generated – kchkg Jul 07 '16 at 04:16
  • g++ -fPIC -shared -l:thostmduserapi.so -L../../../third_party/ctp/linux64 -Wl,-rpath,. -o libctp2.so main2.o – kchkg Jul 07 '16 at 04:17
  • if I put main2.o before -l:thostmduserapi.so, everything is fine, but how to control GYP's command order? – kchkg Jul 07 '16 at 04:19
  • I ended up modifying GYP's code. Modified /build/gyp/pylib/gyp/generator/make.py, so that LDFLAGS goes after $(LIBS), which solved my problem! – kchkg Jul 08 '16 at 15:02

1 Answers1

0

Try to set libraries and library_dirs instead of ldflags manipulation:

{
'targets': [
    {
        'target_name': 'ctp',
        'type': 'shared_library',
        'include_dirs': [
            '../../../',
        ],
        'sources': [
            'main.cpp',
        ],
        'product_dir': '../../../deploy',
        'library_dirs': ['../../../third_party/ctp/linux64'],
        'libraries': ['thostmduserapi'],
        'cflags': [
            '-fPIC',
            '-std=c++11',
        ],
    },
],
pmed
  • 1,536
  • 8
  • 13