2015-10-16 134 views
2

当尝试使用痛饮包Java中的简单的C程序,我给出以下错误:UnsatisfiedLinkError使用痛饮与Java

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 shortstart

使用Mac 10.9.5,痛饮3.0.7,Java的1.7

我开始与以下文件:

example.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; 
} 

example.i

%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 

我试着搜索了一堆用没有成功。任何帮助表示赞赏。

+0

但你有没有检查[动态链接问题](http://swig.org/Doc2.0/Java.html#Java_dynamic_linking_problems)错误消息建议? – MirMasej

+0

是的,他们只涉及3个问题 - 其中没有涉及我得到的错误 – samT

回答

1

在这个指令添加一个额外的标志:

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

必须需要的-dylib标志指定该文件类型为MH_DYLIB。