gpt4 book ai didi

java - UnsatisfiedLinkError 在 Java 中使用 swig

转载 作者:行者123 更新时间:2023-11-30 10:55:55 25 4
gpt4 key购买 nike

当尝试使用 swig 来包装一个简单的 Java C 程序时,出现以下错误:

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

我从以下文件开始:

例子.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;
}

例子.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;

运行程序.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。

关于java - UnsatisfiedLinkError 在 Java 中使用 swig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com