c++ - 无法将 boost 库连接到我的项目 "symbol lookup error"

标签 c++ boost linker java-native-interface undefined-symbol

我有以下情况:

我已经创建了动态库 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

正如我发现的那样,当它试图调用 boost 方法“路径”时,它在 lib.a 中崩溃了。 但我声明了所有 boost header ,并将它们链接到 CMake 文件中。 你能解释一下,为什么它不识别 boost 方法吗?

编辑:

我的编译器 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} )

最佳答案

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

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

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

关于c++ - 无法将 boost 库连接到我的项目 "symbol lookup error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14360464/

相关文章:

c++11 和 boost_filesystem cout 和 printf 不起作用

gcc - 在 GCC 中链接包含循环引用的库

macos - 在 Mac 上延迟(动态)加载框架(或 dylib)

c++ - 使用istringstream处理一个变长的内存块

c++ - 将 int64_t 转换为 time_duration

c++ - 编译Boost::Python时出现问题

c - GCC 在编译时在/usr/local/include 中查找头文件,但在链接时不在/usr/local/lib 中查找库。为什么?

c++ - 我应该使用断言来验证第三方功能吗?

c++ - 设置变量的最大可能值 C++

c++ - Google Test 宏似乎不适用于 Lambda 函数