有在龙芯笔记本电脑电脑上升级或重新编译Linux内核成功的吗

> 博客详情
Linux环境搭建与内核编译
上一篇中把4750开发板与PC的硬件连通了,这一篇记录的是基本的Linux环境的搭建。其实官方文档已经比较细了,网上也有大量的资料,但到实践中还是有一些问题,在此记录一下。
首先是安装一个LINUX系统来做开发环境了。官方推荐当然是直接装一个,但是一方面我平时很多工作还是要在Windows下完成,另一方面我也习惯了用Windows,因此我还是没有直接装。
最开始搞了个VMWare虚拟机装了个RedHat,后来又尝试了一个Cygwin,下载了老半天装完,然后鼓弄了很久,觉得都不好用。后来看官方 文档上是以Ubuntu为例,于是在VMWare上装了个Ubuntu桌面,但VMWare Tools老装不上,查了下说要重新编译内核,试了几回,内核是编译过了,但VMWare Tools还是有一部分编译不了,最重要的是共享文件那块没法用。我一怒之下,把装好的系统删了。后来下载了一个别人已经装好的Ubuntu Server 9.10镜像,据说已经装好了VMWare Tools,拉下来一看其实也没有装,但我懒得再折腾了,就基于这个系统开始配置开发环境了。
由于没有搞掂VMWare与主机的文件共享,我只好把JZ4750的源码和工具包打包成一个ISO文件挂到Ubuntu的CDROM上访问。按照文 档的指示,安装编译mipseltools-gcc412-glibc261.tar.bz2,编译了君正PATCH过的UBOOT和LINUX内核,烧 写和配置UBOOT,再安装TFTP和NFS网络文件系统,配置UBOOT从NFS加载LINUX内核。文档上是先安装TFTP和NFS,其实可以放到后 一步与根文件系统一块去做更合乎习惯。
具体过程基本上都是在putty终端上完成。我一般在WINDOWS上开两个putty,一个连开发板COM口,另一个连VMWare的ubuntu终端。如果你本来就是用LINUX系统,那你可以像官方文档说的一样用minicom连COM口。
由于我装的是ubuntu server,是没有桌面的,原始大小只有900M多一点,压缩一下只有208M,因此我不在上面做图形开发。由于选择用QT做GUI,QT提供了 WINDOWS下的源码和开发环境,可以跟Visual Studio一起用,因此可以在WINDOWS下开发完,再转到ubuntu上交叉编译,再放到开发板上运行。
进行编译操作前,最好确认有足够的磁盘空间,因为中间编译时间较长,需要很大磁盘空间,要是因为空间不够导致中止就不划算了。 我的虚拟磁盘最大设置是20G。
为了较详细地记录安装编译过程,我把所有过程重新做了一次,把主要的操作命令和输出结果也贴上来,虽有些繁琐,但对新人理解和比较整个安装过程是有些帮助的。我自己在安装时,就很希望有这么个日志记录。
安装基本的编译工具gcc,g++,make之类的,Ubuntu提供了一个build-essential软件包可以直接全装上:
(如果不是以root登录,要在命令前加sudo提升权限;安装过程需要从网上下载数据,请保持网络连接;如果是刚装好的系统,可能需要先执行apt-get update更新软件库列表) root :/# apt-get install build-essential Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: & ssl-cert Use 'apt-get autoremove' to remove them. The following extra packages will be installed: & binutils cpp-4.4 dpkg-dev fakeroot g++ g++-4.4 gcc gcc-4.4 gcc-4.4-base & libc-bin libc-dev-bin libc6 libc6-dev libc6-i686 libgcc1 libgomp1 libstdc++6 & libstdc++6-4.4-dev linux-libc-dev Suggested packages: & binutils-doc gcc-4.4-locales debian-keyring debian-maintainers g++-multilib & g++-4.4-multilib gcc-4.4-doc libstdc++6-4.4-dbg gcc-multilib manpages-dev & autoconf automake1.9 libtool flex bison gdb gcc-doc gcc-4.4-multilib & libmudflap0-4.4-dev libgcc1-dbg libgomp1-dbg libmudflap0-dbg libcloog-ppl0 & libppl-c2 libppl7 glibc-doc libstdc++6-4.4-doc The following NEW packages will be installed: & binutils build-essential dpkg-dev fakeroot g++ g++-4.4 gcc gcc-4.4 & libc-dev-bin libc6-dev libgomp1 libstdc++6-4.4-dev linux-libc-dev The following packages will be upgraded: & cpp-4.4 gcc-4.4-base libc-bin libc6 libc6-i686 libgcc1 libstdc++6 7 upgraded, 13 newly installed, 0 to remove and 54 not upgraded. Need to get 26.9MB of archives. After this operation, 61.6MB of additional disk space will be used. Do you want to continue [Y/n]? y Get:1
karmic-updates/main libc-bin 2.10.1-0ubuntu16 [715kB] Get:2
karmic-updates/main libc6 2.10.1-0ubuntu16 [3,759kB] Get:3
karmic-updates/main libc6-i686 2.10.1-0ubuntu16 [1,199kB] ... Get:19
karmic/main build-essential 11.4 [7,172B] Get:20
karmic/main fakeroot 1.12.4ubuntu1 [126kB] Fetched 26.9MB in 19min 55s (22.5kB/s) Preconfiguring packages ... (Reading database ... 40434 files and directories currently installed.) Preparing to replace libc-bin 2.10.1-0ubuntu15 (using .../libc-bin_2.10.1-0ubuntu16_i386.deb) ... Unpacking replacement libc-bin ... Processing triggers for man-db ... ... Preparing to replace libstdc++6 4.4.1-4ubuntu8 (using .../libstdc++6_4.4.1-4ubuntu9_i386.deb) ... Unpacking replacement libstdc++6 ... Processing triggers for man-db ... Setting up libstdc++6 (4.4.1-4ubuntu9) ...
Processing triggers for libc-bin ... ldconfig deferred processing now taking place Selecting previously deselected package binutils. (Reading database ... 40434 files and directories currently installed.) Unpacking binutils (from .../binutils_2.20-0ubuntu2_i386.deb) ... ... Selecting previously deselected package fakeroot. Unpacking fakeroot (from .../fakeroot_1.12.4ubuntu1_i386.deb) ... Processing triggers for man-db ... Setting up libc6-i686 (2.10.1-0ubuntu16) ...
Setting up cpp-4.4 (4.4.1-4ubuntu9) ... ...
Setting up libstdc++6-4.4-dev (4.4.1-4ubuntu9) ... Setting up g++-4.4 (4.4.1-4ubuntu9) ... Setting up g++ (4:4.4.1-1ubuntu2) ... update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode.
Setting up build-essential (11.4) ... Processing triggers for libc-bin ... ldconfig deferred processing now taking place root :/# make make: *** No targets specified and no makefile found.& Stop. root :/#
把君正提供的几个TAR安装打包了ISO挂到虚拟机的CDROM上,然后按文档进行解压安装。 root :/# mount /dev/cdrom /mnt mount: block device /dev/sr0 is write-protected, mounting read-only root :/# ls /mnt celinux-040503-jz-.patch.gz& mipseltools-gcc412-lnx24.tar.gz celinux-040503.tar.bz2&&&&&&&&&&&&&& mipseltools-gcc412-lnx26.tar.gz linux2.4_developer_guide.pdf&&&&&&&& mips_toolchain_guide.pdf linux-2.6.24.3-jz-.patch.gz& MPlayer-1.0rc2-.tar.bz2 linux-2.6.24.3.tar.bz2&&&&&&&&&&&&&& mxu_user_guide.pdf linux2.6_developer_guide_v1.4.1.pdf& root-jz-.tar.bz2 linux_resource_guide_v1.1.pdf&&&&&&& u-boot-1.1.6-jz-.patch.gz mipsel-gcc4.1-cygwin-nopic.tar.bz2&& uboot-1.1.6.tar.gz mipseltools-gcc412-glibc261.tar.bz2& uboot_developer_guide.pdf root :/#
首先安装MIPS交叉编译工具。我理解的所谓交叉编译,就是从一个系统上编译程序给另一个不同的系统使用。 对我的实际来说,就是从PC电脑的ubuntu linux系统上编译程序给君正4750开发板mips的系统使用。 root :/opt# mkdir -p /opt root :/# cd /opt root :/opt# tar -xjf /mnt/mipseltools-gcc412-glibc261.tar.bz2 root :/opt# ls mipseltools-gcc412-glibc261 root :/opt#
写一个简单C文件验证一下: root :/opt# vi hello.c
按i进入编辑模式,输入以下内容,再按ESC,输入:wq保存退出: #include &stdio.h& int main() { &&&&&&& printf("hello jz-mipsel-linux/r/n"); &&&&&&& return 0; }
root :/opt# /opt/mipseltools-gcc412-glibc261/bin/mipsel-linux-gcc -o hello hello.c root :/opt# ls hello& hello.c& mipseltools-gcc412-glibc261 root :/opt# ./hello -bash: ./hello: cannot execute binary file root :/opt# 编译成功,当然了,编译的输出程序是mips指令系统的,只能在开发板上运行,不能在本机运行。
接着是交叉编译用于mips架构的UBOOT和Linux内核。其实开发板上默认已经有这两个东东了,如果你只想编写程序不想修改内核的话,应该可以不用编译烧录。 不过既然是搞开发的,不管用不用得着,自然还是要试一试的。 解压UBOOT,打上君正的补丁: root :/opt/u-boot-1.1.6# tar -xf /mnt/uboot-1.1.6.tar.gz root :/opt# ls hello& hello.c& mipseltools-gcc412-glibc261& u-boot-1.1.6 root :/opt# cd u-boot-1.1.6/ root :/opt/u-boot-1.1.6# gzip -cd /mnt/u-boot-1.1.6-jz-.patch.gz |patch -p1 patching file ... ... patching file nand_spl/nand_boot_jz4750.c patching file nand_spl/nand_boot_jz5730.c patching file net/eth.c patching file README.JZ patching file tools/bmp_logo.c root :/opt/u-boot-1.1.6#
进行配置和交叉编译: root :/opt/u-boot-1.1.6# export PATH=/opt/mipseltools-gcc412-glibc261/bin:$PATH root :/opt/u-boot-1.1.6# make apus_nand_config Compile NAND boot image for apus Configuring for apus board... root :/opt/u-boot-1.1.6# make for dir in tools examples post post/ do make -C $dir _ done make[1]: Entering directory `/opt/u-boot-1.1.6/tools' ln -s ../common/environment.c environment.c ln -s ../lib_generic/crc32.c crc32.c make[1]: Leaving directory `/opt/u-boot-1.1.6/tools' make[1]: Entering directory `/opt/u-boot-1.1.6/tools' make[1]: Nothing to be done for `_depend'. make[1]: Leaving directory `/opt/u-boot-1.1.6/tools' make[1]: Entering directory `/opt/u-boot-1.1.6/examples'
make -C tools all make[1]: Entering directory `/opt/u-boot-1.1.6/tools' gcc -Wall -pedantic -idirafter /opt/u-boot-1.1.6/include -idirafter /opt/u-boot-1.1.6/include2 -idirafter /opt/u-boot-1.1.6/include -DTEXT_BASE=0x -DUSE_HOSTCC
-O -c -o img2srec.o img2srec.c gcc -Wall -pedantic -idirafter /opt/u-boot-1.1.6/include -idirafter /opt/u-boot-1.1.6/include2 -idirafter /opt/u-boot-1.1.6/include -DTEXT_BASE=0x -DUSE_HOSTCC
-O& -o img2srec img2srec.o strip img2srec gcc -g -Wall -pedantic -idirafter /opt/u-boot-1.1.6/include -idirafter /opt/u-boot-1.1.6/include2 -idirafter /opt/u-boot-1.1.6/include -DTEXT_BASE=0x -
DUSE_HOSTCC -O -c -o mkimage.o mkimage.c gcc -g -Wall -pedantic -idirafter /opt/u-boot-1.1.6/include -idirafter /opt/u-boot-1.1.6/include2 -idirafter /opt/u-boot-1.1.6/include -DTEXT_BASE=0x -
DUSE_HOSTCC -O -c -o crc32.o crc32.c gcc -Wall -pedantic -idirafter /opt/u-boot-1.1.6/include -idirafter /opt/u-boot-1.1.6/include2 -idirafter /opt/u-boot-1.1.6/include -DTEXT_BASE=0x -DUSE_HOSTCC
-O& -o mkimage mkimage.o crc32.o ..... dd bs=1024 count=256 if=/opt/u-boot-1.1.6/nand_spl/junk6 of=/opt/u-boot-1.1.6/nand_spl/u-boot-spl-pad.bin 256+0 records in 256+0 records out 262144 bytes (262 kB) copied, 1e-09 s, 262 TB/s rm -f /opt/u-boot-1.1.6/nand_spl/junk* make[1]: Leaving directory `/opt/u-boot-1.1.6/nand_spl/board/apus' cat nand_spl/u-boot-spl-pad.bin u-boot.bin & u-boot-nand.bin root :/opt/u-boot-1.1.6# 编译成功,生成u-boot-nand.bin,可烧录到开发板上。
接下来解压、补丁、配置和交叉编译mips下的linux内核: root :/opt# export PATH=/opt/mipseltools-gcc412-glibc261/bin:$PATH&& //如果前面已经设置了路径,这里可以不需要了。 root :/opt# tar -xjf /mnt/linux-2.6.24.3.tar.bz2 root :/opt# ls hello& hello.c& linux-2.6.24.3& mipseltools-gcc412-glibc261& u-boot-1.1.6 root :/opt# cd linux-2.6.24.3/ root :/opt/linux-2.6.24.3# gzip -cd /mnt/linux-2.6.24.3-jz-.patch.gz | patch -p1 patching file ... ... patching file sound/soc/jz4740/jz4740-ac97.c patching file sound/soc/jz4740/jz4740-ac97.h patching file sound/soc/jz4740/jz4740-i2s.c patching file sound/soc/jz4740/jz4740-i2s.h patching file sound/soc/jz4740/jz4740-pcm.c patching file sound/soc/jz4740/jz4740-pcm.h patching file sound/soc/jz4740/Kconfig patching file sound/soc/jz4740/Makefile patching file sound/soc/jz4740/pavo.c patching file sound/soc/Kconfig patching file sound/soc/Makefile root :/opt/linux-2.6.24.3# root :/opt/linux-2.6.24.3# make apus_defconfig & HOSTCC& scripts/basic/fixdep scripts/basic/fixdep.c: In function ‘traps’: scripts/basic/fixdep.c:377: warning: dereferencing type-punned pointer will break strict-aliasing rules ... scripts/kconfig/lex.zconf.c:1639: warning: ‘input’ defined but not used & HOSTLD& scripts/kconfig/conf * * Linux Kernel Configuration * * * Machine selection * System type & 1. Ingenic JZ4730 PMP board (JZ4730_PMP) & 2. Ingenic JZ4740 PAVO board (JZ4740_PAVO) & 3. Ingenic JZ4740 LEO board (JZ4740_LEO) & 4. Ingenic JZ4740 LYRA board (JZ4740_LYRA) & 5. Ingenic JZ4725 DIPPER board (JZ4725_DIPPER) & 6. Ingenic JZ4720 VIRGO board (JZ4720_VIRGO) & 7. Ingenic JZ4750 FUWA board (JZ4750_FUWA) & 8. Ingenic JZ4750d FUWA1 board (JZ4750D_FUWA1) (NEW) & 9. Ingenic JZ4750 APUS board (JZ4750_APUS) & 10. Alchemy processor based machines (MACH_ALCHEMY) & 11. Basler eXcite smart camera (BASLER_EXCITE) ... & 45. Wind River PPMC board (WR_PPMC) choice[1-45]: 9 Endianess selection & 1. Little endian (CPU_LITTLE_ENDIAN) choice[1]: 1 * * CPU selection * CPU type & 1. MIPS32 Release 1 (CPU_MIPS32_R1) choice[1]: 1 * * Kernel type * Kernel code model & 1. 32-bit kernel (32BIT) choice[1]: 1 Kernel page size & 1. 4kB (PAGE_SIZE_4KB) & 2. 16kB (PAGE_SIZE_16KB) & 3. 64kB (PAGE_SIZE_64KB) choice[1-3]: 1
.... CRC-CCITT functions (CRC_CCITT) [N/m/y/?] n CRC16 functions (CRC16) [N/m/y/?] n CRC ITU-T V.41 functions (CRC_ITU_T) [N/m/y/?] n CRC32 functions (CRC32) [Y/?] y CRC7 functions (CRC7) [N/m/y/?] n CRC32c (Castagnoli, et al) Cyclic Redundancy-Check (LIBCRC32C) [N/m/y/?] n # # configuration written to .config # root :/opt/linux-2.6.24.3# root :/opt/linux-2.6.24.3# make uImage scripts/kconfig/conf -s arch/mips/Kconfig & CHK&&&& include/linux/version.h & UPD&&&& include/linux/version.h & CHK&&&& include/linux/utsrelease.h & UPD&&&& include/linux/utsrelease.h & SYMLINK include/asm -& include/asm-mips & CC&&&&& arch/mips/kernel/asm-offsets.s & GEN&&&& include/asm-mips/asm-offsets.h & CALL&&& scripts/checksyscalls.sh & HOSTCC& scripts/genksyms/genksyms.o & SHIPPED scripts/genksyms/lex.c & SHIPPED scripts/genksyms/parse.h & SHIPPED scripts/genksyms/keywords.c & HOSTCC& scripts/genksyms/lex.o scripts/genksyms/lex.c:1481: warning: ‘input’ defined but not used & SHIPPED scripts/genksyms/parse.c & HOSTCC& scripts/genksyms/parse.o & HOSTLD& scripts/genksyms/genksyms & CC&&&&& scripts/mod/empty.o & HOSTCC& scripts/mod/mk_elfconfig & MKELF&& scripts/mod/elfconfig.h & HOSTCC& scripts/mod/file2alias.o ... & CC&&&&& drivers/mtd/mtdchar.o & CC&&&&& drivers/mtd/mtd_blkdevs.o uudecode drivers/mtd/mtdblock-jz.uu -o drivers/mtd/mtdblock-jz.o make[2]: uudecode: Command not found make[2]: *** [drivers/mtd/mtdblock-jz.o] Error 127 make[1]: *** [drivers/mtd] Error 2 make: *** [drivers] Error 2 root :/opt/linux-2.6.24.3#
编译失败,没有找到uudecode,看君正的文档是因为缺sharutils这个包,于是安装之:
root :/opt/linux-2.6.24.3# apt-get install sharutils Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: & ssl-cert Use 'apt-get autoremove' to remove them. Suggested packages: & mailx The following NEW packages will be installed: & sharutils 0 upgraded, 1 newly installed, 0 to remove and 54 not upgraded. Need to get 109kB of archives. After this operation, 999kB of additional disk space will be used. Get:1
karmic/main sharutils 1:4.6.3-1build2 [109kB] Fetched 109kB in 4s (25.7kB/s) Selecting previously deselected package sharutils. (Reading database ... 42798 files and directories currently installed.) Unpacking sharutils (from .../sharutils_1%3a4.6.3-1build2_i386.deb) ... Processing triggers for install-info ... Processing triggers for man-db ... Setting up sharutils (1:4.6.3-1build2) ... Ignoring install-info called from maintainer script The package sharutils should be rebuild with new debhelper to get trigger support
root :/opt/linux-2.6.24.3# 再编译: root :/opt/linux-2.6.24.3# make uImage ... mipsel-linux-objcopy -O binary --remove-section=.reginfo --remove-section=.mdebug --remove-section=.comment --remove-section=.note --remove-section=.pdr --remove-
section=.options --remove-section=.MIPS.options vmlinux arch/mips/boot/vmlinux.bin rm -f arch/mips/boot/vmlinux.bin.gz gzip -9 arch/mips/boot/vmlinux.bin mkimage -A mips -O linux -T kernel -C gzip / &&&&&&&&&&&&&&& -a 0x -e 0x / &&&&&&&&&&&&&&& -n 'Linux-2.6.24.3' / &&&&&&&&&&&&&&& -d arch/mips/boot/vmlinux.bin.gz arch/mips/boot/uImage make[1]: mkimage: Command not found make[1]: *** [uImage] Error 127 make: *** [uImage] Error 2 root :/opt/linux-2.6.24.3# 这回缺mkimage,也是意料之中了,按君正的文档说明,从u-boot/tools目录下复制到/usr/sbin(放其它目录也行,只要在PATH范围内能找到),再编译: root :/opt/linux-2.6.24.3# cp ../u-boot-1.1.6/tools/mkimage /usr/sbin/ root :/opt/linux-2.6.24.3# make uImage & CHK&&&& include/linux/version.h & CHK&&&& include/linux/utsrelease.h & CALL&&& scripts/checksyscalls.sh & CHK&&&& include/linux/compile.h mipsel-linux-objcopy -O binary --remove-section=.reginfo --remove-section=.mdebug --remove-section=.comment --remove-section=.note --remove-section=.pdr --remove-
编译成功,可以按文档烧录到开发板上了。
烧录过程大概如下: 1.把上面编译好的的u-boot-nand.bin和uImage文件复制到USB_BOOT.exe目录 2.给开发板和PC接上小USB线,运行USB_BOOT.exe,出现命令行窗口 3.按住SW6,再按一下RESET键,松开SW6键,开发板进入白屏状态 4.输入&boot 0,屏幕上出现Configure success字样 5.擦除所有分区:&nerase 0
6.也可以单独探险uboot分区:&nerase 0 8 0 0 擦除uImage分区:&nerase 8 8 0 0 7.写uboot:&nprog 0 u-boot-nand.bin 0 0 -n
8.写uImage:&nprog 1024 uImage.bin 0 0 -n
屏幕日志输出如下:
&Welcome! &USB Boot Host Software! &USB Boot Software current version: 1.4b &Handling user command. &USBBoot :& boot 0
&Checking state of No.0 device: Unboot &Now booting No.0 device: &Download stage one program and execute at 0x: Pass &Download stage two program and execute at 0x80c00000: Pass &Boot success! &Now configure No.0 device: &Now checking whether all configure args valid: &Current device information: CPU is Jz4750 &Crystal work at 24MHz, the CCLK up to 336MHz and PMH_CLK up to 112MHz &Total SDRAM size is 16 MB, work in 4 bank and 32 bit mode &Nand page size 2048, ECC offset 24, bad block ID 127, use 1 plane mode &Configure success! &USBBoot :& nerase 0
&Erasing No.0 device No.0 flash...... Finish! &Operation end position : 4096 &Force erase ,no bad block infomation ! &USBBoot :& nprog 0 u-boot-nand.bin 0 0 -n
&Programing No.0 device... &Erasing No.0 device No.0 flash...... Finish! &Operation end position : 2 &Force erase ,no bad block infomation ! &Total size to send in byte is :266932 &Image type : without oob &It will cause 2 times buffer transfer. &No.1 Programming... Finish! Checking... pass! End at 128 &No.2 Programming... Finish! Checking... pass! End at 131 &USBBoot :& nprog 1024 uImage 0 0 -n
&Programing No.0 device... &Erasing No.0 device No.0 flash...... Finish! &Operation end position : 15 &Force erase ,no bad block infomation ! &Total size to send in byte is :1784168 &Image type : without oob &It will cause 7 times buffer transfer. &No.1 Programming... Finish! Checking... pass! End at 1152 &No.2 Programming... Finish! Checking... pass! End at 1280 &No.3 Programming... Finish! Checking... pass! End at 1408 &No.4 Programming... Finish! Checking... pass! End at 1536 &No.5 Programming... Finish! Checking... pass! End at 1664 &No.6 Programming... Finish! Checking... pass! End at 1792 &No.7 Programming... Finish! Checking... pass! End at 1896 &USBBoot :& exit
烧写完后,按RESET键重启,COM口输出如下:
NAND Secondary Program Loader
Starting U-Boot ...
U-Boot 1.1.6 (May 21 2009 - 16:21:12)
Board: Ingenic APUS (CPU Speed 336 MHz) DRAM:& 64 MB Flash:& 0 kB NAND:1024 MiB *** Warning - bad CRC or NAND, using default environment
In:&&& serial Out:&& lcd Err:&& lcd Net:&& JZ ETHERNET Hit any key to stop autoboot:& 0
NAND read: device 0 offset 0x200000, size 0x200000 &2097152 bytes read: OK ## Booting image at
... && Image Name:&& Linux-2.6.24.3 && Image Type:&& MIPS Linux Kernel Image (gzip compressed) && Data Size:&&& 1784104 Bytes =& 1.7 MB && Load Address:
&& Entry Point:&
&& Verifying Checksum ... OK && Uncompressing Kernel Image ... OK
Starting kernel ...
到这一步,内核已经编译启动成功,接下来是根文件系统的问题了。关于TFTP和NFS网络根文件系统下一篇再细述。龙芯1b linux下 程序用什么编译执行
Linux下C程序的编辑,编译和运行以及调试要使用的工具:编辑:vim(vi)编译和运行:gcc调试:gdb安装很简单(以下是以在CentOS中安装为例):1
yum vim gcc gdb
1.使用vim编辑源文件首先,打开终端练下手:1
vim hello.c
(进入一般模式)按下&i&,进入编辑模式,在编辑模式下输入:1
#include &stdio.h&
int main(){
printf(&Hello, World!\n&);
输入完成,按&ESC&键,回到一般模式,然后按下&:wq&,即可保存并退出vim。附注:在一般模式下,按下&:%!xxd&查看hello.c的16进制形式,回到文本格式按下&:%!xxd -r&。查看hello.c的二进制形式,按下&:%!xxd -b&,这是hello.c保存在磁盘上的存储状态。至此,在vim已完成C源文件的编辑。关于vim的使用,直接上网搜索vim,相关的文章是相当多的;或者参考vim的联机帮助,在命令行上键入&man vim&即可。2.编译和运行gcc命令的基本用法:1
gcc[options] [filenames]
其中,filenames为文件名;options为编译选项当不使用任何编译选项编译hello.c时,gcc将会自动编译产生一个a.out的可执行文件:1
[root@localhost c]# ls
[root@localhost c]# gcc hello.c
[root@localhost c]# ls
[root@localhost c]# ./a.out
Hello, World!
使用-o编译选择,可以为编译后的文件指定一个名字:1
[root@localhost c]# ls
[root@localhost c]# gcc hello.c -o hello
[root@localhost c]# ls
[root@localhost c]# ./hello
Hello, World!
注意:使用-o选项时,-o后面必须跟一个文件名,即:-o outfile。为了便于描述后面的选项,删除hello和a.out可执行文件。结合介绍gcc的编译选项,分析hello.c的编译和执行过程:(1)预处理阶段:使用-E选项,对输入文件只做预处理不编译。当使用这个选项时,预处理器的输出被送到标准输出而不是存储到文件。如果想将预处理的输出存储到文件,可结合-o选项使用,使用如下:1
[root@localhost c]# ls
[root@localhost c]# gcc -E hello.c -o hello.i
[root@localhost c]# ls
使用less查看下hello.i:1
[root@localhost c]# less hello.i
(2)编译阶段:使用-S选项,将C程序编译为汇编语言文件后停止编译,gcc编译产生汇编文件的默认后缀为.s。1
[root@localhost c]# ls
[root@localhost c]# gcc -S hello.c
[root@localhost c]# ls
在gcc -S hello.c处,使用C源文件编译,也可以用gcc -S hello.i的预处理文件编译,结果一样。使用-S编译时,也可以和-o结合使用指定编译产生的汇编语言文件的名字:1
[root@localhost c]# ls
[root@localhost c]# gcc -S hello.i -o hello_s.s
[root@localhost c]# ls
可使用less命令查看汇编代码。(3)汇编阶段:使用-c选项,将C源文件或者汇编语言文件编译成可重定向的目标文件(二进制形式),其默认后缀为.o。1
[root@localhost c]# ls
[root@localhost c]# gcc -c hello.s
[root@localhost c]# ls
也可以和-o结合使用指定编译产生的目标文件的名字:1
[root@localhost c]# gcc -c hello.s -o hello.o
由于hello.o是二进制文件,使用less查看显示为乱码;然后使用vim hello.o打开也显示为乱码,按下&:%!xxd&查看其16进制形式,按下&:%!xxd -r&退出 16进制查看模式,回到乱码状态。在退出vim时,若提示已经修改了文件,则使用&:q!&强制退出。(4)链接阶段:链接器将可重定向的目标文件hello.o以及库文件(如printf.o)执行并入操作,形成最终可执行的可执行目标文件。1
[root@localhost c]# ls
[root@localhost c]# gcc hello.o
[root@localhost c]# ls
可使用-o选项,指定输出文件(即可执行目标文件)的名字:1
[root@localhost c]# gcc hello.o -o hello
[root@localhost c]# ls
(5)执行阶段:1
[root@localhost c]# ./a.out
Hello, World!
[root@localhost c]# ./hello
Hello, World!
由此,看出前面使用的gcc hello.c -o hello命令,将hello.c直接编译为可执行的目标文件,中间经过于处理器的预处理阶段(源文件到预处理文件),编译器的编译阶段(预处理文件到汇编文件),汇编器的汇编阶段(汇编文件到可重定向的目标文件),链接器的链接阶段(可重定向的目标文件到可执行的目标文件)。还有其他的选项如下:-Idir:dir是头文件所在的目录-Ldir:dir是库文件所在的目录-Wall:打印所有的警告信息-Wl,options:options是传递给链接器的选项编译优化选项:-O和-O2-O选项告诉GCC 对源代码进行基本优化。这些优化在大多数情况下都会使程序执行的更快。-O2选项告诉GCC产生尽可能小和尽可能快的代码。-O2选项将使编译的速度比使用-O时慢。但通常产生的代码执行速度会更快。除了-O和-O2优化选项外,还有一些低级选项用于产生更快的代码。这些选项非常的特殊,而且最好只有当你完全理解这些选项将会对编译后的代码产生什么样的效果时再去使用。这些选项的详细描述,请参考GCC的联机帮助,在命令行上键入&man gcc&即可。调试选项:-g(使用详情见第3部分)-g选项告诉GCC产生能被GNU调试器使用的调试信息以便调试你的程序。即:在生成的目标文件中添加调试信息,所谓调试信息就是源代码和指令之间的对应关系,在gdb调试和objdump反汇编时要用到这些信息。3.调试虽然GCC提供了调试选项,但是本身不能用于调试。Linux 提供了一个名为gdb的GNU调试程序。gdb是一个用来调试C和C++程序的调试器。它使你能在程序运行时观察程序的内部结构和内存的使用情况。以下是gdb所提供的一些功能: a.它使你能监视你程序中变量的值;b.它使你能设置断点以使程序在指定的代码行上停止执行;c.它使你能一行行的执行你的代码。(1)启动gdb在命令行上键入&gdb&并按回车键就可以运行gdb了,如下:1
[root@localhost c]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type &show copying&
and &show warranty& for details.
This GDB was configured as &x86_64-redhat-linux-gnu&.
For bug reporting instructions, please see:&&.
当启动gdb之后,即可在命令行上输入命令进行相关的调试操作。也可以以下面的方式来启动gdb:1
[root@localhost c]# gdb hello
这种方式启动gdb,直接将指定调试的程序文件装载到调试环境中。也就是让gdb装入名称为filename的可执行文件,从而准备调试。为了能够进行调试,当前调试的程序文件中必须包含调试信息。其中调试信息包含程序中的每个变量的类型和其在可执行文件里的地址映射以及源代码的行号,gdb利用这些信息使源代码和机器码相关联。因此在使用gcc编译源程序的时候必须使用-g选项,以便将调试信息包含在可执行文件中。例如:1
[root@localhost c]# gcc -g hello.c -o hello
gdb还提供了其他的启动选项,请参考gdb的联机帮助。在命令行上键入&man gdb&并回车即可。(2)gdb基本命令&1&单步执行和跟踪函数调用程序编辑如下:01
#include &stdio.h&
int add_range(int low, int high){
for(i = i &= i++){
sum = sum +
int main(){
int result[100];
result[0] = add_range(1, 10);
result[1] = add_range(1, 100);
printf(&result[0] = %d\nresult[1] = %d\n&, result[0], result[1]);
编译和运行如下:1
[root@localhost gdb_demo]# vim test1.c
[root@localhost gdb_demo]# gcc test1.c -o test1
[root@localhost gdb_demo]# ls
[root@localhost gdb_demo]# ./test1
result[0] = 55
result[1] = 5105
我没玩过gcc,有提示几行错误么
scanf里边不能含有除格式之外的东西,像你这里有The th level.是不行的。把scanf这一句改为scanf(&%d&,&n);如果要显示输入的n的多少,就再加一句printf(&The %dth level.\n&,n);
看设置了,我的反正可以运行。不过 X 里面运行字符界面的程序有个问题就是 X 一般不会显示终端窗口,所以除非你的程序是窗口程序,不然什么显示都没有就运行完成退出了。 --------------有的 linux 有一个“在终端中运行”的项目,这个可以保留终端字符界面来显示。不过现在大部分都没有了。现在你似乎要么写界面,要么这个程序自己启动一个终端窗口来运行。
程序本身和编译环境没关系,在什么平台下编译就生成什么平台的文件。编译选项可能略有不同。
用linux自带的fortran编译器gfortran重新编译源程序就可以了。intel visual fortran 也有linux版本,在网上找找看。论坛的fortran版上有讨论这个问题的帖子,你搜一下应该能找到相关下载。
没有设置执行权限吧,chmod a+x 程序
程序安装的目的是为了把执行程序放到执行路径下面,把配置文件放到程序可读取的位置,把需要的链接库放到可找到的位置,配置一些需要预先设置的参数等等。如果一个程序这些都不需要,那么直接把可执行程序放到该在的目录就可以了。
人的综合分
感谢您为社区的和谐贡献力量请选择举报类型
经过核实后将会做出处理感谢您为社区和谐做出贡献
确定要取消此次报名,退出该活动?}

我要回帖

更多关于 龙芯电脑 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信