2015-10-16 2 views
2

При попытке использовать глотнул, чтобы обернуть простую программу C в Java я дал следующее сообщение об ошибке: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, Swig 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.

Смежные вопросы