当前位置: 首页 > news >正文

【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

转载于:https://www.cnblogs.com/alex-bn-lee/p/11073119.html

相关文章:

  • 那些年我们一起遗忘的位运算!
  • Pots bfs()记录每一种状态,直到求出最优值
  • .Net Remoting(分离服务程序实现) - Part.3
  • ssh架构简单解释和vo po解释
  • [NOIP2018 PJ T4]对称二叉树
  • 移动互联网时代的人才管理新思维之学习笔记
  • Codeforces Round #159 (Div. 2) B. Playing Cubes
  • Gridview常用技巧
  • Open xml 操作Excel 透视表(Pivot table)-- 实现Excel多语言报表
  • Delphi 数据类型列表
  • 在struts1.1框架下,利用smartupload实现文件的上传(可以是多个文件)
  • [转帖]三星F488E的JAVA安装方法
  • UICheckBox 用法解析
  • MySQL笔记系列:数据库概述
  • JOIN 和 WHERE?简单的问题也有学问。
  • python3.6+scrapy+mysql 爬虫实战
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • 2017前端实习生面试总结
  • Github访问慢解决办法
  • javascript从右向左截取指定位数字符的3种方法
  • Java方法详解
  • Java教程_软件开发基础
  • JS笔记四:作用域、变量(函数)提升
  • laravel with 查询列表限制条数
  • mysql 5.6 原生Online DDL解析
  • mysql中InnoDB引擎中页的概念
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • TCP拥塞控制
  • V4L2视频输入框架概述
  • 二维平面内的碰撞检测【一】
  • 计算机在识别图像时“看到”了什么?
  • 解决iview多表头动态更改列元素发生的错误
  • 警报:线上事故之CountDownLatch的威力
  • 王永庆:技术创新改变教育未来
  • 用Python写一份独特的元宵节祝福
  • Nginx实现动静分离
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 阿里云服务器如何修改远程端口?
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • #vue3 实现前端下载excel文件模板功能
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (9)STL算法之逆转旋转
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (转)编辑寄语:因为爱心,所以美丽
  • .Mobi域名介绍
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .net framework profiles /.net framework 配置
  • .net mvc部分视图
  • .NET 反射 Reflect
  • .NET精简框架的“无法找到资源程序集”异常释疑
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现