【问题标题】:JNI on Mac OS X Undefined symbols error when linking链接时 Mac OS X 上的 JNI 未定义符号错误
【发布时间】:2013-10-19 06:00:06
【问题描述】:

我正在尝试为 mac os x 编译一个 jni 库。如果这很重要,我的系统正在运行 Mountain Lion。我在 xcode 中创建了一个 jni 项目并将源文件复制到项目中。它编译得很好,但有链接错误。 这是错误:

Undefined symbols for architecture x86_64:
  "_init_queue", referenced from:
      _floodfill in floodfill.o
  "_jumpPointSearch", referenced from:
      _Java_com_clashtune_pathfind_Pathfinder_jumpPointSearchNative in main.o
     (maybe you meant: _Java_com_clashtune_pathfind_Pathfinder_jumpPointSearchNative)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我做错了什么?它有四个源文件main.cfloodfill.cjumppointsearch.cqueue.c。我不明白他们在做什么,因为我不是 C 程序员。我只是在为this forum 上的朋友编译它们。

编辑:

这是此项目的项目属性页面“构建阶段”。

谢谢。

【问题讨论】:

  • @Dayalrai 我刚刚使用了 IDE。我不知道命令是什么。
  • @Dayalrai 我不明白你的意思。我已将 JavaVM.framework 添加到框架列表中,并将包含文件夹设置为 JDK 附带的文件夹。
  • @Dayalrai 我添加了Build Phases 属性页的屏幕截图。
  • 项目 -> 构建设置 -> 查找 LLVM 编译器组 -> C++ 标准库。无论如何也请看看here
  • @sandeepupadhyay 是libstdc++ (GNU C++ Standard Library)吗?它给出了同样的错误。

标签: java c java-native-interface linker-errors xcode5


【解决方案1】:

您可以尝试使用超级简单的示例代码。您只需要一个简单的 C 代码和一个可以加载库的 Java 类。

我建议看这里:

http://jnicookbook.owsiak.org/recipe-No-001/

这是一个超级简单的 Hello World 应用程序,您所要做的就是调用简单的 C 代码:

JNIEXPORT void JNICALL Java_recipeNo001_HelloWorld_displayMessage
  (JNIEnv *env, jclass obj) {

  printf("Hello world!\n");

}

来自 Java 代码:

package recipeNo001;

public class HelloWorld {

  /* This is the native method we want to call */
  public static native void displayMessage();

  /* Inside static block we will load shared library */
  static {
    System.loadLibrary("HelloWorld");
  }

  public static void main(String[] args) {
    /* This message will help you determine whether
        LD_LIBRARY_PATH is correctly set
    */
    System.out.println("library: "
      + System.getProperty("java.library.path"));

    /* Call to shared library */
    HelloWorld.displayMessage();
  }
}

如果您使用 XCode,您也应该能够使用命令行工具。看看这里,如何检查你是否安装了它们:

http://jnicookbook.owsiak.org/introduction/

享受 JNI 的乐趣;)

【讨论】:

  • 感谢您的回答,但我已经得到了这个工作。错误是我将它设置为目标 C 作为默认语言。我所要做的就是将它更改为 C++,它就可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 2013-10-22
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
相关资源
最近更新 更多