Android dlopen calls the so Library

Source: Internet
Author: User

From: http://hi.baidu.com/mcu99/blog/item/216f1fce17e1c00b92457edd.html

The following programs are compiled by GCC on the Ubuntu operating system. However, when I want to move them to Android to run, an error occurs. It is unknown where the problem occurs.
(Supplement: the problem has been resolved, see the http://hi.baidu.com/mcu99/blog/item/59ed168014b130df9023d942.html)

************* *************/
# Include <stdio. h>
# Include <dlfcn. h>
Int main ()
{
Int A, B;
Void * P;
INT (* func) (INT, INT );
Scanf ("% d", & A, & B );
P = dlopen ("./dl2.so", rtld_now );
Func = dlsym (P, "Max ");
Printf ("% d compared with % d, % d is a large number. \ N ", A, B, (* func) (a, B ));
Dlclose (P );
}
************* *************/

************* *************/
# Include <stdio. h>
Int max (int x, int y)
{
Return x> Y? X: Y;
}
************* *************/

Compile:
GCC dl2.c-shared-FPIC-O dl2.so
Gcc-O dl1-LDL dl1.c
Appendix: generate dynamic link library-FPIC Problems
Http://hi.baidu.com/han_zhi_xing/blog/item/08f0ef37a50409360a55a9b4.html

Run:
Admin @ admin-desktop:/ABC/test $./dl1
2008 2012
2008 is a large number compared with 2012.

======================================

In Android, I wrote an android. mk to process them.

Local_path: = $ (call my-DIR)
Include $ (clear_vars)

Local_src_files: = \
Dl2.c
Local_prelink_module: = true
Local_cflags =-FPIC
Local_module: = dl2
Include $ (build_shared_library)

Include $ (clear_vars)
Local_src_files: = \
Dl1.c
Local_cflags =-LDL
Local_prelink_module: = true
Local_module: = dl1
Include $ (build_executable)

Then, make dl2 and make dl1 respectively, and then send the compiled dl1 and dl2.so to the same directory in the simulator, but the following error occurs during running.

#./Dl1
3 5
[1] + stopped (signal)./dl1
#
[1] segmentation fault./dl1

I don't know where the problem is ......

 

In this article (http://hi.baidu.com/mcu99/blog/item/216f1fce17e1c00b92457edd.html), I'm calling the dlopen function in Android. the so file fails, so you have to specify it in the compilation process instead. so file method to call, this time in Android is passed.

Create a folder in the (Android source code directory)/development/directory, for example, test123

Create the following three files in the test123 Directory: Test. C, Max. C, and Android. mk.

/***************** Test. C main program ********************/
# Include <stdio. h>
Extern max (INT, INT );
Int main ()
{
Int A = 5, B = 3, C;
C = max (A, B );
Printf ("% d is larger than % d. \ N ", A, B, C );
Return 0;
}

/***************** Max. C will be compiled into a dynamic link library ********************/
Int max (int x, int y)
{
Return x> Y? X: Y;
}

/***************** Android. MK is the MAKEFILE file ********************/
Local_path: = $ (call my-DIR)
Include $ (clear_vars)

Local_src_files: = \
Max. c
Local_prelink_module: = false
Local_module: = libmax
Include $ (build_shared_library)

Include $ (clear_vars)
Local_src_files: = \
Test. c
Local_cflags =-Lmax
Local_ldflags: = \
-Lout/target/product/generic/obj/lib
Local_shared_libraries: = \
Libmax
Local_module: = test
Include $ (build_executable)

After these three files are created, run the CD command to return to the android source code directory and run make test. Note that the folder name is not test123

Reuse commands
ADB push (Android source code directory)/out/target/product/generic/system/bin/test/Data
ADB push out/target/product/generic/system/lib/libmax. So/system/lib
Run the test program and the compiled libmax. so is sent to the corresponding location in the simulator, where libmax. so should be put under/system/lib, or test won't be able to find it, the test program won't matter, it can be put anywhere.

Use the ADB shell to enter the console of the simulator. Of course, you must first start the emulator. Otherwise, the ADB shell will fail.

Enter the directory where test is located and run./test
#./Test
5 is 5 larger than 3.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.