Search code examples
androidc++cmakeandroid-ndkpoco-libraries

Issue linking against POCO::Net::SocketAddress


I'm working on an Android native project where I'm using NDK + CMake to build a native solution based on POCO C++. I'm getting a super weird link error when I comment this line in my cpp file:

//std::ostream& o = session.sendRequest(request);

this is the link error:

../NetSSL_OpenSSL/src/SecureSocketImpl.cpp:381: error: undefined reference to 'Poco::Net::SocketAddress::~SocketAddress()'

But is even more weird that the file with the object where sendRequest is placed it is not being included in any other file. So, I have one isolated file (header and implementation) that is not being included in any other file (but it is build with CMake, I cannot remove it because of the issue) and if I comment one line of the implementation, it generates a link issue.

This is how I'm linking Poco:

cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_COMPILER_VERSION "5.0") # Unknown miss-detection of the compiler by CMake

find_library( # Defines the name of the path variable that stores the
        # location of the NDK library.
        log-lib
        # Specifies the name of the NDK library that
        # CMake needs to locate.
        log )

add_library(PocoFoundation STATIC IMPORTED)
set_target_properties(PocoFoundation PROPERTIES IMPORTED_LOCATION
        /Users/user/dev/poco-2/install/lib/libPocoFoundation.a)

add_library(PocoNet STATIC IMPORTED)
set_target_properties(PocoNet PROPERTIES IMPORTED_LOCATION
        /Users/user/dev/poco-2/install/lib/libPocoNet.a)

add_library(PocoJSON STATIC IMPORTED)
set_target_properties(PocoJSON PROPERTIES IMPORTED_LOCATION
        /Users/user/dev/poco-2/install/lib/libPocoJSON.a)

add_library(PocoNetSSL STATIC IMPORTED)
set_target_properties(PocoNetSSL PROPERTIES IMPORTED_LOCATION
        /Users/user/dev/poco-2/install/lib/libPocoNetSSL.a)

add_library(PocoCrypto STATIC IMPORTED)
set_target_properties(PocoCrypto PROPERTIES IMPORTED_LOCATION
        /Users/user/dev/poco-2/install/lib/libPocoCrypto.a)

add_library(PocoUtil STATIC IMPORTED)
set_target_properties(PocoUtil PROPERTIES IMPORTED_LOCATION
        /Users/user/dev/poco-2/install/lib/libPocoUtil.a)

add_library(PocoXML STATIC IMPORTED)
set_target_properties(PocoXML PROPERTIES IMPORTED_LOCATION
        /Users/user/dev/poco-2/install/lib/libPocoXML.a)


// ... SOURCES ...

target_include_directories(mylib PRIVATE /Users/user/dev/poco-2/install/include)


target_link_libraries(mylib
        PocoNet
        PocoNetSSL
        PocoUtil
        PocoJSON
        PocoCrypto
        PocoXML
        PocoFoundation
        Ssl
        Crypto
        ${log-lib})

Also, I'm using Android Studio 3.6.2 with NDK version r19c. I have tried cleaning the build, rebuilding and cleaning .cxx, .gradle and build folder.

Any idea?


Solution

  • You need to recreate the inter-library dependencies such that CMake can construct the correct linker command line with dependencies after the libraries that depend on them.

    Running through the libraries you listed, that should be at least the following (reconstructed from the Poco CMakeLists.txt).

    target_link_libraries(PocoNet PocoFoundation)
    target_link_libraries(PocoNetSSL PocoCrypto PocoUtil PocoNet)
    target_link_libraries(PocoCrypto PocoFoundation Ssl Crypto)
    target_link_libraries(PocoUtil PocoFoundation)
    target_link_libraries(PocoJSON PocoFoundation)
    target_link_libraries(PocoXML PocoFoundation)