1

Im using ctypes following this example with cmake and gcc -fPIC.

#include <iostream>

class Foo {
public:
    void bar() {
        std::cout << "fooo" << std::endl;
    }
};

extern "C" {
    Foo* Foo_new() {
        return new Foo();
    }

    void Foo_bar(Foo* foo) {
        foo->bar();
    }
}

This compiles with the following cmake script:

set(CMAKE_CXX_STANDARD 11)

SET(SOURCES
        foo.cpp)

add_library(foo SHARED ${SOURCES})

However, if I add another static library from my project (target_link_libraries(foo otherlib)) I get errors from ctypes:

from ctypes import cdll
lib = cdll.LoadLibrary("./libfood.so")
lib.Foo_new()

>> AttributeError: ./libfood.so: undefined symbol: Foo_new

However I am only linking the other library, im not actually using it. Also nm says the symbol is defined:

$ nm libfood.so | grep Foo_new
000000000002650a T Foo_new

Also I noticed that with other static libraries this works fine, so my guess is that there is a problem with my static library. How do find out whats wrong with it?

Johannes
  • 3,300
  • 2
  • 20
  • 35
  • Do you compile the static library that you want to link against with `-fPIC`, too? – lubgr Jul 12 '18 at 14:55
  • Yes. However it has some large dependencies (*should* all be static, fPIC aswell) which are built using different build systems. It is hard to keep track of them. Is there a way to find out if one of the dependencies is built incorrectly? I used the static library before and it works fine, but I never included it with a shared library. – Johannes Jul 12 '18 at 15:00
  • What is the output of `nm -D libfood.so | grep Foo_new`? – Mike Kinghan Jul 13 '18 at 08:04
  • The output is empty. Without the added static library its `000000000000094a T Foo_new` – Johannes Jul 13 '18 at 10:59
  • Something in the changes you make that involve the static library is causing the linker to omit `Foo_new` from the dynamic symbol table of `libfood.so`, so it will not be resolved in dynamic linkage. You will need to provide a [mcve] to reveal why. – Mike Kinghan Jul 13 '18 at 13:07
  • Thanks Mike, but the static library is 100mb and the code has lots of dependencies. I cannot upload the sourcecode or the binary here. Hence, the question how to debug this (im not asking for a definite answer, just a hint how to access the problem). – Johannes Jul 16 '18 at 15:03

1 Answers1

0

Found the problem: the static library was linked with -pie (it slipped in from the android build). Rebuilding the static library without -pie solved it.

Thanks for the valuable hints though

Johannes
  • 3,300
  • 2
  • 20
  • 35