如何在 Unix 控制台/Mac 终端中编译和运行 C/C++?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/221185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:49:40  来源:igfitidea点击:

How to compile and run C/C++ in a Unix console/Mac terminal?

c++cmacosconsoleterminal

提问by P-A

How can I compile/run C or C++ in Unix console or a Mac terminal?

如何在 Unix 控制台或 Mac 终端中编译/运行 C 或 C++?

(I know it, forget it, and relearn it again. Time to write it down.)

(我知道了,忘记了,重新学习。是时候写下来了。)

回答by camh

If it is a simple single source program:

如果是简单的单源程序:

make foo

where the source file is foo.c or foo.cpp, etc.

其中源文件是 foo.c 或 foo.cpp 等。

You dont even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus extension.

您甚至不需要生成文件。Make 有足够的内置规则将您的源文件构建为同名的可执行文件,减去扩展名。

Running the executable just built is the same as running any program - but you will most often need to specify the path to the executable as the shell will only search what is in $PATHto find executables, and most often that does not include the current directory (.).

运行刚刚构建的可执行文件与运行任何程序相同 - 但您通常需要指定可执行文件的路径,因为 shell 只会搜索其中的内容$PATH以查找可执行文件,并且通常不包括当前目录(.)。

So to run the built executable foo:

所以要运行构建的可执行文件foo

./foo

回答by Andrey Neverov

gcc main.cpp -o main.out  
./main.out

回答by Komengem

This is the command that works on all Unix machines... I use it on Linux/Ubuntu, but it works in OS X as well. Type the following command in Terminal.app.

这是适用于所有 Unix 机器的命令......我在 Linux/Ubuntu 上使用它,但它也适用于 OS X。在Terminal.app 中键入以下命令。

$ g++ -o lab21 iterative.cpp

-ois the letter O not zero

-o字母 O 不是零吗

lab21will be your executable file

lab21将是你的可执行文件

iterative.cppis your c++ file

iterative.cpp是你的 C++ 文件

After you run that command type the following in terminal to run your program:

运行该命令后,在终端中键入以下内容以运行您的程序:

$ ./lab21

回答by Victor Augusto

Two steps for me:

对我来说两个步骤:

first:

第一的:

make foo

then:

然后:

./foo

回答by orj

All application execution in a Unix (Linux, Mac OS X, AIX, etc.) environment depends on the executable search path.

Unix(Linux、Mac OS X、AIX 等)环境中的所有应用程序执行都依赖于可执行文件搜索路径。

You can display this path in the terminal with this command:

您可以使用以下命令在终端中显示此路径:

echo $PATH

回声 $PATH

On Mac OS X (by default) this will display the following colon separated search path:

在 Mac OS X 上(默认情况下),这将显示以下冒号分隔的搜索路径:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

So any executable in the listed directories can by run just by typing in their name. For example:

因此,列出目录中的任何可执行文件都可以通过键入其名称来运行。例如:

cat mytextfile.txt

cat mytextfile.txt

This runs /bin/catand displays mytextfile.txt to the terminal.

这会运行/bin/cat并将 mytextfile.txt 显示到终端。

To run any other command that is not in the executable search path requires that you qualify the path to the executable. So say I had an executable called MyProgram in my home directory on Mac OS X I can fully qualify it like so:

要运行不在可执行文件搜索路径中的任何其他命令,您需要限定可执行文件的路径。所以说我在 Mac OS XI 的主目录中有一个名为 MyProgram 的可执行文件可以像这样完全限定它:

/Users/oliver/MyProgram

/用户/奥利弗/我的程序

If you are in a location that is near the program you wished to execute you can qualify the name with a partial path. For example, if MyProgramwas in the directory /Users/oliver/MyProjectI and I was in my home directory I can qualify the executable name like this, and have it execute:

如果您位于要执行的程序附近的位置,则可以使用部分路径限定名称。例如,如果MyProgram在目录/Users/oliver/MyProjectI 中并且我在我的主目录中,我可以像这样限定可执行文件名称,并让它执行:

MyProject/MyProgram

我的项目/我的程序

Or say I was in the directory /Users/oliver/MyProject2and I wanted to execute /Users/oliver/MyProject/MyProgramI can use a relative path like this, to execute it:

或者说我在目录中/Users/oliver/MyProject2并且我想执行/Users/oliver/MyProject/MyProgram我可以使用这样的相对路径来执行它:

../MyProject/MyProgram

../我的项目/我的程序

Similarly if I am in the same directory as MyProgramI need to use a "current directory" relative path. The current directory you are in is the period character followed by a slash. For example:

同样,如果我在同一目录中,因为MyProgram我需要使用“当前目录”相对路径。您所在的当前目录是句点字符后跟斜杠。例如:

