gpt4 book ai didi

java - 如何调试这个 Makefile

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:43 25 4
gpt4 key购买 nike

我有一个 C 生成文件,它从 java 代码生成头文件并编译非常简单的 C 可执行文件。有一个我找不到的错误。当我尝试运行代码时,出现 UnsatisfiedLinkError。

这是生成文件的内容:

#!/usr/bin/make -f
#
# Makefile for native stuff
#

# c files to compile
C_SOURCES := sqrt.c

# the name of the library to build
LIBNAME := sqrt

C_SOURCE_DIR := src
C_GENSOURCE_DIR := src

TARGET_DIR := ../../../target
C_BUILD_DIR = $(TARGET_DIR)/native
JAVA_BUILD_DIR = $(TARGET_DIR)/classes

# the name of the file we build
TARGET = $(JAVA_BUILD_DIR)/META-INF/lib/$(LIB_PREFIX)$(LIBNAME)$(LIB_EXTN)

# find the jdk. if this doesn't work for you, define JAVA_HOME in your
# environment or on the make command line
JAVA_HOME ?= /opt/jdk1.7.0_02

# classpath for javah
JAVAH_CLASSPATH = `cat $(TARGET_DIR)/compile-classpath`

# tools and options
CFLAGS = -Wall -fpic
CPPFLAGS = -I$(C_SOURCE_DIR) -I$(C_GENSOURCE_DIR) -Iinclude \
-I$(JAVA_HOME)/include
JAVAH = /opt/jdk1.7.0_02/bin/javah
JAVAH_FLAGS += -classpath $(JAVAH_CLASSPATH)
JAVAH_CMD = $(JAVAH) $(JAVAH_FLAGS) $(OUTPUT_OPTION)
LDFLAGS = -shared
LINK.so = $(CC) $(LDFLAGS) $(LD_LIBS)

ifdef DEBUG
CFLAGS += -g
LDFLAGS += -g
endif

# os-dependent bits
UNAME := $(shell uname)

ifeq ($(UNAME),Linux)
LIB_PREFIX = lib
LIB_EXTN = .so
CPPFLAGS += -I$(JAVA_HOME)/include/linux
else
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
LIB_PREFIX =
LIB_EXTN = .dll
CPPFLAGS += -I$(JAVA_HOME)/include/win32
else
f := $(error Platform $(UNAME) not supported)
endif
endif

# we look in $(C_SOURCE_DIR) for c files...
vpath %.c $(C_SOURCE_DIR)

# convenience variables
C_OBJFILES = $(addprefix $(C_BUILD_DIR)/,$(subst .c,.o,$(C_SOURCES)))

# default target
all: $(TARGET)

# rule to compile the .c files
$(C_BUILD_DIR)/%.o: %.c
@mkdir -p `dirname $@`
$(COMPILE.c) $(OUTPUT_OPTION) $<

# link the C objects into a shared library
$(TARGET): $(C_OBJFILES) $(LDLIBS)
@mkdir -p `dirname $@`
$(LINK.so) $(OUTPUT_OPTION) $^

# a rule to build the .h file with javah
$(C_GENSOURCE_DIR)/org_DX_57_osgi_NB_27_impl_Sqrt.h: $(JAVA_BUILD_DIR)/org/DX_57/osgi/NB_27/impl/Sqrt.class
rm -f $@
$(JAVAH) $(JAVAH_FLAGS) $(OUTPUT_OPTION) org.DX_57.osgi.NB_27.impl.Sqrt

# the .o file depends on the .h file
$(C_BUILD_DIR)/sqrt.o: $(C_GENSOURCE_DIR)/org_DX_57_osgi_NB_27_impl_Sqrt.h

clean::
rm -f $(C_OBJFILES)
rm -f $(TARGET)
rm -f $(C_BUILD_DIR)/jnirules.mak

C文件的内容如下:

#include "org_DX_57_osgi_NB_27_impl_Sqrt.h"

JNIEXPORT jdouble JNICALL Java_Sqrt_sqrt
(JNIEnv *env, jclass cls, jdouble d, jdouble tol)
{

return tol;
}

最美好的祝愿彼得

最佳答案

UnsatisfiedLinkError 表示 JVM 无法加载 makefile 生成的 native 共享对象,或者 native 共享对象所依赖的 .so

../../../target/native 的绝对路径添加到 LD_LIBRARY_PATH 将使 JVM 能够找到 .so。如果 .so 依赖于其他 .so 库,则这些 .so 所在的目录也需要添加到 LD_LIBRARY_PATH.

编辑:

使用 NB_27-impl/target/classes/lib 的绝对路径代替 ../../../target/native

关于java - 如何调试这个 Makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9421829/

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