1、何为linux系统编程?
linux系统编程也称为linux下的高级编程,它介于应用层与驱动层之间。
一般来说分为三个层面,分别是:应用层(写一个qt程序或者c程序就属于是应用层面)、驱动层(比如写一个蜂鸣器、led等)以及硬件层(与开发板相关的)。
2、linux系统编程基本程序框架
先从最简单的程序开始说起(helloworld.c)

#include <stdio.h>
#include <stdlib.h>int main(void)
{printf("hello world! \n");return 0;
}

根据在嵌入式入门学习中学习到的samba的应用,将该测试程序放置在此电脑的网络位置“ubuntu_samba”处,在ubuntu界面使用gcc编译器编译该测试程序,相关操作如下:

topeet@ubuntu:~$ cd /home/
topeet@ubuntu:/home$ ls
linuxsystemcode  minilinux  samba  topeet  vmware-tools-distrib
topeet@ubuntu:/home$ cd samba/
topeet@ubuntu:/home/samba$ ls
test  test.c
topeet@ubuntu:/home/samba$ sudo mkdir linux+c_test
[sudo] topeet 的密码:
topeet@ubuntu:/home/samba$ ls
linux+c_test  test  test.c
topeet@ubuntu:/home/samba$ cd linux+c_test/
topeet@ubuntu:/home/samba/linux+c_test$ ls
test.c
topeet@ubuntu:/home/samba/linux+c_test$ gcc test.c
/usr/bin/ld:无法打开输出文件 a.out:权限不够
collect2: ld returned 1 exit status
topeet@ubuntu:/home/samba/linux+c_test$ sudo gcc test.c
topeet@ubuntu:/home/samba/linux+c_test$ ls
a.out  test.c
topeet@ubuntu:/home/samba/linux+c_test$ ./a.out
hello world!

在该测试程序下,里面的main函数是没有写参数的,直接写的是void,但是在学习linux系统编程时,main函数大多数情况下是带参数的,因为要配合命令行来使用,要通过命令行来给程序传参数,main函数的两个参数习惯写为“argc”“argv”。
写入参数argc,并打印argc的值

#include <stdio.h>
#include <stdlib.h>int main(int argc,char *argv[])
{printf("argc is %d\n",argc);return 0;
}
topeet@ubuntu:/home/samba/linux+c_test$ ls
a.out  test1.c  test.c
collect2: ld returned 1 exit status
topeet@ubuntu:/home/samba/linux+c_test$ sudo gcc test1.c -o test1
topeet@ubuntu:/home/samba/linux+c_test$ ls
a.out  test1  test1.c  test.c
topeet@ubuntu:/home/samba/linux+c_test$ ./test1
argc is 1

为什么ubuntu界面的打印值为1呢?“argc参数是指命令行中参数的个数”,因为文件名本身也算是一个参数,所以执行程序之后会打印1。
在执行test1后面再输入一个参数,则执行结果为2;再输入三个参数,则执行结果为4;

topeet@ubuntu:/home/samba/linux+c_test$ ./test1 2
argc is 2
topeet@ubuntu:/home/samba/linux+c_test$ ./test1 2 3 q
argc is 4

写入参数argv,并打印argv的值。“argv给出的是命令行中的参数”。注意:此处需要重新编译一下改写过的程序,否则执行结果仍然是未修改过的。

#include <stdio.h>
#include <stdlib.h>int main(int argc,char *argv[])
{printf("argc is %d\n",argc);printf("argv is %s\n",argv[0]);return 0;
}
topeet@ubuntu:/home/samba/linux+c_test$ sudo gcc test1.c -o test1
topeet@ubuntu:/home/samba/linux+c_test$ ls
a.out  test1  test1.c  test.c
topeet@ubuntu:/home/samba/linux+c_test$ ./test1
argc is 1
argv is ./test1
#include <stdio.h>
#include <stdlib.h>int main(int argc,char *argv[])
{printf("argc is %d\n",argc);printf("argv is %s\n",argv[0]);printf("argv is %s\n",argv[1]);return 0;
}
topeet@ubuntu:/home/samba/linux+c_test$ sudo gcc test1.c -o test1
topeet@ubuntu:/home/samba/linux+c_test$ ./test1
argc is 1
argv is ./test1
argv is (null)
topeet@ubuntu:/home/samba/linux+c_test$ ./test1 1
argc is 2
argv is ./test1
argv is 1
topeet@ubuntu:/home/samba/linux+c_test$ ./test1 2
argc is 2
argv is ./test1
argv is 2
topeet@ubuntu:/home/samba/linux+c_test$ ./test1 2 3
argc is 3
argv is ./test1
argv is 2

