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.)
(我知道,忘掉它,重新学习它。是时候把它写下来了。
13 个解决方案
#1
139
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.
您甚至不需要一个makefile。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 $PATH
to find executables, and most often that does not include the current directory (.
).
运行刚刚构建的可执行文件与运行任何程序一样——但是您通常需要指定可执行文件的路径,因为shell将只搜索$ path中的内容来查找可执行文件,而且通常不包括当前目录()。
So to run the built executable foo
:
因此,运行构建的可执行文件foo:
./foo
#2
96
gcc main.cpp -o main.out
./main.out
#3
52
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
-o
is the letter O not zero
-o是O不是0
lab21
will be your executable file
lab21将是您的可执行文件
iterative.cpp
is your c++ file
迭代。cpp是您的c++文件
After you run that command type the following in terminal to run your program:
运行该命令后,在终端输入以下命令以运行程序:
$ ./lab21
#4
30
Two steps for me:
对我来说两个步骤:
first:
第一:
make foo
then:
然后:
./foo
#5
15
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
回声路径美元
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
工作:/ bin:/ usr / sbin:/ sbin:/ usr /地方/ 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
猫mytextfile.txt
This runs /bin/cat
and displays mytextfile.txt to the terminal.
它运行/bin/cat并显示mytextfile。三种终端。
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 X上的主目录中有一个叫做MyProgram的可执行文件我可以像这样完全限定它:
/Users/oliver/MyProgram
/用户/奥利弗/ 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 MyProgram
was in the directory /Users/oliver/MyProject
I and I was in my home directory I can qualify the executable name like this, and have it execute:
如果您位于您希望执行的程序附近,您可以使用部分路径限定名称。例如,如果MyProgram在目录/用户/oliver/MyProject I和我在我的主目录中,我可以这样限定可执行名称,并让它执行:
MyProject/MyProgram
MyProject / MyProgram
Or say I was in the directory /Users/oliver/MyProject2
and I wanted to execute /Users/oliver/MyProject/MyProgram
I can use a relative path like this, to execute it:
或者假设我在目录/用户/oliver/MyProject2中,我想执行/用户/oliver/MyProject/MyProgram我可以使用这样的相对路径来执行:
../MyProject/MyProgram
. . / MyProject / MyProgram
Similarly if I am in the same directory as MyProgram
I 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
。/ MyProgram
To determine which directory you are currently in use the pwd
command.
确定当前使用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_profile
file 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 ~/bin
to be used in preference to system executables your should reorder the export statement as follows:
在这里,tilde(~)字符被用作/Users/oliver的快捷方式。还要注意,散列bang(#!)行需要是文件的第一行(如果它还不存在的话)。还要注意,这项技术要求您的登录shell是bash (Mac OS X和大多数Linux发行版的默认)。还请注意,如果您希望将安装在~/bin中的程序优先用于系统可执行文件,您应该重新订购以下导出语句:
export PATH=~/bin:$PATH
#6
8
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".
莱恩,我把它改成了答案而不是评论,因为我似乎太简短了。在“终端”中完成所有这些。
To use the G++ compiler, you need to do this:
要使用g++编译器,您需要这样做:
-
Navigate to the directory in which you stored the *.cpp file.
导航到存储*的目录。cpp文件。
cd ~/programs/myprograms/
(the ~ is a shortcut for your home, i.e. /Users/Ryan/programs/myprograms/, replace with the location you actually used.)cd ~/program /myprograms/ (the ~是你家的快捷方式,例如/Users/Ryan/program /myprogram /,替换为你实际使用的位置)
-
Compile it
编译它
g++ input.cpp -o output.bin
(output.bin can be anything with any extension, really. bin is just common on unix.)g++输入。cpp - o输出。本(输出。bin可以是任何扩展的东西,真的。bin在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但是,如果您输入ls,您将看到相同目录中的文件列表。例如,您将看到其他文件夹,input。cpp和output.bin
-
From inside the directory, now execute it with
./outbut.bin
从目录内部,现在使用./outbut.bin执行它
#7
8
Assuming the current directory is not in the path, the syntax is ./[name of the program]
.
假设当前目录不在路径中,语法为./[程序名称]。
For example ./a.out
例如. / a.o ut
#8
7
Add following to get best warnings, you will not regret it. If you can, compile WISE (warning is error)
添加以下内容以获得最佳警告,您不会后悔。如果可以,编译WISE(警告是错误)
- Wall -pedantic -Weffc++ -Werror
#9
7
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.
很高兴有一行代码,这样您就可以轻松地重新运行您的可执行文件。
#10
2
Use a makefile
. Even for very small (= one-file) projects, the effort is probably worth it because you can have several sets of compiler settings to test things. Debugging and deployment works much easier this way.
使用一个makefile。即使对于非常小的(=一个文件)项目,这种努力也很值得,因为您可以有几组编译器设置来测试。通过这种方式,调试和部署工作起来要容易得多。
Read the make
manual, it seems quite long at first glance but most sections you can just skim over. All in all it took me a few hours and made me much more productive.
阅读制作手册,乍一看似乎很长,但大多数部分你都可以浏览。总之,这花了我几个小时,让我更有效率。
#11
1
I found this link with directions:
我发现了与方向的联系:
http://www.wesg.ca/2007/11/how-to-write-and-compile-c-programs-on-mac-os-x/
http://www.wesg.ca/2007/11/how-to-write-and-compile-c-programs-on-mac-os-x/
Basically you do:
基本上你该怎么做:
gcc hello.c
./a.out (or with the output file of the first command)
#12
0
To compile C or C++ programs, there is a common command:
要编译C或c++程序,有一个通用的命令:
-
make filename
使文件名
-
./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++构建C程序& g++
For C:
C:
gcc filename.c
./a.out
For C++:
c++:
g++ filename.cpp
./a.out
#13
-1
Running a .C file using the terminal is a two-step process. The first step is to type gcc in the terminal and drop the .C file to the terminal, and then press Enter:
使用终端运行. c文件需要两个步骤。第一步是在终端输入gcc,将.C文件放到终端,然后按Enter:
username$ gcc /Desktop/test.c
In the second step, run the following command:
在第二步中,运行以下命令:
username$ ~/a.out
#1
139
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.
您甚至不需要一个makefile。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 $PATH
to find executables, and most often that does not include the current directory (.
).
运行刚刚构建的可执行文件与运行任何程序一样——但是您通常需要指定可执行文件的路径,因为shell将只搜索$ path中的内容来查找可执行文件,而且通常不包括当前目录()。
So to run the built executable foo
:
因此,运行构建的可执行文件foo:
./foo
#2
96
gcc main.cpp -o main.out
./main.out
#3
52
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
-o
is the letter O not zero
-o是O不是0
lab21
will be your executable file
lab21将是您的可执行文件
iterative.cpp
is your c++ file
迭代。cpp是您的c++文件
After you run that command type the following in terminal to run your program:
运行该命令后,在终端输入以下命令以运行程序:
$ ./lab21
#4
30
Two steps for me:
对我来说两个步骤:
first:
第一:
make foo
then:
然后:
./foo
#5
15
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
回声路径美元
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
工作:/ bin:/ usr / sbin:/ sbin:/ usr /地方/ 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
猫mytextfile.txt
This runs /bin/cat
and displays mytextfile.txt to the terminal.
它运行/bin/cat并显示mytextfile。三种终端。
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 X上的主目录中有一个叫做MyProgram的可执行文件我可以像这样完全限定它:
/Users/oliver/MyProgram
/用户/奥利弗/ 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 MyProgram
was in the directory /Users/oliver/MyProject
I and I was in my home directory I can qualify the executable name like this, and have it execute:
如果您位于您希望执行的程序附近,您可以使用部分路径限定名称。例如,如果MyProgram在目录/用户/oliver/MyProject I和我在我的主目录中,我可以这样限定可执行名称,并让它执行:
MyProject/MyProgram
MyProject / MyProgram
Or say I was in the directory /Users/oliver/MyProject2
and I wanted to execute /Users/oliver/MyProject/MyProgram
I can use a relative path like this, to execute it:
或者假设我在目录/用户/oliver/MyProject2中,我想执行/用户/oliver/MyProject/MyProgram我可以使用这样的相对路径来执行:
../MyProject/MyProgram
. . / MyProject / MyProgram
Similarly if I am in the same directory as MyProgram
I 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
。/ MyProgram
To determine which directory you are currently in use the pwd
command.
确定当前使用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_profile
file 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 ~/bin
to be used in preference to system executables your should reorder the export statement as follows:
在这里,tilde(~)字符被用作/Users/oliver的快捷方式。还要注意,散列bang(#!)行需要是文件的第一行(如果它还不存在的话)。还要注意,这项技术要求您的登录shell是bash (Mac OS X和大多数Linux发行版的默认)。还请注意,如果您希望将安装在~/bin中的程序优先用于系统可执行文件,您应该重新订购以下导出语句:
export PATH=~/bin:$PATH
#6
8
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".
莱恩,我把它改成了答案而不是评论,因为我似乎太简短了。在“终端”中完成所有这些。
To use the G++ compiler, you need to do this:
要使用g++编译器,您需要这样做:
-
Navigate to the directory in which you stored the *.cpp file.
导航到存储*的目录。cpp文件。
cd ~/programs/myprograms/
(the ~ is a shortcut for your home, i.e. /Users/Ryan/programs/myprograms/, replace with the location you actually used.)cd ~/program /myprograms/ (the ~是你家的快捷方式,例如/Users/Ryan/program /myprogram /,替换为你实际使用的位置)
-
Compile it
编译它
g++ input.cpp -o output.bin
(output.bin can be anything with any extension, really. bin is just common on unix.)g++输入。cpp - o输出。本(输出。bin可以是任何扩展的东西,真的。bin在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但是,如果您输入ls,您将看到相同目录中的文件列表。例如,您将看到其他文件夹,input。cpp和output.bin
-
From inside the directory, now execute it with
./outbut.bin
从目录内部,现在使用./outbut.bin执行它
#7
8
Assuming the current directory is not in the path, the syntax is ./[name of the program]
.
假设当前目录不在路径中,语法为./[程序名称]。
For example ./a.out
例如. / a.o ut
#8
7
Add following to get best warnings, you will not regret it. If you can, compile WISE (warning is error)
添加以下内容以获得最佳警告,您不会后悔。如果可以,编译WISE(警告是错误)
- Wall -pedantic -Weffc++ -Werror
#9
7
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.
很高兴有一行代码,这样您就可以轻松地重新运行您的可执行文件。
#10
2
Use a makefile
. Even for very small (= one-file) projects, the effort is probably worth it because you can have several sets of compiler settings to test things. Debugging and deployment works much easier this way.
使用一个makefile。即使对于非常小的(=一个文件)项目,这种努力也很值得,因为您可以有几组编译器设置来测试。通过这种方式,调试和部署工作起来要容易得多。
Read the make
manual, it seems quite long at first glance but most sections you can just skim over. All in all it took me a few hours and made me much more productive.
阅读制作手册,乍一看似乎很长,但大多数部分你都可以浏览。总之,这花了我几个小时,让我更有效率。
#11
1
I found this link with directions:
我发现了与方向的联系:
http://www.wesg.ca/2007/11/how-to-write-and-compile-c-programs-on-mac-os-x/
http://www.wesg.ca/2007/11/how-to-write-and-compile-c-programs-on-mac-os-x/
Basically you do:
基本上你该怎么做:
gcc hello.c
./a.out (or with the output file of the first command)
#12
0
To compile C or C++ programs, there is a common command:
要编译C或c++程序,有一个通用的命令:
-
make filename
使文件名
-
./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++构建C程序& g++
For C:
C:
gcc filename.c
./a.out
For C++:
c++:
g++ filename.cpp
./a.out
#13
-1
Running a .C file using the terminal is a two-step process. The first step is to type gcc in the terminal and drop the .C file to the terminal, and then press Enter:
使用终端运行. c文件需要两个步骤。第一步是在终端输入gcc,将.C文件放到终端,然后按Enter:
username$ gcc /Desktop/test.c
In the second step, run the following command:
在第二步中,运行以下命令:
username$ ~/a.out