Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用带Java的swig时出现不满意的链接错误_Java_Swig_Dynamic Linking_Unsatisfiedlinkerror - Fatal编程技术网

使用带Java的swig时出现不满意的链接错误

使用带Java的swig时出现不满意的链接错误,java,swig,dynamic-linking,unsatisfiedlinkerror,Java,Swig,Dynamic Linking,Unsatisfiedlinkerror,当尝试使用swig在Java中包装一个简单的C程序时,我遇到以下错误: java.lang.UnsatifiedLink错误: /Users/localadmin/example/libexample.dylib: dlopen(/Users/localadmin/example/libexample.dylib,1):没有合适的 找到图像。未找到:/Users/localadmin/example/libexample.dylib: 文件开始太短 使用mac10.9.5、swig3.0.7、j

当尝试使用swig在Java中包装一个简单的C程序时,我遇到以下错误:

java.lang.UnsatifiedLink错误: /Users/localadmin/example/libexample.dylib: dlopen(/Users/localadmin/example/libexample.dylib,1):没有合适的 找到图像。未找到:/Users/localadmin/example/libexample.dylib: 文件开始太短

使用mac10.9.5、swig3.0.7、java1.7

我从以下文件开始:

例c

/* A global variable */
double Foo = 3.0;

/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
    int g;
    g = y;
    while (x > 0) {
        g = x;
        x = y % x;
        y = g;
    }
    return g;
}
例如,我

%module example

%{
// code here is passed straight to example_wrap.c unmodified
extern int gcd(int x, int y);
extern double Foo;
%}

// code here is wrapped:
extern int gcd(int x, int y);
extern double Foo;
runme.java

public class runme {

  static {
    try {
    System.loadLibrary("example");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[]) {
    // Call our gcd() function

    int x = 42;
    int y = 105;
    int g = example.gcd(x,y);
    System.out.println("The gcd of " + x + " and " + y + " is " + g);

    // Manipulate the Foo global variable

    // Output its current value
    System.out.println("Foo = " + example.getFoo());

    // Change its value
    example.setFoo(3.1415926);

    // See if the change took effect
    System.out.println("Foo = " + example.getFoo());
  }
}
我的命令行输入是:

swig -java example.i
gcc -c example.c example_wrap.c -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers
ld -r example.o example_wrap.o -o libexample.dylib
javac *.java
java -Djava.library.path=. runme
然后是错误:

Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.
java.lang.UnsatisfiedLinkError: /Users/localadmin/example/libexample.dylib: dlopen(/Users/localadmin/example/libexample.dylib, 1): no suitable image found.  Did find:
    /Users/localadmin/example/libexample.dylib: file too short

我试着搜索了一堆,但没有成功。非常感谢您的帮助。

在此命令中添加了一个额外的标志:

ld -r -dylib example.o example_wrap.o -o libexample.dylib

必须使用
-dylib
标志才能将文件类型指定为MH_dylib。

但是您是否按照错误消息的建议进行了检查?是的,它们只涉及3个问题-所有问题都与我遇到的错误无关