4

我有以下情况:

我创建了动态库 lib.so。该库使用另一个静态库 lib.a。它们都使用 Boost 库(我将它们链接到 CMake 文件中)。(我在Java项目中使用这个动态库)

这是 lib.so 中 file.cpp 的代码,它从 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.so 库时,它会引发以下消息:

 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 文件中。你能解释一下,为什么它不能识别提升方法?

编辑

我的编译器 gcc 4.6 的版本。如果我使用 4.5 一切都很好!

此外,如果我直接在 lib.so 中的 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} )
4

1 回答 1

3

您似乎是Boost.Filesystem动态链接的 - 如果是这样,您必须确保加载此库。您可以通过将其添加到LOCAL_LDLIBSline in来执行此操作,或者在加载Java 代码Android.mk之前手动预加载它。lib.so

或者,只需将 Boost(和其他所有内容)静态链接到lib.so并忘记所有这些动态混乱。:)

UPDATE1:关于您发布的更新——尝试将 boost_filesystem.a 作为链接器行中的最后一个对象。

于 2013-01-16T15:40:16.340 回答