再引入一个新的问题,输入的参数最多可以是多少呢?原则上是没有上限的,使用for循环来写一下代码病在ubuntu上运行

#include <stdio.h>
#include <stdlib.h>int main(int argc,char *argv[])
{int i;printf("argc is %d\n",argc);for(i=0;i<argc;i++){printf("argv[%d] is %s\n",i,argv[i]);}return 0;
}
topeet@ubuntu:/home/samba/linux+c_test$ sudo gcc test2.c -o test2
topeet@ubuntu:/home/samba/linux+c_test$ ls
a.out  test1  test1.c  test2  test2.c  test.c
topeet@ubuntu:/home/samba/linux+c_test$ ./test2
argc is 1
argv[0] is ./test2
topeet@ubuntu:/home/samba/linux+c_test$ ./test2 0 1 2 3 4 5 6 7
argc is 9
argv[0] is ./test2
argv[1] is 0
argv[2] is 1
argv[3] is 2
argv[4] is 3
argv[5] is 4
argv[6] is 5
argv[7] is 6
argv[8] is 7

3、上述是在ubuntu上进行的linux系统编程,那如何在开发板上进行系统编程呢?
在ubuntu上使用的是gcc编译的,gcc编译出来的程序是x86的,可以使用file命令来查看一下文件类型

topeet@ubuntu:/home/samba/linux+c_test$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=326c9cc80a7889d122a117b45fcff632851faf5d, not stripped
topeet@ubuntu:/home/samba/linux+c_test$ file test2
test2: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=56b1a99ef89a287ce64c53ed6082b95f419566fa, not stripped

要使用gcc编译器的话,只能在ubuntu上运行,如果想在开发板上运行,就需要用到交叉编译以及交叉编译器。切换到root用户,输入arm命令,按下tab键,查看交叉编译器,使用交叉编译器静态编译文件,命名为test22,此时再使用file命令查看文件类型,可看到类型为arm,说明test22文件可以在开发板上运行啦

topeet@ubuntu:/home/samba/linux+c_test$ cd ..
topeet@ubuntu:/home/samba$ arm
arm       arm2hpdl
topeet@ubuntu:/home/samba$ su root
密码:
root@ubuntu:/home/samba# arm
arm                               arm-none-linux-gnueabi-gcc        arm-none-linux-gnueabi-objdump
arm2hpdl                          arm-none-linux-gnueabi-gcc-4.4.1  arm-none-linux-gnueabi-ranlib
arm-none-linux-gnueabi-addr2line  arm-none-linux-gnueabi-gcov       arm-none-linux-gnueabi-readelf
arm-none-linux-gnueabi-ar         arm-none-linux-gnueabi-gdb        arm-none-linux-gnueabi-size
arm-none-linux-gnueabi-as         arm-none-linux-gnueabi-gdbtui     arm-none-linux-gnueabi-sprite
arm-none-linux-gnueabi-c++        arm-none-linux-gnueabi-gprof      arm-none-linux-gnueabi-strings
arm-none-linux-gnueabi-c++filt    arm-none-linux-gnueabi-ld         arm-none-linux-gnueabi-strip
arm-none-linux-gnueabi-cpp        arm-none-linux-gnueabi-nm
arm-none-linux-gnueabi-g++        arm-none-linux-gnueabi-objcopy
root@ubuntu:/home/samba# arm-none-linux-gnueabi-gcc test2.c -o test22 -static
arm-none-linux-gnueabi-gcc: test2.c: No such file or directory
arm-none-linux-gnueabi-gcc: no input files
root@ubuntu:/home/samba# ls
linux+c_test  test  test.c
root@ubuntu:/home/samba# cd linux+c_test/
root@ubuntu:/home/samba/linux+c_test# arm-none-linux-gnueabi-gcc test2.c -o test22 -static
root@ubuntu:/home/samba/linux+c_test# ls
a.out  test1  test1.c  test2  test22  test2.c  test.c
root@ubuntu:/home/samba/linux+c_test# file test22
test22: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped

将编译成的可在开发板执行的文件test22拷贝至U盘,启动开发板,插入U盘,加载U盘,如下图执行该文件,可都得到执行结果。

总结:
还是要说明一点,并不是说使用gcc编译器编译出来的程序就可以直接在开发板上运行,那个程序只能在x86上运行,而不能在arm开发板上面运行。要想在开发板上运行,需要将其转化为可执行文件,进行交叉编译!!!

