linux系统的编译命令怎么用
linux系统的编译命令是“Make”。在linux系统中,make是一个非常重要的编译命令,管理员用它通过命令行来编译和安装很多开源的工具,程序员用它来管理他们大型复杂的项目编译问题。Make被用于自动化编译大型程序的任务,它可以自动检测程序中需要重新编译的部分,并下发相应的编译指令。
Make简介
make 是 linux 系统的实用程序。它被用来管理大型程序的自动编译任务,自动判断程序的哪个部分需要重新编译,并发送编译指令。虽然,我们最常见于 C 语言程序的编译。但是,make 不限于某一特定语言,凡是可以通过 shell 命令来运行编译器的语言都可以使用 make 。除此之外,你甚至可以用 make 描述任何构建任务,这些任务中,文件需要在其依赖的文件发生变动后自动更新。
Make 如何工作的
Make 命令对于不熟悉其背后机理的人来说,就像命令行参数一样接受目标。通常,这些操作被存储在名为“Makefile”的特殊文件中,并且与目标相对应。更多信息,阅读关于 Makefiles 如何工作的系列文章。
Make命令在第一次执行时,会扫描Makefile以查找目标和相应的依赖关系。如果这些依赖项也需要被编译成目标,就要继续扫描 Makefile 并建立它们的依赖关系,接着进行编译。一旦主要依赖项完成编译,就会编译主目标(这是通过make命令输入的)。
现在,假设你对某个源文件进行了修改,你再次执行 make 命令,它将只编译与该源文件相关的目标文件,因此,编译完最终的可执行文件节省了大量的时间。
>Make 命令实例
下面是本文所使用的测试环境:
OS —— Ubunut 13.04 Shell —— Bash 4.2.45 Application —— GNU Make 3.81
下面是工程的内容:
$ ls anotherTest.c Makefile test.c test.h
下面是 Makefile 的内容:
all: test test: test.o anotherTest.o gcc -Wall test.o anotherTest.o -o testtest.o: test.c gcc -c -Wall test.c anotherTest.o: anotherTest.c gcc -c -Wall anotherTest.c clean: rm -rf *.o test
现在我们来看 Linux 下一些 make 命令应用的实例:
1. 一个简单的例子
为了编译整个工程,你可以简单的使用 make
或者在 make 命令后带上目标 all
。
$ make gcc -c -Wall test.c gcc -c -Wall anotherTest.c gcc -Wall test.o anotherTest.o -o test
你能看到 make 命令第一次创建的依赖以及实际的目标。
如果你再次查看目录内容,里面多了一些 .o 文件和执行文件:
$ ls anotherTest.c anotherTest.o Makefile test test.c test.h test.o
现在,假设你对 test.c 文件做了一些修改,重新使用 make 编译工程:
$ make gcc -c -Wall test.c gcc -Wall test.o anotherTest.o -o test
你可以看到只有 test.o 重新编译了,然而另一个 Test.o 没有重新编译。
现在清理所有的目标文件和可执行文件 test,你可以使用目标 clean
:
$ make clean rm -rf *.o test$ ls anotherTest.c Makefile test.c test.h
你可以看到所有的 .o 文件和执行文件 test 都被删除了。
2. 通过 -B 选项让所有目标总是重新建立
到目前为止,你可能注意到 make 命令不会编译那些自从上次编译之后就没有更改的文件,但是,如果你想覆盖 make 这种默认的行为,你可以使用 -B 选项。
下面是个例子:
$ make make: Nothing to be done for `all’.$ make -B gcc -c -Wall test.c gcc -c -Wall anotherTest.c gcc -Wall test.o anotherTest.o -o test
你可以看到尽管 make 命令不会编译任何文件,然而 make -B
会强制编译所有的目标文件以及最终的执行文件。
3. 使用 -d 选项打印调试信息
如果你想知道 make 执行时实际做了什么,使用 -d 选项。
这是一个例子:
$ make -d | more GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for x86_64-pc-linux-gnu Reading makefiles… Reading makefile `Makefile’… Updating makefiles…. Considering target file `Makefile’. Looking for an implicit rule for `Makefile’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.o’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.c’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.cc’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.C’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.cpp’. Trying pattern rule with stem `Makefile’. --More--
这是很长的输出,你也看到我使用了 more
命令来一页一页显示输出。
4. 使用 -C 选项改变目录
你可以为 make 命令提供不同的目录路径,在寻找 Makefile 之前会切换目录的。
这是一个目录,假设你就在当前目录下:
$ ls file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt
但是你想运行的 make 命令的 Makefile 文件保存在 ../make-dir/ 目录下,你可以这样做:
$ make -C ../make-dir/ make: Entering directory `/home/himanshu/practice/make-dir’ make: Nothing to be done for `all’. make: Leaving directory `/home/himanshu/practice/make-dir
你能看到 make 命令首先切到特定的目录下,在那执行,然后再切换回来。
5. 通过 -f 选项将其它文件看作 Makefile
如果你想将重命名 Makefile 文件,比如取名为 my_makefile 或者其它的名字,我们想让 make 将它也当成 Makefile,可以使用 -f 选项。
make -f my_makefile
通过这种方法,make 命令会选择扫描 my_makefile 来代替 Makefile。
以上是linux系统的编译命令怎么用的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

如何解决 Nginx 403 Forbidden 错误?检查文件或目录权限;2. 检查 .htaccess 文件;3. 检查 Nginx 配置文件;4. 重启 Nginx。其他可能原因还包括防火墙规则、SELinux 设置或应用程序问题。

问题的答案:304 Not Modified 错误表示浏览器已缓存客户端请求的最新资源版本。解决方案:1. 清除浏览器缓存;2. 禁用浏览器缓存;3. 配置 Nginx 允许客户端缓存;4. 检查文件权限;5. 检查文件哈希;6. 禁用 CDN 或反向代理缓存;7. 重启 Nginx。

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

错误日志位于 /var/log/nginx(Linux)或 /usr/local/var/log/nginx(macOS),使用命令行清理步骤:1. 备份原日志;2. 创建空文件作为新日志;3. 重启 Nginx 服务。也可使用第三方工具(如 logrotate)或配置自动清理。
