Reputation: 1984
I have a CMake project with the following structure
root
|- common
|- Bitmap.h
|- Bitmap.cpp
|- BytesUtils.h
|- BytesUtils.cpp
|- encoding
|- jni
|- impl.cpp
|- main.cpp
The project will create two artifacts: an executable using main.cpp, and a shared library using the source code in jni/ to be loaded by JNI. They both rely on the source files in common and encoding.
Here's my root/CMakeLists.txt
file(GLOB_RECURSE COMMON_SOURCE
"common/*.h"
"common/*.cpp"
)
file(GLOB_RECURSE ENCODING_SOURCE
"encoding/*.h"
"encoding/*.cpp"
)
add_library(common STATIC ${COMMON_SOURCE})
add_library(encoding STATIC ${ENCODING_SOURCE})
add_executable(main main.cpp)
target_link_libraries(main common)
target_link_libraries(main encoding)
add_subdirectory(jni)
and here's the jni/CMakeLists.txt
set(JAVA_HOME "/usr/lib/jvm/jdk1.8.0_221")
#set(JAVA_HOME "/usr/lib/jvm/java-8-openjdk-amd64/")
include_directories(${JAVA_HOME}/include)
include_directories(${JAVA_HOME}/include/linux)
file(GLOB_RECURSE JNI_SOURCE
"*.h"
"*.cpp"
)
add_library(lqf SHARED ${JNI_SOURCE})
target_link_libraries(lqf common)
target_link_libraries(lqf encoding)
When I load the library using JNI I got the following error:
/usr/lib/jvm/jdk1.8.0_221/bin/java: symbol lookup error: /home/harper/git/lqf/lqf-cpp/cmake-build-debug/lib/liblqf.so: undefined symbol: _ZN6common6BitmapC1EPmj
Using nm
to look at the generated so, I discovered that all functions from common/Bitmap.cpp are marked as undefined symbol
U _ZN6common6Bitmap10appendBitsEhj
U _ZN6common6Bitmap11moveForwardEj
U _ZN6common6BitmapC1EPmj
But functions from other source code under common/ are included properly
0000000000002894 T _ZN6common10BytesUtils19readIntLittleEndianEPhPj
00000000000028d0 T _ZN6common10BytesUtils35readIntLittleEndianPaddedOnBitWidthEPhPjh
00000000000029d3 T _ZN6common10BytesUtils18readUnsignedVarIntEPhPj
The only difference I can see is that Bitmap is a class and BytesUtils is a collection of functions.
Bitmap.h
#include <cstdint>
namespace common {
class Bitmap {
public:
Bitmap(uint64_t *data, uint32_t offset);
void appendWord(uint64_t *word, uint32_t count);
void appendBits(uint8_t bit, uint32_t rep);
void moveForward(uint32_t count);
};
}
Bitmap.cpp:
#include "Bitmap.h"
common::Bitmap::Bitmap(uint64_t *data, uint32_t offset) {...}
void common::Bitmap::appendBits(uint8_t bit, uint32_t repetition) {...}
void common::Bitmap::appendWord(uint64_t *word, uint32_t count) {...}
void common::Bitmap::moveForward(uint32_t count) {...}
So what is a proper way to build the shared library so these symbols are included?
Update @ Sep 24, 2019 21:32
This is caused by dependencies between static libraries. The encoding lib uses common lib but I failed to add the target_link_libraries between them. After adding target_link_libraries(encoding, common)
the symbol tables is fixed.
Upvotes: 0
Views: 1043
Reputation: 1984
This is caused by dependencies between static libraries. The encoding lib uses common lib but I failed to add the target_link_libraries between them. After adding target_link_libraries(encoding common)
the symbol tables is fixed.
Upvotes: 1