search
HomeJavaCompiled c++ library function not found

When php editor Youzi is writing a C program, sometimes he may encounter the problem of "the compiled C library function cannot be found". This error usually occurs during the linking phase and means that the compiler cannot find the required library function. There may be many reasons for this problem, such as incorrect configuration of the library file path, improper installation of the library file, etc. In order to solve this problem, we can fix it by checking the library file path, reinstalling the library file, and updating the compiler. In this article, we'll detail how to fix this common compilation error.

Question content

I have a file named libmylibrary.a and a file named mylibrary.h The header file contains a function named myfunction(). I combined them with the cpp file (helloworldjni.cpp) and created a new library called native.dll. However, when I try to compile native.dll in java using jni, I cannot find the functions in libmylibrary.a and mylibrary.h.

mylibrary.h

// mylibrary.h
#ifndef mylibrary_h
#define mylibrary_h

#ifdef __cplusplus
extern "c" {
#endif

void myfunction();  // declare the function prototype

#ifdef __cplusplus
}
#endif

#endif // mylibrary_h

These are the steps I followed

1. Create a java file for integrating jni (helloworldjni.java)

public class helloworldjni {
    static {
        system.load("/home/centos/test5/native.dll");
    }
    public static void main(string[] args) {
        new helloworldjni().sayhello();
    }
  //native method with no body
  public native void sayhello();
}

2.Use this command to create the helloworldjni.h file

javac -h .  helloworldjni.java

3. Integrate the mylibrary.h file and helloworldjni.h file with my cpp file (helloworldjni.cpp)

#include<iostream>
#include<jni.h>
#include "mylibrary.h"
#include "helloworldjni.h"
void sayhello(){
  myfunction();
}
int main() {
    sayhello();  // call the function from the library

    return 0;
}
jniexport void jnicall java_helloworldjni_sayhello
  (jnienv* env, jobject thisobject) {
    sayhello();
}

4. Compile these files (create .o files)

g++ helloworldjni.cpp -l. -lmylibrary -i"/home/centos/jdk-21.0.1/include" -i"/home/centos/jdk-21.0.1/include/linux" -o helloworldjni.o

5. Generate .o file to .dll file

g++ -shared -o native.dll helloworldjni.o

6. Compile and run helloworldjni.java

java helloworldjni

Received this error:

java: symbol lookup error: /home/centos/test5/native.dll: undefined symbol: myFunction

How do I incorporate libmylibrary.a into my java code compilation to ensure it recognizes the functions in libmylibrary.a?

ps: I'm sure there's no problem with libmylibrary.a since I'm able to call myfunction() using pure c but there seems to be some kind of linking issue when using jni that I can't figure out

Workaround

I tried all your steps with some minor changes and it worked for me. Attached build script:

#!/bin/sh

JAVA_SDK_HOME=/home/user/lib/jdk-21.0.1

#echo BUILD THE LIB
#g++ -c mylibrary.cpp -o mylibrary.o
#ar rcs libmylibrary.a mylibrary.o

echo MAKE JAVA STUB
$JAVA_SDK_HOME/bin/javac -h . HelloWorldJNI.java

echo BUILD SO file
g++ -c -fPIC -o HelloWorldJNI.o HelloWorldJNI.cpp \
  -I $JAVA_SDK_HOME/include/ \
  -I $JAVA_SDK_HOME/include/linux

g++ -shared -o native.dll HelloWorldJNI.o -L. -lmylibrary

echo TESTING
$JAVA_SDK_HOME/bin/java HelloWorldJNI


But there are some things to note:

  1. On Windows you will most likely have to set up some macros so jniexport is actually __declspec(dllexport) rather than __declspec(dllimport)
  2. To link *.so files on linux you need to compile the c file using -fpic

The above is the detailed content of Compiled c++ library function not found. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor