简体   繁体   English

如何用gcc编译C程序?

[英]How to compile a C program with gcc?

I wrote a simple program named bc to call dir in Windows 10 cmd via C. 我编写了一个名为bc的简单程序,以通过C在Windows 10 cmd中调用dir

This is what the script looks like: 脚本如下所示:

#include <stdlib.h>

int main()
{
    system("dir");
    return 0;
}

I typed this in cmd 我用cmd输入

gcc b.c
b

but it returns 但它返回

J:\fundamental of C programming>b
'b' is not recognized as an internal or external command, operable program or batch file.

This is how I add path 这是我添加路径的方式

在此处输入图片说明

I don't think it is path matter. 我认为这不是路径问题。

So how can I fix it? 那么我该如何解决呢?

You need to run 你需要跑步

gcc b.c -o b.exe

Without -o option it'll use the default output executable name which is a.exe on Windows and a.out on *nix systems . 如果没有-o选项,它将使用默认的输出可执行文件名称,在Windows a.exe在* nix系统上为a.out You can easily see the a.exe with the dir command 您可以使用dir命令轻松查看a.exe

-o file

  • Place output in file file. 将输出放置在文件文件中。 This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code. 这适用于所产生的任何类型的输出,无论是可执行文件,目标文件,汇编文件还是预处理的C代码。

  • If -o is not specified, the default is to put an executable file in a.out , the object file for source.suffix in source.o , its assembler file in source.s , a precompiled header file in source.suffix.gch , and all preprocessed C source on standard output. 如果未指定-o ,则默认值为将可执行文件放入a.out ,将source.suffix的目标文件放入source.o ,将其汇编文件放入source.s ,将source.s中的预编译头文件source.suffix.gch ,以及所有标准输出上的预处理C源代码。

https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html

For more information read Determining C executable name 有关更多信息,请参阅确定C可执行文件名。

Of course you can also run a instead of b 当然你也可以用a代替b

Compile like this 像这样编译

gcc -o b b.c

by specifying output filename 通过指定输出文件名

-o <output_file>

尝试在Windows中更新bin路径变量,打开下一个链接以查看操作方法: http : //www.c4learn.com/c-programming/run-c-program-command-prompt-windows/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM