Table of Contents
Question content
Workaround
Home Java Compiled c++ library function not found

Compiled c++ library function not found

Feb 08, 2024 pm 11:57 PM
Compile Error lsp

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
Copy after login

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();
}
Copy after login

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

javac -h .  helloworldjni.java
Copy after login

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();
}
Copy after login

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
Copy after login

5. Generate .o file to .dll file

g++ -shared -o native.dll helloworldjni.o
Copy after login

6. Compile and run helloworldjni.java

java helloworldjni
Copy after login

Received this error:

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

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


Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to adjust the font size on Redmi Note13RPro? How to adjust the font size on Redmi Note13RPro? May 07, 2024 pm 06:34 PM

Do not change the meaning of the original content, fine-tune the content, rewrite the content, do not continue. Redmi Note13RPro not only has excellent performance, but also has an even better user experience. In order to give everyone a more comfortable experience, Redmi Note13RPro is equipped with a font adjustment function to allow users to Ability to adjust fonts for your phone. If you want to know how to adjust the font size of Redmi Note13R Pro, then take a look. How to adjust the font size on Redmi Note13RPro? 1. Open the Settings app. 2. Scroll to the bottom and select "Display." 3. Click "Text Size". 4. On this screen, you can adjust the font size by dragging the slider or select a preset font size. 5. After the adjustment is completed, press "OK" to save

How to extract text from pictures on Redmi Note13RPro? How to extract text from pictures on Redmi Note13RPro? May 08, 2024 pm 10:00 PM

The Redmi Note13RPro mobile phone integrates a number of smart tools in terms of software functions. Among them, quickly and accurately extracting text content from pictures is one of them. The following editor will introduce to you how Redmi Note13RPro extracts text from pictures. How to extract text from pictures on Redmi Note13RPro? Use the Xiaomi QR code scanning function, open the QR code scanning application on your phone, click the picture icon, select a picture, and then click the "Recognize text" option on the right to successfully extract the text in the picture. Operate through mobile phone album. Find the picture for which text needs to be extracted in the mobile phone album, click "More" below the picture, and select "Extract Text". After successful recognition, you can copy or save the text as needed. Use WeChat mini programs. Open micro

How to connect Redmi Note13RPro to the computer? How to connect Redmi Note13RPro to the computer? May 09, 2024 pm 06:52 PM

The phone Redmi Note13RPro has been very popular recently. Many consumers have purchased this phone. However, many users are using this phone for the first time, so they don’t know how to connect the Redmi Note13RPro to the computer. In this regard, the editor is here to explain to you Detailed tutorial introduction is provided. How to connect Redmi Note13RPro to the computer? 1. Use a USB data cable to connect the Redmi phone to the USB interface of the computer. 2. Open the phone settings, click Options, and turn on USB debugging. 3. Open the device manager on your computer and find the mobile device option. 4. Right-click the mobile device, select Update Driver, and then select Automatically search for updated drivers. 5. If the computer does not automatically search for the driver,

A complete guide to all levels of 'Whole Life' A complete guide to all levels of 'Whole Life' May 07, 2024 pm 06:31 PM

Entire Huoba is a very fun casual puzzle game that everyone can experience on multiple platforms! The game provides countless exciting levels waiting to be unlocked. Each level is full of novelty and challenges, allowing you to experience an interesting adventure of answering questions and unlock new game content. So how to clear the entire level of Huoba? Today I bring you a complete guide to all levels of "Whole Life". If you encounter any problems in passing the level, you can refer to it! "The Whole Life" complete guide to all levels of "The Whole Life" The complete guide to all levels of "The Whole Life" Help her to watch the New Year's Eve, help the little sister to identify the scumbag wolf, and the grandmother helps the little girl escape from danger for outdoor adventure, pretending not to see Ah Piao and transforming back The humanoid helped the orangutan to become a humanoid. Someone in the back seat stayed calm and dealt with the female ghost. The night-shift taxi kept calm and dealt with the female ghost.

