【412】Linux 系统编译 C 程序
1. 直接编译,会自动生成 a.out 文件,此文件即为可执行文件
# 编译 *.c 文件,生成可执行文件 a.out
gcc euclide.c # 直接运行如下,如果没有输入和输出文件的话
# < input.txt
# > output.txt
# 可以增加参数值
./a.out
下面代码可以生成相同用户名的 .o 文件,生成 getchar.o 文件
# -c - Compile and assemble, but do not link.
gcc -c getchar.c
下面代码可以生成指定名称的 executable,生成 getchar 文件
# -o <file> - Place the output into <file>.
gcc getchar.c -o getchar # 也可以
gcc -o getchar getchar.c
2. 运行时间计算,使用 time 命令
The time command returns:
- real = time you wait (not useful, systems multitask), also called clock time
- user = CPU time when your program is actually running
- sys = CPU time taken in system calls (your program is not running but is still using time)
Hence the user + sys time is the actual time taken by the program.
代码如下
alex@alex-VirtualBox:~/Documents/COMP9024/Lectures/Week01$ gcc euclid.c
alex@alex-VirtualBox:~/Documents/COMP9024/Lectures/Week01$ ./a.out
1
alex@alex-VirtualBox:~/Documents/COMP9024/Lectures/Week01$ time ./a.out
1 real 0m0.002s
user 0m0.001s
sys 0m0.001s
alex@alex-VirtualBox:~/Documents/COMP9024/Lectures/Week01$ date
Mon 24 Jun 15:38:06 AEST 2019
alex@alex-VirtualBox:~/Documents/COMP9024/Lectures/Week01$ time date
Mon 24 Jun 15:38:08 AEST 2019 real 0m0.001s
user 0m0.000s
sys 0m0.001s
alex@alex-VirtualBox:~/Documents/COMP9024/Lectures/Week01$ time sleep 3 real 0m3.015s
user 0m0.002s
sys 0m0.000s
3. 写的话就正常写,跟 Windows 也没啥区别,虽然 gedit 显示的很舒服,但是没有语法提示。。

编译的话就通过命令实现:
# 编译使用 gcc
# fgets.c 为 源代码
# -o
# fgets 为可执行文件
gcc fgets.c -o fgets
程序运行如下:
# fgets 为可执行文件,注意调用的时候需要添加 ./ 的部分
# < test.txt 表示文件内容标准输入,注意此时没有 ./ 的部分
# 不造为什么哈,也懒得查啦。。
./fgets < test.txt
Ubuntu 运行效果:

