简体   繁体   English

JNI共享库中的未定义符号,可执行文件正在运行

[英]Undefined symbol in shared library for JNI, executable is working

I am really unexperienced when it comes to C++ so I hope this just a small mistake that causes this error. 对于C ++,我真的没有经验,所以我希望这只是一个导致该错误的小错误。 Sorry that the post is so long but I tried to include all the important details. 抱歉,帖子这么长,但我尝试提供所有重要细节。

So here is what I am trying to do: I am using an external library that comes in the form of two folders with a makefile each. 因此,这就是我想要做的事情:我正在使用一个外部库,该库以两个文件夹的形式出现,每个文件夹都有一个makefile。 I am trying to create a JNI library with one additional cpp file and the JNI header in an .h file. 我试图用一个附加的cpp文件和一个.h文件中的JNI标头创建一个JNI库。

So the folder structure is like this: -> libcds/src with a makefile that creates libcds.a 因此,文件夹结构是这样的:-> libcds / src,带有创建libcds.a的makefile
-> SSA2 with a makefile that creates SSA.a ->具有创建SSA.a的makefile的SSA2
-> myFolder with my cpp and h file and a makefile that should create the library -> myFolder,其中包含我的cpp和h文件以及应创建库的makefile

So here are the different makefiles: 所以这是不同的makefile:

libcds/src: libcds / src目录:

CPP=g++

CPPFLAGS=-O9 -w -DNDEBUG 

INCL=-I../includes/

CODERS_DIR=coders
CODERS_OBJECTS=$(CODERS_DIR)/huff.o $(CODERS_DIR)/huffman_codes.o

STATIC_PERMUTATION_DIR=static_permutation
STATIC_PERMUTATION_OBJECTS=$(STATIC_PERMUTATION_DIR)/perm.o ...

STATIC_BITSEQUENCE_DIR=static_bitsequence
STATIC_BITSEQUENCE_OBJECTS=$(STATIC_BITSEQUENCE_DIR)/static_bitsequence.o ..

STATIC_SEQUENCE_DIR=static_sequence
STATIC_SEQUENCE_OBJECTS=$(STATIC_SEQUENCE_DIR)/static_sequence.o, ..., wt_coder.o

UTILS_DIR=utils
UTILS_OBJECTS=$(UTILS_DIR)/alphabet_mapper_none.o $(UTILS_DIR)/alphabet_mapper.o

%.o: %.cpp

$(CPP) $(CPPFLAGS) $(INCL) -c $< -o $@

all: lib 

lib: pre $(CODERS_OBJECTS) $(STATIC_BITSEQUENCE_OBJECTS) $(STATIC_SEQUENCE_OBJECTS) $(UTILS_OBJECTS) $(STATIC_PERMUTATION_OBJECTS)
    ar rcs ../lib/libcds.a $(CODERS_OBJECTS) $(STATIC_BITSEQUENCE_OBJECTS) $(STATIC_SEQUENCE_OBJECTS) $(UTILS_OBJECTS) $(STATIC_PERMUTATION_OBJECTS)

