Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

How to compile a C program with gcc?

I wrote a simple program named b.c to call dir in Windows 10 cmd via C.

This is what the script looks like:

#include <stdlib.h>

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

I typed this in 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

enter image description here

I don't think it is path matter.

So how can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. You can easily see the a.exe with the dir command

-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.

  • 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.

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

For more information read Determining C executable name

Of course you can also run a instead of b


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...