List of Stardust Memory Weapons in 'Dust White Forbidden Zone' List of Stardust Memory Weapons in 'Dust White Forbidden Zone' May 08, 2024 pm 04:22 PM

How about Stardust Memories in "Dust White Forbidden Zone"? In the game "Chenbai Forbidden Zone", there are many powerful weapons for players to choose from. Let's follow the editor to learn more about the detailed attributes of the Stardust Memory weapons in the "Chenbai Forbidden Zone". I hope it can be helpful to players. "Dust White Forbidden Zone" Stardust Memories Weapon Introduction List Weapon Information Weapon Introduction Name Stardust Memories Level Five-star Type Submachine Gun Element Low Temperature Fire Rate 950 Ammo Capacity 50 Reload Speed ​​1.35 Weapon Skills: Snow Suffering Low Temperature Damage Increased by 18%/30%; After using a regular skill, you will immediately gain 6 layers of [Planetary Ice] effect. The damage caused by the regular skill will gain 1 layer of [Planetary Ice]. The trigger interval is 2 seconds; each layer of [Planetary Ice] will increase the damage of regular skills by 3%/ 5%, can be stacked 12 times for 3 seconds.

'Whole Life' guide to saving neighbors and urging all neighbors to go downstairs 'Whole Life' guide to saving neighbors and urging all neighbors to go downstairs May 07, 2024 pm 07:00 PM

Entire Huoba is a very popular fun level-breaking game on Douyin. There are many levels in the game waiting for everyone to challenge! In the game, you need to persuade all the neighbors to go downstairs in the level "Rescue the Neighbors". How do you pass the level? The following is the guide for saving the neighbors and urging all the neighbors to go downstairs to pass the level brought to you by the editor. Friends who don’t know how to pass the level, let’s take a look together. I hope it can help you. "Whole Life" Saving the Neighbors and Advising All the Neighbors to Go Downstairs Clearance Strategy Saving the Neighbors and Encouraging All the Neighbors to Go Downstairs 1. Someone downstairs needs first aid 2. The bookstore downstairs sells out-of-print learning materials 3. There are many cardboard boxes in the trash can downstairs 4. I Agree to the date, you go downstairs first 5. Your brother came to our community to promote 6. Someone discovered a new fishing spot 7. Drag the canned cat downstairs 8. Drag the fork to

VESA announces DisplayHDR 1.2 specification, improving performance requirements: increasing color accuracy, wide color gamut, etc. VESA announces DisplayHDR 1.2 specification, improving performance requirements: increasing color accuracy, wide color gamut, etc. May 08, 2024 pm 02:13 PM

According to news from this website on May 8, the Video Electronics Standards Association (VESA) issued a press release today to announce the DisplayHDR1.2 specification and stated that there are currently more than 3,000 display models in the world that have passed the DisplayHDR standard certification. The DisplayHDR1.2 version specification brings more stringent performance requirements. In addition to brightness, color gamut and bit depth, it also introduces color accuracy, contrast, and black level (blacklevel, which refers to the fact that on a display device that has undergone certain calibration, there is not a single line. Multiple new test requirements for video signal levels of light output) and subtitle flicker. DisplayHDRv1.2 improves the white brightness level test, changing the 10% center patch test on a black background to a non-black background

How to add widgets to Redmi Note13RPro? How to add widgets to Redmi Note13RPro? May 07, 2024 pm 07:55 PM

Redmi Note13RPro, which has just been launched by Redmi, is very popular. It has achieved very good sales just after it was launched. Many consumers have bought this phone. In order to let everyone better use Redmi Note13RPro, the editor is here to bring it to you Here is an introduction to how to add widgets to Redmi Note13R Pro. How to add widgets to Redmi Note13RPro? 1. Click Add Widget. Press and hold the desktop and click the Add Widget option. 2. Click the component name to select the name of the widget that needs to be added. 3. Click the Add to Desktop option below the Add to Desktop selection. 4. Click OK and select the OK icon in the upper right corner. FAQ Connect Bluetooth Change input method theme Change factory settings Hide apps Mirror TV