我有一个带有如下任务的makeifile。当我编译时,我得到一个链接器错误,但我不确定为什么因为我" m链接/包含到java libs。我验证了$ JAVA_HOME存在且libjvm.so存在于/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server
犯错:
(.text+0x5a): undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status
生成文件:
my_program:
gcc -L${JAVA_HOME}/jre/lib/amd64/server/ \
-I${JAVA_HOME}/include/ \
-I${JAVA_HOME}/include/linux/ \
-ljvm \
my_program.c
run: my_program
./a.out
c fiel:
#include <jni.h>
int main ( int argc, char **argv ) {
printf("hello\n");
return 0;
}
JNIEnv *create_vm(JavaVM **jvm){
JNIEnv *env = NULL;
JavaVMInitArgs vm_args;
JavaVMOption options;
// options.optionString = "-Djava.class.path=" JAVA_PROG_PATH;
// vm_args.version = JNI_VERSION_1_7;
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
int ret = JNI_CreateJavaVM(jvm, (void **)&env, &vm_args);
char *failed = "nothing";
if(ret < 0){
goto create_vm_error;
}
return env;
lookup_error:
dlog(0, "Error looking up class or method %s.\n", failed);
(**jvm)->DestroyJavaVM(*jvm);
create_vm_error:
*jvm = NULL;
return NULL;
}
答案 0 :(得分:2)
听起来很愚蠢 - 将-ljmv移到my_program.c之后:
my_program:
gcc -L${JAVA_HOME}/jre/lib/amd64/server/ \
-I${JAVA_HOME}/include/ \
-I${JAVA_HOME}/include/linux/ \
my_program.c \
-ljvm
run: my_program
./a.out
这是一个常见错误,因为GCC要求在库规范之前提供编译单元(my_program.c
)(是的,编译单元应严格在库的左侧),这样它就可以告诉它需要从该库中获得哪些符号。如果订单被颠倒,gcc会扣除没有必要的符号,并且不会使用库...
如果每次发生这件事我都有一分钱