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

第八章 LINUX 编程基础 PowerPoint PPT Presentation


  • 94 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


Linux

Linux

VMWareRedHat9.0

ShellMakefile


Linux

.1

1. Linux

Linux


Linux

Linux


Linux

2.

PC


Linux

libc cglibc

uClibc dietlibc newlib


Linux

GNU

http://www.gnu.orgGNULinux

Binutils

GCC

GLIBC

GDB


Linux

gccg++arasldglibcgdb

arm-linux-gccLinuxgccLinuxLinux


Linux

1Binutils

Binutilsbinutils


Linux

2GCC

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


Linux

GCC,:

1(Pre-Processing)

2(Compiling)

3(Assembling)

4(Linking)


Linux

gcc

gcc [options] [filenames]

options

filenames:


Linux

gcc100

-o output_filenameoutput_filenamegcca.out

-c.c.o


Linux

-g(GNUgdb)

-O

-O2-O


Linux

3GLIBC

GlibcCGCCglibc/lib/usr/libGlibc


Linux

4GDB

GNUGDBGNU DebugGDBCC++GDB

1

2

3

GDB

gdb

gdb helloworld


Linux

#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

gcc-g tst.c

2GDB

gdb tst

3main

break main

4

run

5

next

6

Continue


Linux

5 Crosstool

Crosstoolgccglibccrosstoolgccglibc


Linux

BinutilsgccglibcBUG

gcc2.95.xLinux2.4

Linux2.6Linux2.6gcc3.3


Linux

8.1.3

GNU


Linux

GCCGCCGNUCPU-PLATFORM-OS

x86/i386 i686-pc-linux-gnu

ARMarm-linux-gnu


Linux

3


Linux

Crosstool


Linux

ftp.arm.kernel.org.uk


Linux

1.

8.1.7


Linux

1

# cd /my2410

# mkdir armlinux


Linux

armlinux3 build-toolskernel tools

# cd armlinux

# mkdir build-tools kernel tools

1build-toolsbinutilsgccglibc

2kernel

3tools


Linux

2

:


Linux

# 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


Linux

3Binutils

BinutilsGNU

Binutilsaddr2linearasc++filtgprofldnmobjcopyobjdumpranlibreadelfsizestringsstriplibibertylibbfdlibopcodes


Linux

Binutils

BashCoreutilsDiffutilsGCCGettextGlibcGrepMakePerlSedTexinfo


Linux

binutils-2.15

binutils-2.15.tar.bz2

# cd $PRJROOT/build-tools

# tar xjvf binutils-2.15.tar.bz2


Linux

Binutils

# cd $PRJROOT/build-tools

# mkdir build-binutils

# cd build-binutils

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


Linux

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

Linux


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


Linux

ARCH=armarm

CROSS_COMPILE=arm-linux-arm-linux-

configxconfigmenuconfigmake menuconfig

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


Linux

8.1.5 Linux 2.6.30


Linux

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) boot-trap gcc

arm-linux-gccgccglibcBootLoaderC

CCBootLoader


Linux

# 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

ARMlibcgcc/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

# 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

# make

# make install

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


Linux

6) glibc

glibcGUN CLinux

glibc-2.3.2

(1) GNU make 3.79

(2) GCC 3.2

(3) GNU binutils 2.13


Linux

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


Linux

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


Linux

CC=arm-linux-gccCCCross Compilergccglibc

--prefix=/usr/usr/local

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

--with-headersglibc linux


Linux

glibc

# make

# make install


Linux

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


Linux

8)

hello.c

#include <stdio.h>

int main( )

{

printf(Hello,world!\n);

return 0;

}


Linux

hellofile

ARM

ARMX86PC


Linux

# 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


Linux

2. Crosstool

Crosstoolgccglibcglibchttp://kegel.com/crosstool

CrosstoolCrosstool


Linux

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


Linux

/opt/my2410/down

/opt/my2410crosstool-0.43.tar.gz

# cd /opt/my2410

# tar xvzf crosstool-0.43.tar.gz


Linux

2

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

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


Linux

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


Linux

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


Linux

5

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 Linux

LINUXWindowsLinuxVMware WorkstationCygwin


Linux

LINUXTelnetSSH


Linux

LinuxgccFTPPC


Linux

1. Windows Linux


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 Workstationhttp://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

2ISO image

8.1.11


Linux

3OptionsShared Folders8.1.12


Linux

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


Linux

8.1.7

1. minicom

minicomWindowsminicomminicom

Linux

minicom -s

minicom


Linux

Serial port setup11520081

Save setup as dfl

Exitminicom

PCminicomttyS0ttyS10

minicom


Linux

minicomminicomCTRL+AXCTRL+AZ


Linux

2. TFTP

TFTPTrivial File Transfer ProtocolTFTP


Linux

TFTP REDHAT7.2 REDHAT9.0

setup

System servicestftp[*]ipchains iptables *

Firewall configurationNo firewallsetupTFTP


Linux

service xinetd restart

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

NFSNetwork File SystemNFSLinuxNFSRPC servicewindows, "mount"remotelocal


Linux

NFSServersmountClients

NFS


Linux

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)


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

LinuxLinuxNFSWindowswindowsLinuxwindowsSamba

Sambasmbdnmbd

Samba8.1.14Samba

SambaSamba


Linux

Samba

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

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


Linux

Samba

Microsoft WindowsLinux SambaDOS\\192.168.0.221

smbclientSamba


Linux

8.2Shell

8.2.1 shell

LinuxShellShellShell

ShellLinux


Linux

1. Shell

Shell

2. Shell

ifcasewhileforShell


Linux

Shell

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

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


Linux

Shell

Bourne ShellUnixShellUnixBourne ShellShellShell

BashBourne Again ShellBourne ShellBourne ShellC ShellKorn Shell


Linux

BashLinuxShellBash

1

BashTabBash

2

Bash**

3

Bash


Linux

4

Bashaliasunalias

5/

/

6

|DOS/Windows


Linux

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

last

last |grep root

last |grep root |wc -l


Linux

8.2.2 Shell

ShellShellDOSLinuxShell


Linux

1.

viEmacsShellLinuxShellBash

ShellShell

#! /bin/sh


Linux

#!/bin/sh

chmod+x filename

shell#


Linux

2.Shell

Shell

1

Shell LinuxShell


Linux

1HOME/home/root/root

mynameHOME/home/mynamecd $HOMELinux

Shell$


Linux

2LOGNAME

LinuxLOGNAME

3PATH

ShellPATHPATHPATHDOS/Windows

PATH=$PATH:$HOME/bin


Linux

4PS1

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

5PWD

6SHELL

ShellShellShell


Linux

2

Shell


Linux

echo$

echo $SHELL

/bin/bash

cd $HOME/bin


Linux

3Shell

Shell==BASH

#varname=value :

#readonly varname

#export varname


Linux

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

#varname=expr $varname + 1 1

x=$[ $x + 1 ]

#echo $PATH

shell


Linux

:

#!/bin/sh

#set variable a

a="hello world"

#print a

Echo "A is:"

echo $a


Linux

3.

1

1test

test d /usr [ -d /usr ]

test f .bashrc [ -f .bashrc ]

test $count gt 0 [ $count gt 0 ]


Linux

2if

if (expression) then

command-list

else

command-list

fi


Linux

3 case

case $var in

pattern1) command-list ;;

pattern2) command-list ;;

esac


Linux

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


Linux

2

1for

for var in word-list

do

command-list

done


Linux

#!/bin/bash

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

do

echo$day

done

forday"$"echo$day"$"


Linux

4.shell

shell

1

1chmod u+x scriptname

2chmod ug+x scriptname

3chmod +x scriptname


Linux

test1.sh

$chmod +x test1

$ ./ test1

2Bashsource

$ source test1

3sh

$ sh test1

4 shell

bash<

$sh < dircmp


Linux

8.2.3 shell

1.

1 UNIX()

*

.,-,!

a-dxy abcdxy

z* z


Linux

2)

a-f* afabcd2e3.cf.dat

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

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

*!o o


Linux

2.

shellshell


Linux

1)

|

w|wc -l

ps aux|grep ftp


Linux

2

&& (true)

(false)

& ()


Linux

1

ls -l /tmp /root

w|wc -l

ps

2 1

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


Linux

3 .

1()

<

<<

>

>>


Linux

2()

Linux012

3. (>>>)

command 2 >file

command 2 >>file


Linux

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


Linux

8.3 Makefile

1.GNU make

1) GNUmake

make make Makefile


Linux

2) Makefile

make,MakefileMakefile


Linux


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

#make

#make network

#make f makefile


Linux

2.makefile

targets: prerequisites

command

main.o:main.c

gccc main.c


Linux

1

MakefileMakefilemake

Makefile


Linux

2

3

makemakefileMakefile

make f


Linux

4

Makefilephony targets

.PHONY:clean

clean:

rmf hello main.o func1.o func2.o

.PHONYclean


Linux

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


Linux

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


  • Login