Download
1 / 161

第八章 LINUX 编程基础 - PowerPoint PPT Presentation


  • 118 Views
  • Uploaded on
  • Presentation posted in: General

第八章 LINUX 编程基础. ◆ 熟悉嵌入式 Linux 系统开发流程 ◆ 掌握构建交叉工具链的方法 ◆ 熟悉 VMWare 与 RedHat9.0 的安装与使用以及配置宿主机的方法 ◆ 熟悉 Shell 与 Makefile 基本用法。. 8 .1 交叉开发环境的建立. 1. 嵌入式 Linux 系统开发流程 嵌入式 Linux 系统的软件结构. 嵌入式 Linux 系统开发流程. 2. 交叉编译概述

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha

Download Presentation

第八章 LINUX 编程基础

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Linux

LINUX


Linux

Linux

VMWareRedHat9.0

ShellMakefile


Linux

.1

1. Linux

Linux


Linux

Linux


Linux

2.

PC


Linux

libc cglibc

uClibc dietlibc newlib


Linux

GNU cglibc

http://www.gnu.orgGNULinux

Binutils

GCC

GLIBC

GDB


Linux

cglibcgccg++arasldglibcgdb

arm-linux-gccLinuxgccLinuxLinux


Linux

1 cglibcBinutils

Binutilsbinutils


Linux

2 cglibcGCC

GCC(GNU Compiler Collection)gccC/C++FORTRAN JAVA ADAC/C++glibcGCC


Linux

cglibcGCC,:

1(Pre-Processing)

2(Compiling)

3(Assembling)

4(Linking)


Linux

gcc cglibc

gcc [options] [filenames]

options

filenames:


Linux

cglibc

gcc100

-o output_filenameoutput_filenamegcca.out

-c.c.o


Linux

-g cglibc(GNUgdb)

-O

-O2-O


Linux

3 cglibcGLIBC

GlibcCGCCglibc/lib/usr/libGlibc


Linux

4GDB cglibc

GNUGDBGNU DebugGDBCC++GDB

1

2

3

GDB

gdb

gdb helloworld


Linux

cglibc

#include<stdio.h>

void main()

{

int i;

long result=0;

for(i=1;i<=100;i++)

{

result+=i;

}

printf("result=%d\n",result);

}


Linux

1 cglibc

gcc-g tst.c

2GDB

gdb tst

3main

break main

4

run

5

next

6

Continue


Linux

5 cglibc Crosstool

Crosstoolgccglibccrosstoolgccglibc


Linux

Binutils cglibcgccglibcBUG

gcc2.95.xLinux2.4

Linux2.6Linux2.6gcc3.3


Linux

8.1.3 cglibc

GNU


Linux

GCCGCCGNUCPU-PLATFORM-OS

x86/i386 i686-pc-linux-gnu

ARMarm-linux-gnu


Linux

3 GCCGCCGNUCPU-PLATFORM-OS


Linux

GCCGCCGNUCPU-PLATFORM-OS

Crosstool


Linux

GCCGCCGNUCPU-PLATFORM-OS

ftp.arm.kernel.org.uk


Linux

1. GCCGCCGNUCPU-PLATFORM-OS

8.1.7


Linux

1 GCCGCCGNUCPU-PLATFORM-OS

# cd /my2410

# mkdir armlinux


Linux

armlinux3 build-toolskernel tools

# cd armlinux

# mkdir build-tools kernel tools

1build-toolsbinutilsgccglibc

2kernel

3tools


Linux

2 build-toolskernel tools

:


Linux

# export PRJROOT=/home/mike/armlinux build-toolskernel tools

# export TARGET=arm-linux

# export PREFIX=$PRJROOT/tools

# export TARGET_PREFIX=$PREFIX/$TARGET

# export PATH=$PREFIX/bin:$PATH

export ,export

bashrcexport


Linux

3 build-toolskernel toolsBinutils

BinutilsGNU

Binutilsaddr2linearasc++filtgprofldnmobjcopyobjdumpranlibreadelfsizestringsstriplibibertylibbfdlibopcodes


Linux

Binutils build-toolskernel tools

BashCoreutilsDiffutilsGCCGettextGlibcGrepMakePerlSedTexinfo


Linux

build-toolskernel toolsbinutils-2.15

binutils-2.15.tar.bz2

# cd $PRJROOT/build-tools

# tar xjvf binutils-2.15.tar.bz2


Linux

Binutils Binutils

# cd $PRJROOT/build-tools

# mkdir build-binutils

# cd build-binutils

# ../ binutils-2.15/configure --target=$TARGET --prefix=$PREFIX


Linux

Binutils

target arm-linux

--prefix check Makefile

make

# make

# make install


Linux

/my2410/armlinux/tools/binBinutils

# ls $PREFIX/bin

arm-linux-addr2line arm-linux-ld arm-linux-ranlib arm-linux-strip arm-linux-ar arm-linux-nm arm-linux-readelf arm-linux-as arm-linux-objcopy arm-linux-size arm-linux-c++filt

arm-linux-objdump arm-linux-strings


Linux

4 /my2410/armlinux/tools/binBinutils

Linux


Linux

/my2410/armlinux/tools/binBinutilsarm-linux

1kernellinux-2.6.30.4.tar.gz

# cd $PRJROOT/kernel

# tar xvzf inux-2.6.30.4.tar.gz

2

# cd linux-2.6.30.4

# make ARCH=arm CROSS_COMPILE=arm-linux- menuconfig


Linux

/my2410/armlinux/tools/binBinutils

ARCH=armarm

CROSS_COMPILE=arm-linux-arm-linux-

configxconfigmenuconfigmake menuconfig

S3C2410System Type->ARM System Type->SMDK2410/ARM920T


Linux

/my2410/armlinux/tools/binBinutils8.1.5 Linux 2.6.30


Linux

/my2410/armlinux/tools/binBinutils

include/linux/version.hinclude/linux/autoconf.hglibcversion.h autoconf.h


Linux

/home/mike/armlinux/tools/arm- linuxinlcude

# mkdir p $TARGET_PREFIX/include

# cp r $PRJROOT/kernel/linux-2.6.30.4/include/linux $TARGET_PREFIX/include

# cp r $PRJROOT/kernel/linux-2.6.30.4/include/asm-arm $TARGET_PREFIX/include/asm


Linux

5) linuxinlcudeboot-trap gcc

arm-linux-gccgccglibcBootLoaderC

CCBootLoader


Linux

linuxinlcude

# cd $PRJROOT/build-tools

# tar xvzf gcc-3.3.6.tar.gz

# mkdir build-gcc

# cd gcc-3.3.6

# vi gcc/config/arm/t-linux


Linux

linuxinlcudeARMlibcgcc/config/arm/t- linux TARGET_LIBGCC2_CFLAGS-Dinhibit_libc -D__gthr_ posix_h/usr/inlcude


Linux

TARGET_LIBGCC2-CFLAGS = -fomit-frame-pointer fPIC

TARGET_ LIBGCC2- CFLAGS=-fomit-frame-pointerfPIC -Dinhibit_libc -D__gthr_posix_h

t-linux


Linux

fPIC

# cd build-gcc

# ./ build-gcc /configure --target=$TARGET --prefix=$PREFIX --enable-languages=c

--disable-threads --disable-shared

--enable-languages=cC

--disable-threadsthreadglibc

--disable-shared


Linux

fPIC

# make

# make install

/home/mike/armlinux/tools/binarm-linux-gccboot-trap gcc


Linux

6) fPICglibc

glibcGUN CLinux

glibc-2.3.2

(1) GNU make 3.79

(2) GCC 3.2

(3) GNU binutils 2.13


Linux

fPICglibc-2.2.3.tar.gzglibc-linuxthreads-2.2.3.tar.gz

# cd $PRJROOT/build-tools

# tar -xvzf glibc-2.2.3.tar.gz

# tar -xzvf glibc-linuxthreads-2.2.3.tar.gz --directory=glibc-2.2.3


Linux

fPICglibc-2.2.3glibc-2.2.3$PRJROOT/build-toolsbuild-glibc

# cd $PRJROOT/build-tools

# mkdir build-glibc

# cd build-glibc

# CC=arm-linux-gcc ../glibc-2.2.3 /configure -- host=$TARGET --prefix="/usr"--enable-add-ons --with-headers=$TARGET_PREFIX/include


Linux

fPICCC=arm-linux-gccCCCross Compilergccglibc

--prefix=/usr/usr/local

--enable-add-onsglibclinuxthreadsglibc-enable-add-ons=linuxthreads

--with-headersglibc linux


Linux

fPICglibc

