linux應用程序開發入門


在上一篇文章中學習了linux字符驅動的開發,需要使用應用程序對完成的驅動進行驗證,現在開始學習應用程序的開發。

一、准備材料

開發環境:VMware
操作系統:ubuntu
開發版:湃兔i2S-6UB

二、man手冊使用

學過編程語言的小伙伴都知道在使用一些函數的時候需要導入相應的庫文件,而函數屬於哪個圖文件都會也相應的API說明文檔,而linux開發也不例外。在linux系統中提供了man手冊,比如我需要查詢printf()這個函數所在的頭文件是那個,只需要linux下使用man 3 printf命令即可查看,如下圖所示

當你不知道使用的函數需要應用什么頭文件時即可通過man手冊進行查詢,具體教程可以參考Linux Man手冊的使用示例

三、main參數

在學習c語言的時候,使用的main的函數都是int main(void),而在linux卻多了兩個變量,原型如下所示

int main(int argc, char *argv[]) {
    return 0;
}

argc:應用程序參數個數
argv[]:具體的參數內容,字符串形式
如果在linux下這樣./helloApp 1執行程序時,那么程序中argc = 1 、 argv[0] = 1。可以通過這樣的形式將執行用戶需要的變量傳入函數中。

四、程序編寫

linux對調用驅動程序時都是通過文件的形式進行操作的,所謂的linux下一切皆文件,需要用到以下函數

int open(const char *pathname, int flags)
int close(int fd)
ssize_t write (int fd, const void * buf, size_t count)
ssize_t read(int fd, void * buf, size_t count)

需要了解函數相應是使用方法,可以通過man手冊進行查詢,准備工作完成后就可以開始應用程序的編寫了。
源碼hello2.c文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


/*
 *argc:應用程序參數個數
 *argv[]:具體的參數內容,字符串形式
 *./hello2App <filename> <1:2> 1表示讀,2表示寫 
 *./hello2App /dev/hello2 1    表示從驅動里面讀數據
 *./hello2App /dev/hello2 2    表示向驅動里面寫數據
 * */

int main(int argc, char *argv[])
{
    int ret = 0;
    int fd = 0;
    char *filename;
    char readbuf[100], writebuf[100];
    static char usrdata[] = {"hell0 This is user data!"};

    if(argc !=3) {
        printf("Instruction usage error!!!\r\n");
        printf("./helle2App <filename> <1:2> 1表示讀,2表示寫\r\n");
        printf("./hello2App ./dev/hello2 1 \r\n");
        return -1;
    }

    filename = argv[1];

    fd = open(filename, O_RDWR);
    if(fd < 0) {

    }

    if(atoi(argv[2]) ==1){
        ret = read(fd, readbuf, 50);
        if(ret <0) {
            printf("read file %s failed!\r\n", filename);
        } else {
            printf("App read data:%s\r\n", readbuf);
        }
    }

    if(atoi(argv[2]) == 2) {
        memcpy(writebuf, usrdata, sizeof(usrdata));
        ret = write(fd,writebuf, 50);
        if(ret <0) {
            printf("write file %s failed\r\n", filename);
        } else {

        }
    }


    ret =close(fd);
    if(ret <0) {
        printf("close file %s falied!\r\n", filename);
    }

    return 0;
}

代碼編寫完成后使用交叉編譯器編譯,編譯完成后將驅動和應用程序都拷貝至arm開發板的/lib/modules/4.1.43+目錄下

arm-linux-gnueabihf-gcc hello2App.c -o hello2App
sudo cp hello2App hello2_demo2.ko /home/rootfs/lib/modules/4.1.43+ -f

五、測試

啟動開發板,進入/lib/modules/4.1.43+目錄,然后加載之前編寫的驅動

modprobe hello_demo2
lsmod
cat /proc/devices


創建屬性節點

mknod /dev/helle2 c 248 0
ls /dev

創建成功后/dev目錄下回生成一個hello2的文件,然后運行應用程序對驅動進行測試

./hello2App /dev/hello2 1
./hello2App /dev/hello2 2


看到如圖信息說明我們編寫的應用程序和驅動都是成功的,測試完成后卸載驅動即可。

rmmod hello_demo2

參考文獻

Linux Man手冊的使用示例:https://www.cnblogs.com/shanyu20/p/10943393.html
linux open函數詳解:https://blog.csdn.net/renlonggg/article/details/80701949
深入理解linux下write()和read()函數:https://blog.csdn.net/boiled_water123/article/details/81951351
正點原子視屏教程:https://www.bilibili.com/video/BV1fJ411i7PB?p=6


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM