C++ 使用 GCC 构建 Linux 可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1140995/
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 19:00:09  来源:igfitidea点击:

Build An Linux Executable Using GCC

c++linuxgccubuntuexecutable

提问by Nathan Campos

I'm using Linux Ubuntu Intrepid Ibex and compiling C++ files with GCC, but when I compile, gcc makes a.outfile, that is the executable, but how I can make Linux executables? Thanks!

我正在使用 Linux Ubuntu Intrepid Ibex 并使用 GCC 编译 C++ 文件,但是当我编译时,gcc 生成a.out文件,即可执行文件,但是我如何制作 Linux 可执行文件?谢谢!

回答by Tim

That executable is a "Linux executable" - that is, it's executable on any recent Linux system. You can rename the file to what you want using

该可执行文件是“Linux 可执行文件”——也就是说,它可以在任何最新的 Linux 系统上执行。您可以将文件重命名为您想要使用的名称

rename a.out your-executable-name

or better yet, tell GCC where to put its output file using

或者更好的是,告诉 GCC 在哪里放置它的输出文件使用

gcc -o your-executable-name your-source-file.c

Keep in mind that before Linux systems will let you run the file, you may need to set its "executable bit":

请记住,在 Linux 系统允许您运行该文件之前,您可能需要设置其“可执行位”:

chmod +x your-executable-name

Also remember that on Linux, the extension of the file has very little to do with what it actually is - your executable can be named something, something.out, or even something.exe, and as long as it's produced by GCC and you do chmod +xon the file, you can run it as a Linux executable.

还请记住,在 Linux 上,文件的扩展名与它的实际情况几乎没有关系——您的可执行文件可以命名为somethingsomething.out、甚至something.exe,只要它是由 GCC 生成的并且您chmod +x在文件上做了,您就可以运行它作为 Linux 可执行文件。

回答by sth

To create a executable called myprogyou can call gcc like this:

要创建一个名为的可执行文件,myprog您可以像这样调用 gcc:

gcc -c -o myprog something.c

You could also just rename the *.out file gcc generates to the desired name.

您也可以将 gcc 生成的 *.out 文件重命名为所需的名称。

回答by EightyEight

That is the executable. If you don't like a.out, you can pass an -o flag to the compiler. If the executable isn't marked with an executable bit, you need to do so youself:

那是可执行文件。如果您不喜欢 a.out,您可以将 -o 标志传递给编译器。如果可执行文件没有用可执行位标记,您需要自己这样做:

chmod u+x ./a.out
./a.out

HTH.

哈。