# make

# make install


Linux

7) fPICgcc

gccglibcglibcglibcgccCCC++

# cd $PRJROOT/build-tools/gcc-2.3.6

# ./configure --target=arm-linux --enable-languages=c,c++ --prefix=$PREFIX

# make

# make install

$PREFIX/binarm-linux-g++ arm-linux-c++


Linux

8) fPIC

hello.c

#include <stdio.h>

int main( )

{

printf(Hello,world!\n);

return 0;

}


Linux

fPIChellofile

ARM

ARMX86PC


Linux

# arm-linux-gcc o hello hello.c fPIC

# ./hello

hello: ELF 32-bit LSB executable, ARM, version 1 (ARM), for GNU/Linux 2.4.3,

dynamically linked (uses shared libs), not stripped

helloarm


Linux

2. fPICCrosstool

Crosstoolgccglibcglibchttp://kegel.com/crosstool

CrosstoolCrosstool


Linux

1 fPIC

binutils-2.15.tar.bz2gcc-3.4.5.tar.bz2glibc- 2.3.6.tar.bz2 glibc-linuxthreads-2.3.6.tar.bz2linux-2.6.28.2.tar.bz2linux-libc-headers- 2.6.12.0.tar.bz2


Linux

/opt/my2410/down

/opt/my2410crosstool-0.43.tar.gz

# cd /opt/my2410

# tar xvzf crosstool-0.43.tar.gz


Linux

2 /opt/my2410/down

crosstool.sh.datCPUCPUS3C2410Ademo-arm- softfloat.sh(UBOOT)


Linux

crosstool-0.43demo-arm-softfloat.sh

# cd crosstool-0.43

# vi demo-arm-softfloat.shdemo-arm-softfloat.sh


Linux

3 crosstool-0.43demo-arm-softfloat.sh

arm.sh arm.dat gcc-3.4.5-glibc-2.3.6.dat crosstool arm.dat


Linux

4 crosstool-0.43demo-arm-softfloat.sh

Crosstool demo-arm-softfloat.sh

[root@zxl-linux crosstool-0.43]# cd crosstool-0.43

[root@zxl-linux crosstool-0.43]# ./demo-arm-softfloat.sh

/opt/crosstoolgcc-3.4.5- glibc-2.3.6


Linux

/opt/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/bin


Linux

5 /opt/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/bin

gcc-3.4.5- glibc-2.3.6


Linux

export PATH=/opt/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/bin:$PATH


Linux

8.1.4 PATH=/opt/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/bin:$PATH Linux

LINUXWindowsLinuxVMware WorkstationCygwin


Linux

LINUXTelnetSSHLINUXTelnetSSH




Linux

2. Windowscygwin

CygwinWindowsLinux


Linux

3. WindowsLinux

VmwareLinuxLinux


Linux

4.WindowsLinux

WindowsLinuxLinux


Linux

8.1.5 VMWare

GCC for ARMLinuxLinux

Windows+VMware + RedHat


Linux

GCC

1WindowsVMware

2VMwareRedHat

3RedHatGCC forARM


Linux

VMWare Workstation http://www.crsky.com/soft/1863.html WindowsLinux5.0LinuxISO


Linux

8.1.6 RedHat9.0

VMwareRedHat

1VMware workstation

2FILE()-NEW()--NewVirtual Machine()

3RedhatLinux

CD-ROM1


Linux

Use ISO imageBrowseRedHatRedhat


Linux

123456,123456


Linux

LINUXISOVMware

1 8.1.10


Linux

2 ISO image

8.1.11


Linux

3 OptionsShared Folders8.1.12


Linux

4 WindowsLINUX

[root@zxl root]# cd mnt

[root@zxlmnt]# cd hgfs

[root@zxl cdrom]# cd zxl //

[root@zxl linux]# cp vivi.tar.gz / //

[root@zxl linux]# cd / //

[root@zxl /]#tar -zxvf vivi.tar.gz //vivi.tar.gz

[root@localhost /]#cd /vivi ////zxl2410


Linux

8.1.7

1. minicom

minicomWindowsminicomminicom

Linux

minicom -s

minicom


Linux

Serial port setup11520081

Save setup as dfl

Exitminicom

PCminicomttyS0ttyS10

minicom


Linux

minicom setup11520081minicomCTRL+AXCTRL+AZ


Linux

2. TFTP setup11520081