./MyProgram

./我的程序

To determine which directory you are currently in use the pwdcommand.

要确定您当前使用的目录,请使用该pwd命令。

If you are commonly putting programs in a place on your hard disk that you wish to run without having to qualify their names. For example, if you have a "bin" directory in your home directory for regularly used shell scripts of other programs it may be wise to alter your executable search path.

如果您通常将程序放在硬盘上的某个位置,您希望无需限定其名称即可运行。例如,如果您的主目录中有一个“bin”目录,用于其他程序的经常使用的 shell 脚本,那么更改您的可执行搜索路径可能是明智的。

This can be does easily by either creating or editing the existing .bash_profilefile in your home directory and adding the lines:

这可以通过.bash_profile在主目录中创建或编辑现有文件并添加以下行来轻松完成:

#!/bin/sh
export PATH=$PATH:~/bin

Here the tilde (~) character is being used as a shortcut for /Users/oliver. Also note that the hash bang (#!) line needs to be the first line of the file (if it doesn't already exist). Note also that this technique requires that your login shell be bash (the default on Mac OS X and most Linux distributions). Also note that if you want your programs installed in ~/binto be used in preference to system executables your should reorder the export statement as follows:

这里波浪号 (~) 字符被用作 /Users/oliver 的快捷方式。另请注意,哈希爆炸 (#!) 行必须是文件的第一行(如果尚不存在)。另请注意,此技术要求您的登录 shell 是 bash(Mac OS X 和大多数 Linux 发行版上的默认设置)。另请注意,如果您希望安装的程序~/bin优先于系统可执行文件使用,则应按如下方式重新排序导出语句:

export PATH=~/bin:$PATH

回答by nerdwaller

Ryan, I am changing this to be an answer instead of a comment, since it appears I was too brief. Do all of this in "Terminal".

Ryan,我正在将其更改为答案而不是评论,因为我似乎太简短了。在“终端”中执行所有这些操作。

To use the G++ compiler, you need to do this:

要使用 G++ 编译器,您需要执行以下操作:

  1. Navigate to the directory in which you stored the *.cpp file.

    cd ~/programs/myprograms/
    (the ~ is a shortcut for your home, i.e. /Users/Ryan/programs/myprograms/, replace with the location you actually used.)

  2. Compile it

    g++ input.cpp -o output.bin(output.bin can be anything with any extension, really. bin is just common on unix.)

    There should be NOTHING returned if it was successful, and that is okay. Generally you get returns on failures.

    However, if you type ls, you will see the list of files in the same directory. For example you would see the other folders, input.cpp and output.bin

  3. From inside the directory, now execute it with ./outbut.bin

  1. 导航到您存储 *.cpp 文件的目录。

    cd ~/programs/myprograms/
    (~是你家的快捷方式,即/Users/Ryan/programs/myprograms/,替换为你实际使用的位置。)

  2. 编译它

    g++ input.cpp -o output.bin(output.bin 可以是任何扩展名,真的。bin 在 unix 上很常见。)

    如果成功,应该没有返回任何东西,这没关系。通常,您会在失败时获得回报。

    但是,如果您键入ls,您将看到同一目录中的文件列表。例如,您会看到其他文件夹 input.cpp 和 output.bin

  3. 从目录内部,现在执行它 ./outbut.bin

回答by markthethomas

A compact way to go about doing that could be:

一种紧凑的方式来做到这一点可能是:

make foo && ./$_

Nice to have a one-liner so you can just re-run your executable again easily.

很高兴有一个单行程序,这样您就可以轻松地重新运行您的可执行文件。

回答by P-A

Assuming the current directory is not in the path, the syntax is ./[name of the program].

假设当前目录不在路径中,则语法为./[name of the program].

For example ./a.out

例如 ./a.out

回答by Nazgob

Add following to get best warnings, you will not regret it. If you can, compile WISE (warning is error)

添加以下内容以获得最佳警告,您不会后悔。如果可以,请编译 WISE(警告是错误)

- Wall -pedantic -Weffc++ -Werror

回答by Yogesh Nogia

To compile C or C++ programs, there is a common command:

要编译 C 或 C++ 程序,有一个常用命令:

  1. make filename

  2. ./filename

  1. make filename

  2. ./filename

make will build your source file into an executable file with the same name. But if you want to use the standard way, You could use the gcc compiler to build C programs & g++ for c++

make 将把你的源文件构建成一个同名的可执行文件。但是如果你想使用标准的方式,你可以使用 gcc 编译器来构建 C 程序 & g++ for c++

For C:

对于 C:

gcc filename.c

./a.out

For C++:

对于 C++:

g++ filename.cpp

./a.out