Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined symbols for architecture x86_64: JNI_CreateJavaVM OS-X Xcode

I am attempting to compile the following C++ code for creating a Java Virtual Machine, using Xcode on Mac OS-X Mountain Lion:

JNIEnv * createVM(JavaVM **jvm){
    JNIEnv * retEnv = NULL;
    JavaVMInitArgs vm_args = *new JavaVMInitArgs();
    JavaVMOption *options = new JavaVMOption[1];

    std::string sJavaInstallPath = "-Djava.class.path=" + findJavaPath();

    if(sJavaInstallPath == "-Djava.class.path="){
        return NULL;
    }

    options[0].optionString = const_cast<char*>(sJavaInstallPath.c_str());
    vm_args.version = JNI_VERSION_1_6;
    vm_args.nOptions = 1;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = false;

    long status = JNI_CreateJavaVM(jvm, (void**)&retEnv, &vm_args);

    if(status == JNI_ERR){
        std::cout << "Failure: Unable to load JVM \t Exit" << std::endl;
    }else if(status == JNI_OK){
        std::cout << "CreateVM:\t\tJVM loaded successfully" << std::endl;
    }

    delete options;
    return retEnv;
};

Which gives the following error: Undefined symbols for architecture x86_64: "_JNI_CreateJavaVM".

From these similar questions:


undefined reference to `JNI_CreateJavaVM' linux

How can I use JNI in C++ to use a Java class?

undefined symbol: JNI_CreateJavaVM in Linux

http://lists.apple.com/archives/java-dev/2005/Apr/msg00067.html

http://lists.apple.com/archives/java-dev/2005/Apr/msg00068.html


I gather that I'm missing a Library reference to libjvm.dylib. The problem is that I'm not sure how to get Xcode to recognize/include the library for linking,

I've tried putting the following directories (all of which contain a libjvm.dylib file) into the Library Search Paths list in Xcode, with no result:


/System/Library//Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries/

/Library//Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/server/

/System/Library/Frameworks/JavaVM.framework/Libraries/


I'm still quite new to OS-X and Xcode, and the answers to the other questions all relate to compiling using gcc or g++, which I have even less experience with, so what I need to know is: how do I tell the Xcode compiler where to find libjvm.dylib, and also which libjvm.dylib is the one I should be linking to?

like image 521
Fledges Avatar asked Mar 14 '14 05:03

Fledges


1 Answers

OK, found the solution,

the equivalent of g++'s

-L<library/to/link>

setting in Xcode is the Link Binary with Libraries setting under the Build Phases tab, not the Library Search Paths list that I mentioned above.

and the library I needed to link to was actually /System/Library/Frameworks/JavaVM.framework/JavaVM,

not libjvm.dylib

like image 161
Fledges Avatar answered Oct 13 '22 08:10

Fledges