TFTPTrivial File Transfer ProtocolTFTP


Linux

TFTP REDHAT7.2 REDHAT9.0

setup

System servicestftp[*]ipchains iptables *

Firewall configurationNo firewallsetupTFTP


Linux

service xinetd restart REDHAT7.2 REDHAT9.0

TFTP tftp

cp /s3c2410_linux/Image/zImage /tftpboot//**/

tftp 192.168.2.xxx /*tftp*/

tftp>get zImage /*tftpzImage*/

tftp>q /*tftp*/


Linux

Received 741512 bytes in 0.7 secondsTFTP Timed outtftp

netstat -a|grep tftp

TFTP


Linux

3. NFS secondsTFTP Timed outtftp

NFSNetwork File SystemNFSLinuxNFSRPC servicewindows, "mount"remotelocal


Linux

NFSServersmountClientsNFSServersmountClients

NFS


Linux

NFSNFSServersmountClients

setup

System servicesnfs[*]ipchains iptables *

exports

vi /etc/exports

exports

/s3c2410_linux/nfs 192.168.0.16(rw,insecure,no_root_squash,no_all_squash)


Linux

<ESC> : wq ,/s3c2410_linux/nfsIP192.168.0.16

ip

/etc/rc.d/init.d/nfs restart

NFS

NFSroot NFS

/etc/rc.d/init.d/nfs start stop restart


Linux

NFS#

/etc/rc.d/init.d/nfs start

LinuxTerminalifconfigip

# ifconfig eth0 192.168.0.xxx

minicomipipexportsipminicom,(mount)


Linux

4. Samba NFS#

LinuxLinuxNFSWindowswindowsLinuxwindowsSamba

Sambasmbdnmbd

Samba8.1.14Samba

SambaSamba


Linux

SambaSamba

/etc/rc.d/init.d/smb start

/etc/rc.d/init.d/smb stop


Linux

SambaSamba

Microsoft WindowsLinux SambaDOS\\192.168.0.221

smbclientSamba


Linux

8Samba.2Shell

8.2.1 shell

LinuxShellShellShell

ShellLinux


Linux

1. ShellSamba

Shell

2. Shell

ifcasewhileforShell


Linux

ShellSamba

Shellcsh,tcsh,pdksh, ash, sash, zsh,bash

LinuxShellbash(Bourne Again Shell)Bourne ShellshC ShellcshKorn Shell(ksh)Shell


Linux

ShellSamba

Bourne ShellUnixShellUnixBourne ShellShellShell

BashBourne Again ShellBourne ShellBourne ShellC ShellKorn Shell


Linux

BashLinuxShellBash

1

BashTabBash

2

Bash**

3

Bash


Linux

4 BashLinuxShellBash

Bashaliasunalias

5/

/

6

|DOS/Windows


Linux

BashLinuxShellBash

cat dir.out|grep "test "|wc l

last

last |grep root

last |grep root |wc -l


Linux

8.2.2 Shell BashLinuxShellBash

ShellShellDOSLinuxShell


Linux

1. BashLinuxShellBash

viEmacsShellLinuxShellBash

ShellShell

#! /bin/sh


Linux

#!/bin/sh

chmod+x filename

shell#


Linux

2.Shell #!/bin/sh

Shell

1

Shell LinuxShell


Linux

#!/bin/sh1HOME/home/root/root

mynameHOME/home/mynamecd $HOMELinux

Shell$


Linux

2LOGNAME #!/bin/sh

LinuxLOGNAME

3PATH

ShellPATHPATHPATHDOS/Windows

PATH=$PATH:$HOME/bin


Linux

4PS1 #!/bin/sh

ShellShellPS1PS1="\u@\h \W\\$ "@

5PWD

6SHELL

ShellShellShell


Linux

2 #!/bin/sh

Shell


Linux

echo$

echo $SHELL

/bin/bash

cd $HOME/bin


Linux

3Shell echo$

Shell==BASH

#varname=value :

#readonly varname

#export varname


Linux

#setenv PATH=/home:/usr:/etc:/bin:/usr/bin: (csh) echo$

#varname=expr $varname + 1 1

x=$[ $x + 1 ]

#echo $PATH

shell


Linux

echo$:

#!/bin/sh

#set variable a

a="hello world"

#print a

Echo "A is:"

echo $a


Linux

3. echo$

1

1test

test d /usr [ -d /usr ]

test f .bashrc [ -f .bashrc ]

test $count gt 0 [ $count gt 0 ]


Linux

2if echo$

if (expression) then

command-list

else

command-list

fi


Linux

3 case echo$

case $var in

pattern1) command-list ;;

pattern2) command-list ;;

esac


Linux

4 && || echo$

# test f myfile.c && echo file found

If test f myfile.c then

echo file found

fi

# test f myfile.c | | echo file not found

If test ! f myfile.c then

echo file not found

fi


Linux

2 echo$

1for

for var in word-list

do

command-list

done


Linux

echo$

#!/bin/bash

for day in"Sun Mon Tue Wed Thu Fri Sat"

do

echo$day

done

forday"$"echo$day"$"


Linux

4.shell echo$

shell

1

1chmod u+x scriptname

2chmod ug+x scriptname

3chmod +x scriptname


Linux

echo$test1.sh

$chmod +x test1

$ ./ test1

2Bashsource

$ source test1

3sh

$ sh test1

4 shell

bash<

$sh < dircmp


Linux

8.2.3 shell echo$

1.

1 UNIX()

*

.,-,!

a-dxy abcdxy

z* z


Linux

2) echo$

a-f* afabcd2e3.cf.dat

*z zwin.zcore.zza-c-5z

rc?.d rc.drc0.drc2.drcS.d

*!o o


Linux

2. echo$

shellshell


Linux

1) echo$

|

w|wc -l

ps aux|grep ftp


Linux

2 echo$

&& (true)

(false)

& ()


Linux

echo$1

ls -l /tmp /root

w|wc -l

ps

2 1

ls -l /tmp /root w|wc -l ps


Linux

3 . echo$

1()

<

<<

>

>>


Linux

2() echo$

Linux012

3. (>>>)

command 2 >file

command 2 >>file


Linux

1 myfile1sort echo$

sort <myfile1

2 datemyfile2

date >myfile2

3 ls \|lmyfile3

ls -l>>myfile3

4err-file

myprog 2>err-file

5out

myprog >out 2>>out

myprog >out 2>>&1


Linux

8.3 Makefile echo$

1.GNU make

1) GNUmake

make make Makefile


Linux

2) Makefile echo$

make,MakefileMakefile


Linux

echo$


Linux

Makefile makefile: makeMakefilemake


Linux

makefile(Makefile)

network: network.o subrs.o (1)

gcc o network network.o subrs.o (2)

network.o: network.c netdefs.h (3)

gcc c network.c (4)

subrs.o: subrs.c netdefs.h (5)

gcc c subrs.c (6)

(3)(6)

network.o subrs.o: netdefs.h

TAB


Linux

makefile(Makefile)

#make

#make network

#make f makefile


Linux

2.makefile makefile(Makefile)

targets: prerequisites

command

main.o:main.c

gccc main.c


Linux

1 makefile(Makefile)

MakefileMakefilemake

Makefile


Linux

2 makefile(Makefile)

3

makemakefileMakefile

make f


Linux

4 makefile(Makefile)

Makefilephony targets

.PHONY:clean

clean:

rmf hello main.o func1.o func2.o

.PHONYclean


Linux

5 makefile(Makefile)

= $

myprog : foo.o bar.o

gcc foo.o bar.o -o myprog

foo.o : foo.c foo.h bar.h

gcc -c foo.c -o foo.o

bar.o : bar.c bar.h

gcc -c bar.c -o bar.o


Linux

makefile(Makefile)

OBJS = foo.o bar.o

CC = gcc

CFLAGS = -Wall -O -g

myprog : $(OBJS)

$(CC) $(OBJS) -o myprog

foo.o : foo.c foo.h bar.h

$(CC) $(CFLAGS) -c foo.c -o foo.o

bar.o : bar.c bar.h

$(CC) $(CFLAGS) -c bar.c -o bar.o


Linux

:

$@, $< $^

$@

$<

$^ makefile


Linux

OBJS = foo.o bar.o :

CC = gcc

CFLAGS = -Wall -O -g

myprog : $(OBJS)

$(CC) $^ -o $@

foo.o : foo.c foo.h bar.h

$(CC) $(CFLAGS) -c $< -o $@

bar.o : bar.c bar.h

$(CC) $(CFLAGS) -c $< -o $@


Linux

Q&A :


ad
  • Login