4. echo 输出内容
Reminder: the echo command just sends the string arguments to stdout (i.e. the screen here)
alex@alex-VirtualBox:~/Documents/COMP9024/Lectures/Week01$ echo love is blind
love is blind
作为输入参数,echo 输出的 owl,作为 后面程序的输入参数
// prog.c
#include <stdio.h> int main(){
char a[10];
fgets(a, 10, stdin); printf("%s\n", a); return 0;
}
编译效果如下,如果直接运行文件,会提示字符串输入
# 编译
gcc prog.c
# 输出 owl 作为 后面可执行文件的输入参数
echo owl | ./a.out
# input 里面存储的就是 owl,可以实现与上面一样的效果
./a.out < input
5. 获取帮助
可以通过 命令名称 --help
或者 help 命令名称 来实现
gcc --help help echo
6. There are many ways to 'test' a program that reads stdin.
- scanf()
- getchar()
- Using the keyboard
直接输入相应的内容 - Using a data file
将内容写入到文件中,通过小于号实现导入文件内容 - Using a pipe command. A pipe command joins the stdout of a program to the stdin of another program.
通过竖线隔开,左边为输出,然后将其输入到右边
# Using the keyboard prompt$ dcc -o counts counts.c
prompt$ ./counts
10
1 2 3 4 5 6 7 8 9 10 #where the integer 10 was typed on the keyboard by the user, and the program generates #the count from 1 to 10. #Using a data file, input.txt say, which contains the integer 10 (followed by a newline). prompt$ more input.txt
10 prompt$ ./counts < input.txt
1 2 3 4 5 6 7 8 9 10 #Using a pipe command. A pipe command joins the stdout of a program to the stdin of #another program. If we have a program called write10.c:
#then we can pipe its stdout to the stdin of our counting program prompt$ dcc -o write10 write10.c
prompt$ dcc -o counts counts.c
prompt$ ./write10 | ./counts
1 2 3 4 5 6 7 8 9 10 #But you can actually generate a string much more easily in UNIX using echo prompt$ echo "10" | ./counts
1 2 3 4 5 6 7 8 9 10
7. shell 命令
- more:用来查看文件内容
- zip:用来压缩文件
- -r Option:【文件夹】 To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.
$zip –r filename.zip directory_name - 参考:ZIP command in Linux with examples
- -r Option:【文件夹】 To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.
【412】Linux 系统编译 C 程序的更多相关文章
- 在Linux系统如何让程序开机时自动启动
在Linux系统如何让程序开机时自动启动 核心提示:系统的服务在开机时一般都可以自动启动,那在linux系统下如果想要程序在开机时自动启动怎么办?我们知道在 windows系统“开始”--& ...
- Ubuntu系统---编译opencv程序的几种方式g++、Makefile、Cmake
Ubuntu系统---编译opencv程序的几种方式g++.Makefile.Cmake 先建立一个工程(一个文件夹),写好xxx.cpp文件,可以是多个: //----------opencv.cp ...
- 如何选择合适的Linux系统进行桌面程序开发?
32 or 64 ? 众所周知,64位的Windows系统可以近乎完美地运行32位的应用程序,微软出于商业考虑做了这样一个兼容层.而Linux系统则划分的很清楚,默认情况下64位的Linux系统无法运 ...
- linux 系统shell运行程序不退出
如果通过ssh远程连接到linux系统终端,在shell下执行程序.假如程序名称为app,且程序本身会一直执行不退出,程序执行需要参数文件paramfile. 当我们用 ./app paramfile ...
- 在linux下编译线程程序undefined reference to `pthread_create'
由于是Linux新手,所以现在才开始接触线程编程,照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误:undefined reference to 'pthread_create'u ...
- 如何使Linux系统上的程序开机后自动运行 (转)
Linux有自己一套完整的启动体系,抓住了Linux启动的脉络,Linux的启动过程将不再神秘. 阅读之前建议先看一下附图. 本文中假设inittab中设置的init tree为: /etc/rc.d ...
- linux系统编译安装软件的通用步骤
编译安装的步骤: 1.下载源代码,并解压 tar -xf package-version.tar.{gz|bz2|xz} 注意:展开后的目录通常为package-version 2.切换至源码 ...
- LFS,编译自己的Linux系统 - 编译临时系统
编译GCC-4.8.2 PASS 1 解压并重命名 cd /mnt/lfs/sources tar -Jxf ../mpfr-3.1.2.tar.xz mv mpfr-3.1.2 mpfr tar - ...
- Linux下编译C++程序遇到错误:undefined reference to `*::*
“undefined reference to”的意思是,该函数未定义. 如果使用的是g++,有以下检查方案: 如果提示未定义的函数是某个库的函数.检查库是否已经安装,并在编译命令中采用-l和-L参数 ...
随机推荐
- Codevs 3409 搬礼物
时间限制: 1 s 空间限制: 64000 KB 题目等级 : 青铜 Bronze 题目描述 Description 小浣熊松松特别喜欢交朋友,今年松松生日,就有N个朋友给他送礼物.可是要把这些礼 ...
- BZOJ1696: [Usaco2007 Feb]Building A New Barn新牛舍
n<=10000个点(xi,yi),找到一个不同于给出的所有点的点,使得该点到所有点的曼哈顿距离最小并找出这样的点的个数. 第一眼看上去这不是中位数嘛,奇数一个点偶数一片,然后找一下这篇区域有几 ...
- ES6__变量的解构赋值
/* 变量的解构赋值 */ /* 基本概念 : 本质上就是一种匹配模式,只要等号两边的模式相同,那么左边的变量就可以被赋予对应的值. 结构赋值主要分为: 1. 数组的解构赋值 2. 对象的结构赋值 3 ...
- ES6__数据结构 Set
/* 数据结构 Set */ /* *集合的基本概念:集合是由一组无序且唯一(即不能重复)的项组成的.这个数据结构使用了与有限集合相同的数学概念,应用在计算机的数据结构中. *特点:key 和 val ...
- msp430项目编程17
msp430中项目---红外遥控系统 1.定时器工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习
- Borg Maze-POJ3026(bfs+最小生成树)
http://poj.org/problem?id=3026 如果一个一个普通搜处理不好的话会超时 可以连到一块搜 我觉得这个方法特别好 #include<stdio.h> #inclu ...
- 2018 11.1 PION 模拟赛
期望:250 100+100+50 实际:210 80+100+30 期望:100 实际:80 最后:两个点T了.可能是求逆元的方法太慢了,也可能是闲的又加了一个快速乘的原因. #inclu ...
- Java SpringMVC实现PC端网页微信扫码支付完整版
一:前期微信支付扫盲知识 前提条件是已经有申请了微信支付功能的公众号,然后我们需要得到公众号APPID和微信商户号,这个分别在微信公众号和微信支付商家平台上面可以发现.其实在你申请成功支付功能之后,微 ...
- Linux 网络工具
1 nethogs nethogs 是一个免费的工具,当要查找哪个 PID (注:即 process identifier,进程 ID) 给你的网络流量带来了麻烦时,它是非常方便的.它按每个进程来分组 ...
- How to Uninstall Internet Explorer 11 for Windows 7
Internet Explorer 11 is the newest version of Microsoft's web browser, but not everyone is a fan. If ...