Sponsored Links
This presentation is the property of its rightful owner.
1 / 161

第八章 LINUX 编程基础 PowerPoint PPT Presentation


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

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

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

VMWareRedHat9.0

ShellMakefile


.1

1. Linux

Linux


Linux


2.

PC


libc cglibc

uClibc dietlibc newlib


GNU

http://www.gnu.orgGNULinux

Binutils

GCC

GLIBC

GDB


gccg++arasldglibcgdb

arm-linux-gccLinuxgccLinuxLinux


1Binutils

Binutilsbinutils


2GCC

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


GCC,:

1(Pre-Processing)

2(Compiling)

3(Assembling)

4(Linking)


gcc

gcc [options] [filenames]

options

filenames:


gcc100

-o output_filenameoutput_filenamegcca.out

-c.c.o


-g(GNUgdb)

-O

-O2-O


3GLIBC

GlibcCGCCglibc/lib/usr/libGlibc


4GDB

GNUGDBGNU DebugGDBCC++GDB

1

2

3

GDB

gdb

gdb helloworld


#include<stdio.h>

void main()

{

int i;

long result=0;

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

{

result+=i;

}

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

}


1

gcc-g tst.c

2GDB

gdb tst

3main

break main

4

run

5

next

6

Continue


5 Crosstool

Crosstoolgccglibccrosstoolgccglibc


BinutilsgccglibcBUG

gcc2.95.xLinux2.4

Linux2.6Linux2.6gcc3.3


8.1.3

GNU


GCCGCCGNUCPU-PLATFORM-OS

x86/i386 i686-pc-linux-gnu

ARMarm-linux-gnu


3


Crosstool


ftp.arm.kernel.org.uk


1.

8.1.7


1

# cd /my2410

# mkdir armlinux


armlinux3 build-toolskernel tools

# cd armlinux

# mkdir build-tools kernel tools

1build-toolsbinutilsgccglibc

2kernel

3tools


2

:


# export PRJROOT=/home/mike/armlinux

# export TARGET=arm-linux

# export PREFIX=$PRJROOT/tools

# export TARGET_PREFIX=$PREFIX/$TARGET

# export PATH=$PREFIX/bin:$PATH

export ,export

bashrcexport


3Binutils

BinutilsGNU

Binutilsaddr2linearasc++filtgprofldnmobjcopyobjdumpranlibreadelfsizestringsstriplibibertylibbfdlibopcodes


Binutils

BashCoreutilsDiffutilsGCCGettextGlibcGrepMakePerlSedTexinfo


binutils-2.15

binutils-2.15.tar.bz2

# cd $PRJROOT/build-tools

# tar xjvf binutils-2.15.tar.bz2


Binutils

# cd $PRJROOT/build-tools

# mkdir build-binutils

# cd build-binutils

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


target arm-linux

--prefix check Makefile

make

# make

# make install


/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


4

Linux


arm-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


ARCH=armarm

CROSS_COMPILE=arm-linux-arm-linux-

configxconfigmenuconfigmake menuconfig

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


8.1.5 Linux 2.6.30


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


/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


5) boot-trap gcc

arm-linux-gccgccglibcBootLoaderC

CCBootLoader


# 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


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


TARGET_LIBGCC2-CFLAGS = -fomit-frame-pointer fPIC

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

t-linux


# cd build-gcc

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

--disable-threads --disable-shared

--enable-languages=cC

--disable-threadsthreadglibc

--disable-shared


# make

# make install

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


6) glibc

glibcGUN CLinux

glibc-2.3.2

(1) GNU make 3.79

(2) GCC 3.2

(3) GNU binutils 2.13


glibc-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


glibc-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


CC=arm-linux-gccCCCross Compilergccglibc

--prefix=/usr/usr/local

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

--with-headersglibc linux


glibc

# make

# make install


7) gcc

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++


8)

hello.c

#include <stdio.h>

int main( )

{

printf(Hello,world!\n);

return 0;

}


hellofile

ARM

ARMX86PC


# arm-linux-gcc o hello hello.c

# ./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


2. Crosstool

Crosstoolgccglibcglibchttp://kegel.com/crosstool

CrosstoolCrosstool


1

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


/opt/my2410/down

/opt/my2410crosstool-0.43.tar.gz

# cd /opt/my2410

# tar xvzf crosstool-0.43.tar.gz


2

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


crosstool-0.43demo-arm-softfloat.sh

# cd crosstool-0.43

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


3

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


4

Crosstool demo-arm-softfloat.sh

[[email protected] crosstool-0.43]# cd crosstool-0.43

[[email protected] crosstool-0.43]# ./demo-arm-softfloat.sh

/opt/crosstoolgcc-3.4.5- glibc-2.3.6


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


5

gcc-3.4.5- glibc-2.3.6


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


8.1.4 Linux

LINUXWindowsLinuxVMware WorkstationCygwin


LINUXTelnetSSH


LinuxgccFTPPC


1. Windows Linux


2. Windowscygwin

CygwinWindowsLinux


3. WindowsLinux

VmwareLinuxLinux


4.WindowsLinux

WindowsLinuxLinux


8.1.5 VMWare

GCC for ARMLinuxLinux

Windows+VMware + RedHat


GCC

1WindowsVMware

2VMwareRedHat

3RedHatGCC forARM


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


8.1.6 RedHat9.0

VMwareRedHat

1VMware workstation

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