pre:
    cp basics.h ../includes/
    cp */*.h ../includes/

SSA2: SSA2:

CC=g++

CFLAGS=-w -O9 -DNDEBUG -I../libcds/includes/
CODERS_DIR=../libcds/src/coders

CODERS_OBJECTS=$(CODERS_DIR)/huff.o $(CODERS_DIR)/huffman_codes.o

STATIC_BITSEQUENCE_DIR=../libcds/src/static_bitsequence

STATIC_BITSEQUENCE_OBJECTS=$(STATIC_BITSEQUENCE_DIR)/static_bitsequence.o ...

STATIC_SEQUENCE_DIR=../libcds/src/static_sequence

STATIC_SEQUENCE_OBJECTS=$(STATIC_SEQUENCE_DIR)/static_sequence.o ...

UTILS_DIR=../libcds/src/utils

UTILS_OBJECTS=$(UTILS_DIR)/alphabet_mapper_none.o $(UTILS_DIR)/alphabet_mapper.o

all: index

# pattern rule for all objects files

%.o: %.c *.h

    $(CC) -c $(CFLAGS) $< -o $@

%.o: %.cpp *.h

    $(CC) -c $(CFLAGS) $< -o $@

index: bitrankw32int.o bitarray.o  ds/ds.o ds/globals.o ds/helped.o ds/shallow.o ds/deep2.o ds/blind2.o SSA.o

    cp ../libcds/lib/libcds.a SSA.a

    ar rcs SSA.a SSA.o bitrankw32int.o bitarray.o ds/ds.o ds/globals.o ds/shallow.o ds/helped.o ds/deep2.o ds/blind2.o

My makefile: 我的makefile:

CC=g++ -I $(JAVA_HOME)/include -I $(JAVA_HOME)/include/linux
LD_LIBRARY_PATH=./
JNI_NAME= indexlib

all: test

%.o: %.c *.h
    $(CC) -c $(CFLAGS) $< -o $@

test_index: 
    $(CC) run_queries.c -o testIndex SSA.a

test_index_lib: run_queries.o
    ld -shared -soname lib$(JNI_NAME).so -o lib$(JNI_NAME).so run_queries.o SSA.a -lc

The executable testIndex works without issues. 可执行文件testIndex可以正常工作。 The resulting library, however can't be loaded by Java with the error message: 但是,生成的库无法通过错误消息由Java加载:

undefined symbol: _ZNSt8ios_base4InitC1Ev 未定义的符号:_ZNSt8ios_base4InitC1Ev

When I use nm to find that symbol I get the following result: 当我使用nm查找该符号时,得到以下结果:

000131b0 W _ZN8wt_coderD2Ev 000131b0 W _ZN8wt_coderD2Ev
U _ZNKSt5ctypeIcE13_M_widen_initEv U _ZNKSt5ctypeIcE13_M_widen_initEv
U _ZNSo3putEc U _ZNSo3putEc
U _ZNSo5flushEv U _ZNSo5flushEv
U _ZNSo9_M_insertImEERSoT_ U _ZNSo9_M_insertImEERSoT_
U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitC1Ev
U _ZNSt8ios_base4InitD1Ev U _ZNSt8ios_base4InitD1Ev
U _ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i U _ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
U _ZSt16__throw_bad_castv U _ZSt16__throw_bad_castv
U _ZSt4cout U _ZSt4cout

And with --demangle it looks like this: 000131b0 W wt_coder::~wt_coder() 使用--demangle看起来像这样:000131b0 W wt_coder ::〜wt_coder()
U std::ctype::_M_widen_init() const U std :: ctype :: _ M_widen_init()常量
U std::ostream::put(char) U std :: ostream :: put(char)
U std::ostream::flush() U std :: ostream :: flush()
U std::ostream& std::ostream::_M_insert(unsigned long) U std :: ostream&std :: ostream :: __ M_insert(无符号长)
U std::ios_base::Init::Init() U std :: ios_base :: Init :: Init()
U std::ios_base::Init::~Init() U std :: ios_base :: Init ::〜Init()
U std::basic_ostream >& std::__ostream_insert U std :: basic_ostream>&std :: __ ostream_insert

(std::basic_ostream >&, char const*, int) U std::__throw_bad_cast() (std :: basic_ostream>&,char const *,int)U std :: __ throw_bad_cast()
U std::cout U std :: cout

So actually this could be related to Linker error: undefined reference to `std::ctype<char>::_M_widen_init() 所以实际上这可能与链接器错误有关:对`std :: ctype <char> :: __ M_widen_init()的未定义引用

I think you are not linking to the C++ standard library. 我认为您没有链接到C ++标准库。 You need it because one of the libraries you are linking to is C++. 您需要它,因为要链接的库之一是C ++。

In the first case (target test_index) everything works okay because you are using g++ to compile and link, and it will link to libstdc++ implicitly. 在第一种情况下(目标test_index),一切正常,因为您正在使用g++进行编译和链接,并且它将隐式链接到libstdc ++。 In the shared library case you are linking with ld which does not link libstdc++ implicitly. 在共享库的情况下,您将与ld链接,而ld不会隐式链接libstdc ++。

You can use g++ to compile and link the shared library or add libstdc++ explicitly with -lstdc++ . 您可以使用g++编译和链接共享库,也可以使用-lstdc++显式添加libstdc -lstdc++ Either should work. 两者都应该起作用。

因此问题最终变得非常简单:我必须使用g ++来创建库,而不是ld

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM