虚拟机下编程实验_51.docx_第1页
虚拟机下编程实验_51.docx_第2页
虚拟机下编程实验_51.docx_第3页
虚拟机下编程实验_51.docx_第4页
虚拟机下编程实验_51.docx_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

虚拟机下编程实验 黄华振 51 (2012032511) 2014.10.13 一、 安装虚拟机软件:VMWare二、 打开虚拟机双击“打开虚拟机”首先将UbuntuKylin64.rar解压,然后找到此目录下虚拟系统文件并打开单击“开启此虚拟机”输入密码:123456进入UbuntuKylin系统:单击“终端”图标,进入终端窗口:三、 编程实验(参考课件:Linux 讲座(5)-编程.ppt, Python编程.txt )(一)Bash脚本编程1、编程vim greetings.sh,内容如下:#!/bin/bash# This is the first Bash shell program # Scriptname: greetings.shechoecho e Hello $LOGNAME, cecho its nice talking to you.echo e Your present working directory is:pwd # Show the name of present directoryechoecho The time is date +%T!. nByeecho注:若vim不能用说明没有安装,用如下命令安装:Sudo apt-get install vim 2、运行$ sh 脚本名$chmod +x脚本名$./脚本名例如:$sh greetings.sh$chmod +x greetings.sh #将此文件属性设置为可执行状态$./greetings.sh$ cp greetings.sh /usr/bin/ greetings #放置到命令搜索路径目录下$greetings #如Linux的命令一样用3、脚本调试sh x 脚本名 #先执行替换,然后显示,再执行它sh v 脚本名 #在执行脚本之前,按输入的原样打印脚本中的各行 sh n 脚本名 #对脚本进行语法检查,但不执行脚本(二)C/C+编程目的与要求:q 掌握C/C+编程的基本步骤q 学会多文件的编程方法q 学会 makefile编写q 学会静态库与动态库的生成与链接 1、检查与安装(1) 检查是否安装有gcc与g+$which gcc会显示安装的路径与命令文件名:/usr/bin/gcc$which g+不显示任何信息,说明没有安装(2) 安装g+$sudo apt-get install g+2、编程基本方法:n 单文件时的编程过程:a) 编辑程序,使用vi/geditb) 编译 $gcc -o hello hello.c$g+ -o hello hello.cppc) 运行 $./hellon 多文件时的编程过程:a) 编程b) 制作 makefilec) 执行 maked) 运行 例子1,2:hello.c/hello.cpp/* file:hello.c */#include int main() printf(“Hello,World!n”); return 0;/* file: hello.cpp */#include int main() cout“Hello,World!”endl;return 0; 3、多文件编程实验q 命令方式编译链接生成可运行文件q 利用makefile生成可运行文件q 生成静态库,利用静态库链接生成可运行文件q 生成动态库,利用动态库链接生成可运行文件q 如何让利用动态库链接生成可运行文件找到动态链接库程序如下:在/home/zhyuan/mytools/ 目录下,有文件: main.c 主文件 mytool1.h mytool1.c 函数子文件1 mytool2.h mytool2.c 函数子文件2程序内容如下:/* file: main.c */#include mytool1.h#include mytool2.h main() mytool1_print(hello); mytool2_print(hello);/* mytool1.h */#ifndef _MYTOOL_1_H /*条件编译*/#define _MYTOOL_1_H#include /* 函数定义说明 */void mytool1_print(char * print_str);#endif/* file:mytool1.c */#include mytool1.h“void mytool1_print(char * print_str) printf(This is mytool1 print %sn, print_str);/* mytool2.h */#ifndef _MYTOOL_2_H /*条件编译*/#define _MYTOOL_2_H#include /* 函数定义说明 */void mytool2_print(char * print_str);#endif/* file:mytool2.c */#include mytool2.hvoid mytool2_print(char * print_str) printf(This is mytool2 print %sn, print_str);利用命令行方式:$ lsmain.c mytool1.h mytool1.c mytool2.h mytool2.c $ gcc -c mytool1.c$ gcc -c mytool2.c$ gcc -c main.c$ gcc -o myprint main.o mytool1.o mytool2.o$ ./myprintThis is mytool1 print helloThis is mytool2 print hellorootlocalhost zhyuan$ lsmyprint main.o mytool1.o mytool2.o main.c mytool1.h mytool1.c mytool2.h mytool2.c利用makefile文件进行编译$ vim makefile #编制makefile文件# file: makefileprinthello:main.o mytool1.o mytool2.o gcc -o printhello main.o mytool1.o mytool2.omain.o:main.c mytool1.h mytool2.h gcc -c main.cmytool1.o:mytool1.c mytool1.h gcc -c mytool1.cmytool2.o: mytool2.c mytool2.h gcc -c 注意:makefile只有行注释符,注释用“”,另外注意在命令前要用间隔,如:tabgcc -c mytool2.c$ make$ ./printhelloThis is mytool1 print helloThis is mytool2 print hello$ makemake: “printhello”是最新的。4、静态库与动态库编程实验生成静态库文件,然后进行连接n 为了程序版权问题,也是为了源代码的保密,所以一般只提供函数的头文件和库文件。如:mytool1.c mytool2.c 没有提供给您 ,只提供了静态库libmytools.a和头文件mytool1.h, mytool2.h n 如何生成静态库: $ ar rcs libmytools.a mytool1.o mytool2.on 首先编译生成mytool1.o mytool2.o,若未生成,用如下命令生成:rootlocalhost zhyuan$ gcc -c mytool1.crootlocalhost zhyuan$ gcc -c 上命令生成了静态库文件:libmytools.a 如何使用静态库进行链接生成可运行文件: $ gcc -o printhello main.o -static -L. -lmytools $ lslibmytools.a main.o mytool1.c mytool1.o mytool2.h printhellomain.c makefile mytool1.h mytool2.c mytool2.o $ ./printhello #运行程序 This is mytool1 print hello This is mytoo21 print hello生成动态库文件,然后进行连接n 如何生成动态库:$ gcc -fPIC -shared -o libmytools.so mytool1.o mytool2.o或者: $ gcc -shared -Wl,-soname,libmytools.so.1.0 -o libmytools.so.1.0.1 mytool1.o mytool2.o$ ln s libmytools.so.1.0.1 libmytools.so注:使用第二种方法考虑到版本升级问题n 如何使用动态库进行链接生成可运行文件: $ gcc -o printhello main.o -L. -lmytoolsn 运行$./ printhello 有可能不能执行,原因找不到动态库n 如何解决找不到动态库的问题?调用动态库的时候有几个问题会经常碰到,有时,明明已经将库的头文件所在目录 通过 “-I” include进来了,库所在文件通过 “-L”参数引导,并指定了“-l”的库名,但通过ldd命令察看时,就是死活找不到你指定链接的so文件,这时你要作的就是通过修改 LD_LIBRARY_PATH或者/etc/ld.so.conf文件来指定动态库的目录。通常这样做就可以解决库无法链接的问题了。 Linux中加载动态链接库的三种方法(i)修改系统文件:/etc/ld.so.conf文件,此文件指定了默认的动态链接库查找路径.(必须是超级用户)只需要把需要的路径加到/etc/ld.so.conf.d目录下的任何一个文件中,再运行 ldconfig就可以了 。(ii)、运用变量LDLIBRARYPATH: 把需要添加的路径加入到LDLIBRARYPATH中,注意如果多于一个要用冒号隔开。如:export LD_LIBRARY_PATH=/usr/local/lib/minigui(iii)、编译的时候设定: 在编译源码的时候可以用参数:-Wl, -rpath指定动态搜索的路径即可。(三) Python编程1、检查与安装(1) 检查是否安装$which python会显示安装的路径与命令文件名:/usr/bin/python(2) 查看其版本信息$python version会显示Python 2.7.62、Python编程实验(1)、在Ubuntu测试是否安装了python,一般而言默认是已经安装的。$ python -VPython 2.7.1+说明已经安装!(2)、编制第一个程序:打印“Hello,world!”进入python$ python显示:Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) GCC 4.5.2 on linux2Type help, copyright, credits or license for more information.键入命令:print Hello,world!,如下形式: print Hello,world!显示结果:Hello,world!(3)、退出pythonUse exit() or Ctrl-D (i.e. EOF) to exit exit() 或者使用Ctrl-D键结果输入,返回Linux终端状态 $_当你学习一种新的编程语言的时候,你编写运行的第一个程序通常都是“Hello World”程序,这已经成为一种传统了。在你运行“Hello World”程序的时候,它所做的事只是说声:“Hello World”。正如提出“Hello World”程序的Simon Cozens1所说:“它是编程之神的传统咒语,可以帮助你更好的学习语言。”启动你选择的编辑器,输入下面这段程序,然后把它保存为helloworld.py。(4)、编制python源文件#!/usr/bin/python# Filename : helloworld.pyprint Hello World(源文件:code/helloworld.py)(5)、运行python源程序为了运行这个程序,请打开shell(Linux终端或者DOS提示符),然后键入命令python helloworld.py。如下所示:$ python helloworld.py输出:Hello World 或者:$ chmod a+x helloworld.py$ ./helloworld.pyHello World另外,你还可以把你的文件名改成仅仅helloworld,然后运行./helloworld还可以将它复制到可搜索的目录路径下,就可以随处执行了。 $ echo $PATH /opt/mono/bin/:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论