3RedhatLinux

CD-ROM1


Use ISO imageBrowseRedHatRedhat


123456,123456


LINUXISOVMware

1 8.1.10


2ISO image

8.1.11


3OptionsShared Folders8.1.12


4WindowsLINUX

[[email protected] root]# cd mnt

[[email protected]]# cd hgfs

[[email protected] cdrom]# cd zxl //

[[email protected] linux]# cp vivi.tar.gz / //

[[email protected] linux]# cd / //

[[email protected] /]#tar -zxvf vivi.tar.gz //vivi.tar.gz

[[email protected] /]#cd /vivi ////zxl2410


8.1.7

1. minicom

minicomWindowsminicomminicom

Linux

minicom -s

minicom


Serial port setup11520081

Save setup as dfl

Exitminicom

PCminicomttyS0ttyS10

minicom


minicomminicomCTRL+AXCTRL+AZ


2. TFTP

TFTPTrivial File Transfer ProtocolTFTP


TFTP REDHAT7.2 REDHAT9.0

setup

System servicestftp[*]ipchains iptables *

Firewall configurationNo firewallsetupTFTP


service xinetd restart

TFTP tftp

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

tftp 192.168.2.xxx /*tftp*/

tftp>get zImage /*tftpzImage*/

tftp>q /*tftp*/


Received 741512 bytes in 0.7 secondsTFTP Timed outtftp

netstat -a|grep tftp

TFTP


3. NFS

NFSNetwork File SystemNFSLinuxNFSRPC servicewindows, "mount"remotelocal


NFSServersmountClients

NFS


NFS

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)


<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


NFS#

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

LinuxTerminalifconfigip

# ifconfig eth0 192.168.0.xxx

minicomipipexportsipminicom,(mount)


4. Samba

LinuxLinuxNFSWindowswindowsLinuxwindowsSamba

Sambasmbdnmbd

Samba8.1.14Samba

SambaSamba


Samba

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

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


Samba

Microsoft WindowsLinux SambaDOS\\192.168.0.221

smbclientSamba


8.2Shell

8.2.1 shell

LinuxShellShellShell

ShellLinux


1. Shell

Shell

2. Shell

ifcasewhileforShell


Shell

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

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


Shell

Bourne ShellUnixShellUnixBourne ShellShellShell

BashBourne Again ShellBourne ShellBourne ShellC ShellKorn Shell


BashLinuxShellBash

1

BashTabBash

2

Bash**

3

Bash


4

Bashaliasunalias

5/

/

6

|DOS/Windows


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

last

last |grep root

last |grep root |wc -l


8.2.2 Shell

ShellShellDOSLinuxShell


1.

viEmacsShellLinuxShellBash

ShellShell

#! /bin/sh


#!/bin/sh

chmod+x filename

shell#


2.Shell

Shell

1

Shell LinuxShell


1HOME/home/root/root

mynameHOME/home/mynamecd $HOMELinux

Shell$


2LOGNAME

LinuxLOGNAME

3PATH

ShellPATHPATHPATHDOS/Windows

PATH=$PATH:$HOME/bin


4PS1

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

5PWD

6SHELL

ShellShellShell


2

Shell


echo$

echo $SHELL

/bin/bash

cd $HOME/bin


3Shell

Shell==BASH

#varname=value :

#readonly varname

#export varname


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

#varname=expr $varname + 1 1

x=$[ $x + 1 ]

#echo $PATH

shell


:

#!/bin/sh

#set variable a

a="hello world"

#print a

Echo "A is:"

echo $a


3.

1

1test

test d /usr [ -d /usr ]

test f .bashrc [ -f .bashrc ]

test $count gt 0 [ $count gt 0 ]


2if

if (expression) then

command-list

else

command-list

fi


3 case

case $var in

pattern1) command-list ;;

pattern2) command-list ;;

esac


4 && ||

# 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


2

1for

for var in word-list

do

command-list

done


#!/bin/bash

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

do

echo$day

done

forday"$"echo$day"$"


4.shell

shell

1

1chmod u+x scriptname

2chmod ug+x scriptname

3chmod +x scriptname


test1.sh

$chmod +x test1

$ ./ test1

2Bashsource

$ source test1

3sh

$ sh test1

4 shell

bash<

$sh < dircmp


8.2.3 shell

1.

1 UNIX()

*

.,-,!

a-dxy abcdxy

z* z


2)

a-f* afabcd2e3.cf.dat

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

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

*!o o


2.

shellshell


1)

|

w|wc -l

ps aux|grep ftp


2

&& (true)

(false)

& ()


1

ls -l /tmp /root

w|wc -l

ps

2 1

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


3 .

1()

<

<<

>

>>


2()

Linux012

3. (>>>)

command 2 >file

command 2 >>file


1 myfile1sort

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


8.3 Makefile

1.GNU make

1) GNUmake

make make Makefile


2) Makefile

make,MakefileMakefile



Makefile makefile: makeMakefilemake


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


#make

#make network

#make f makefile


2.makefile

targets: prerequisites

command

main.o:main.c

gccc main.c


1

MakefileMakefilemake

Makefile


2

3

makemakefileMakefile

make f


4

Makefilephony targets

.PHONY:clean

clean:

rmf hello main.o func1.o func2.o

.PHONYclean


5

= $

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


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


:

$@, $< $^

$@

$<

$^ makefile


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 $@


Q&A


  • Login