我有以下情况:
我创建了动态库lib.so。这个库使用另一个静态库lib.a。它们都使用Boost库(我在CMake文件中链接它们)。(我在Java项目中使用这个动态库)
这是lib中file.cpp的代码。so,从lib.a中调用getFilesFromDirectory()
#include "DrawingDetector.h"
#include "../../DrawingDetection.h"
#include <vector>
#include <boost/filesystem/operations.hpp>
using namespace std;
JNIEXPORT void JNICALL Java_DrawingDetector_detectImage( JNIEnv * env, jobject, jstring jMsg)
{
const char* msg = env->GetStringUTFChars(jMsg,0);
vector<File> filesPic = getFilesFromDirectory(msg,0);
env->ReleaseStringUTFChars(jMsg, msg);
}
这是来自lib.a的getFilesFromDirectory()的代码:
#include <string.h>
#include <iostream>
#include <boost/filesystem/operations.hpp>
#include "DrawingDetection.h"
using namespace std;
using namespace boost;
using namespace boost::filesystem;
vector<File> getFilesFromDirectory(string dir, int status)
{
vector<File> files;
path directoty = path(dir);
directory_iterator end;
if (!exists(directoty))
{
cout<<"There is no such directory or file!"<<endl;
}
for (directory_iterator itr(directoty); itr != end; ++itr)
{
if ( is_regular_file((*itr).path()))
{
//some code
}
}
return files;
}
但是当我的项目调用lib。因此,它会引发以下消息:
java: symbol lookup error: /home/orlova/workspace/kmsearch/Images/branches/DrawingDetection/jni/bin/lib.so: undefined symbol:_ZN5boost11filesystem34path21wchar_t_codecvt_facetEv
当我发现它在lib中崩溃时。A,当它试图调用boost方法"路径"。但是我声明了所有的boost头,并将它们链接在CMake文件中。你能解释一下为什么它不能识别boost方法吗?
编辑:
我的编译器版本是gcc 4.6。如果我使用4.5,一切都很好!
如果我直接在lib中的file.cpp中使用一些boost方法。所以一切正常,例如:
JNIEXPORT void JNICALL Java_DrawingDetector_detectImage( JNIEnv * env, jobject, jstring jMsg)
{
const char* msg = env->GetStringUTFChars(jMsg,0);
path directoty = path("/home/orlova");//if I use boost method here
vector<File> filesPic = getFilesFromDirectory(msg,0);// then everything works fine
env->ReleaseStringUTFChars(jMsg, msg);
}
,
JNIEXPORT void JNICALL Java_DrawingDetector_detectImage( JNIEnv * env, jobject, jstring jMsg)
{
const char* msg = env->GetStringUTFChars(jMsg,0);
//path directoty = path("/home/orlova");//if I don't use boost method here
vector<File> filesPic = getFilesFromDirectory(msg,0);// then this method causes before-mentioned error!!
env->ReleaseStringUTFChars(jMsg, msg);
}
如何解释编译器的这种行为?
注意,错误发生在运行时。
解决问题是在CMake文件中的*target_link_libraries*中的库顺序。
错:find_library( LIBRARY_MAIN
NAMES lib.a
PATHS ../bin
)
set ( LIB_JNI
jpeg
${OpenCV_LIBS}
${BOOST_LIB}
${LIBRARY_MAIN}//static library is the last in the list of libraries and after boost!
)
target_link_libraries( ${PROJECT} ${LIB_JNI} )
:
find_library( LIBRARY_MAIN
NAMES lib.a
PATHS ../bin
)
set ( LIB_JNI
${LIBRARY_MAIN}
jpeg
${OpenCV_LIBS}
${BOOST_LIB}//boost library is the last in the list and after static library!
)
target_link_libraries( ${PROJECT} ${LIB_JNI} )
似乎您动态链接Boost.Filesystem
-如果是这样,您必须确保该库被加载。您可以将其添加到Android.mk
中的LOCAL_LDLIBS
行,或者在Java代码中加载lib.so
之前手动预加载它。
或者,只需将Boost(和其他所有内容)静态地链接到lib.so
,忘记所有这些动态混乱。:)
UPDATE1:关于您发布的更新—尝试将boost_filesystem。A作为链接行中最后一个对象