如何调试这个Makefile

发布于 2025-01-08 09:30:28 字数 2641 浏览 0 评论 0原文

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

这是 makefile 的内容:

#!/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;
}

祝愿 彼得

I have a C makefile which generates header file from java code and compiles very simple C executable file. There is an error which I can't find. When I try to run the code I get UnsatisfiedLinkError.

This is the content of the makefile:

#!/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) 
lt;

# 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

Here is the content of the C file:

#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;
}

Best wishes
Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2025-01-15 09:30:29

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

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

编辑:

而不是 ../../../target/native 使用 NB_27-impl/target/classes/lib 的绝对路径。

UnsatisfiedLinkError means the JVM failed to load the native shared object that the makefile produced, or .so that the native shared object depends on.

Adding absolute path of ../../../target/native to LD_LIBRARY_PATH will enable the JVM to locate the .so. If the .so depends on other .so libraries the directories where these .so exist will also need added to LD_LIBRARY_PATH.

EDIT:

Instead of ../../../target/native use absolute path of NB_27-impl/target/classes/lib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文