Compile the C program through NDK and run it on the iMX6q development board

 In the past, I wanted to compile a C language program into an executable file in the Ubuntu system and run it on the imx6q development board equipped with the Android 6.0.1 system . When compiling using the gcc compiler , although the executable file could be generated, an error occurred. An error occurred, and the final method still failed to run on the board. However, after changing my thinking, I found that executable files can be generated through NDK compilation and can be successfully run on the development board. The problems encountered and solutions are recorded in detail below.

Table of contents

gcc compilation problem

1.File not found

2. Not an executable 64/32-bit program

NDK compilation to solve problems

Put it into the iMX6q development board and run it


gcc compilation problem

1.File not found

When I get a C program file, the first thing I think of is to compile it into an executable file through the gcc compiler. However, when the executable file compiled using the gcc compiler is put on the development board and run, it prompts: No such file or directory , also Just can't find the file

Later I found out that I was using the gcc compiler that I had previously downloaded in the Ubuntu system. This compiler was not the compiler I used to compile the Android system on the development board, so it could not run on the development board and compile the Android image. The compiler is the kernel compiler provided by the development board manufacturer.

2. Not an executable 64/32-bit program

 When I use the compiler that comes with the development board (as shown below) to compile my C program

 After compiling, I ran the executable file on the board, but there was still a problem, prompting me: not excutable: 64-bit ELF file

The development board is 32-bit, but this program is 64-bit, so I compiled the program into 32-bit and used the reference to compile the 32-bit program under 64-bit Linux.

Run the compiled file on the development board and find that although the program has become 32-bit, it is still an unexecutable file. 

NDK compilation to solve problems

Then decide on the conversion method. No longer stick to gcc compilation. Using ndk compilation can also generate executable files, which can also be copied to the development board and run. The specific steps are as follows:

First, place the c program and an Android.mk file in the folder. You only need to put these two files. For the specific method of writing Android.mk, please refer to (NDK Compilation) Detailed explanation of the C/C++ program process compiled using Android.mk

Then open the terminal in the current directory and enter the NDK compilation command

/home/yinlong/Music/sdkapp/android-ndk-r14b-linux-x86_64/android-ndk-r14b/ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=Android.mk

You can generate libs files in the current directory, and there will be many versions of executable files.

The executable file suitable for my imx6q and the above is a 32-bit system is placed in the armeabi-v7a directory.

 You can see the executable file test_app

Put it into the iMX6q development board and run it

Put the above file into the Android device, that is, put it into my development board and run the following command to set the read and write permissions of the file. Note that you must have root permissions .

chmod 777 test_app

Then you can run the executable file compiled by NDK 

./test_app

Guess you like

Origin blog.csdn.net/danielxinhj/article/details/133070695