嵌入式学习之linux系统编程----1 了解linux编程并且在ubuntu以及ARM上运行测试程序相关推荐

  1. linux进程创建心得体会,Linux系统下计算机C语言编程心得体会

    摘    要:C语言编程是目前计算机领域中应用较为广泛的编程语言, 很多信息系统都基于C语言编程实现.Linux系统是免费且可以自由传播的操作系统, 支持多用户.多平台应用, 实际生活中应用也较为广泛 ...

  2. linux 版本的scipy,Linux 系统中 SciPy (Python 3) 编程环境

    Linux 系统中 SciPy (Python 3) 编程环境 SciPy (pronounced "Sigh Pie") is a Python-based ecosystem ...

  3. LINUX系统程序设计中C/C++编程工具使用

    LINUX系统程序设计中C/C++编程工具使用 一.实验目的 掌握gcc编译器的使用 掌握gdb调试器的使用 掌握make工程管理器的使用 理解Makefile文件的格式,掌握Makefile文件的编 ...

  4. Linux 系统中 SciPy (Python 3) 编程环境

    Linux 系统中 SciPy (Python 3) 编程环境 SciPy (pronounced "Sigh Pie") is a Python-based ecosystem ...

  5. Linux系统-小倒腾之Linux DIY定制裁剪(New kernel+Busybox)o_o(二)

    在前面的<Linux系统-小倒腾之Linux DIY定制裁剪(附带简单网络功能)o_o(一)>博文中,我通过对一个Linux系统的裁剪,最终制作出来了一个带有网络功能的微型Linux系统; ...

  6. linux系统查看串口占用,Linux 系统串口信息查看

    先确认系统启动的时候串口的信息. ECM_5412@chenfl:~$ dmesg | grep tty [ 0.000000] console [tty0] enabled [ 2.511678] ...

  7. linux系统charp命令,操作系统 Linux系统

    Linux操作系统 预备知识(Operating System): 1.GNU项目,GNU项目是一个自由软件项目,旨在建立一个完全自由开放通用的UNIX软件平台. 2.操作系统:管理计算机硬件和软件资 ...

  8. php 登录 linux服务器,如何实现linux系统远程登录到linux服务器

    linux系统登录到远程linux服务器: 有多种不同的协议可供选择,也许SSH是"最好"的.SSH是secure shell的简写,意为"安全的shell". ...

  9. Linux系统磁状态检测,检测Linux硬件状态

    计算机系统是由软件系统和硬件系统共同组成的.检测硬件状态对于保障整个系统的稳定是非常重要的.不论操作系统是使用Linux.还是Windows,一旦硬件出现故障,那么整个系统的安全就严重了.这里我们主要 ...

最新文章

  1. 四层和七层交换技术-loadbalance
  2. 监督学习和无监督学习
  3. Vlan间路由实验(单臂路由实现法)
  4. VTK:网格之Decimation
  5. 机器学习基础-逻辑回归-09
  6. 中石油训练赛 - Historical Maths(二分)
  7. PX4的workqueue
  8. 解决:TypeError: Value passed to parameter 'a' has DataType int64 not in list of allowed values: float1
  9. java函数的参数传递
  10. Bash中单引号和双引号之间的区别
  11. (三.0)通过FPGA实现以太网通信原理及理解
  12. 【笔记】人工智能 一种现代方法 第1章 绪论
  13. 防火墙之ASDM配置虚拟专用网络实践
  14. 根据经纬度算距离 | SQL
  15. kibana报错-Login is currently disabled.
  16. 2022年第二十三届华东杯大学生数学建模竞赛
  17. 最好的Linux文件管理器,适用于 Linux 用户的 10 款优秀且免费的文件管理器
  18. your cuda software stack is old.we fall back to the NIVIDIA driver for some compilation. Update your
  19. 全员全域安全守护,蔚来ET7获Euro NCAP五星安全评级背后的硬核实力
  20. 一寸照片压缩到20k_师大er:关于证件照你需要知道这些干货!(/^^)/ 顺便免费冲印高清照片~...

热门文章

  1. [茶余饭后]10大毕业生必听得歌曲
  2. html之code标签和pre标签
  3. 项目经理如何准确地跟踪项目进度
  4. PPP简介,PPP分层体系架构,PPP链路建立过程及PPP的帧格式
  5. 哪款蓝牙耳机性价比比较高,四款防水效果比较好的蓝牙耳机
  6. Java实现三个维度的跑马灯(数组)
  7. 微软 Lumia 950 XL 被成功刷入 Android 12 系统
  8. Forword和Redirect的区别
  9. 2021-08-11 WPF控件专题 ListView控件详解
  10. css text-align属性,行内元素,块级元素居中详解