C语言 使用 GCC 编译 C 代码

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

Using GCC to compile C code

cgcccompiler-errors

提问by Lakshay Garg

I installed MinGW on my Windows8 laptop and tried to compile a C code file with

我在Windows8 笔记本电脑上安装了 MinGW并尝试编译一个 C 代码文件

gcc test.c -o test.exe

the compiler gave no warnings or errors but it did not create test.exe

编译器没有给出警告或错误,但它没有创建 test.exe

how do i get the compiler to create the file

我如何让编译器创建文件

test.c

测试.c

test.c

测试.c

My terminal session this is what i tried on terminal

我的终端会话 这是我在终端上试过的

An interesting observation:

一个有趣的观察:

When I deliberately introduce an error in the code and try to compile the compiler shows the error

当我故意在代码中引入错误并尝试编译时编译器显示错误

Code with error enter image description hereCompiler output enter image description here

在此处输入图片说明编译器输出 错误的代码 在此处输入图片说明

When I try compiling the same code using Command Prompt

当我尝试使用命令提示符编译相同的代码时

This is what it shows enter image description here

这是它显示的 在此处输入图片说明

But the file does exist in the MinGW\bin directory enter image description here

但该文件确实存在于 MinGW\bin 目录中 在此处输入图片说明

I moved the

我移动了

test.c

file to

归档到

C:\

and started the command prompt in the

并在

C:\MinGW\bin

directory

目录

and here is what it outputs enter image description here

这是它的输出 在此处输入图片说明

Problem partially solved:

问题部分解决:

I disabled hybrid boot in windows 8 and restarted the computer. The compiler now works in Command Prompt but not in PowerShell.

我在 Windows 8 中禁用了混合启动并重新启动了计算机。编译器现在可以在命令提示符中运行,但不能在 PowerShell 中运行。

回答by Sathish

Try to compile your code normally as

尝试正常编译您的代码

gcc test.c

gcc test.c

If you get default output file a.exe,then go for

如果你得到默认输出文件a.exe,那么去

gcc test.c -o test.exe

gcc test.c -o test.exe

回答by AlexRamallo

I know this is an old question, but I came across this after having this same issue and managed to solve it.

我知道这是一个老问题,但我在遇到同样的问题并设法解决后遇到了这个问题。

When I installed MinGW on my computer, I didn't add the MinGW bin directory to my PATH (<mingw install dir>\bin). I had written some code that referred to the GNU compiler binaries by their full path, and when I tried to compile something I experienced the same behavior you described.

当我在我的计算机上安装 MinGW 时,我没有将 MinGW bin 目录添加到我的 PATH ( <mingw install dir>\bin) 中。我已经编写了一些代码,这些代码通过完整路径引用了 GNU 编译器二进制文件,当我尝试编译某些东西时,我遇到了您所描述的相同行为。

So it seems like MinGW won't work properly unless it is added to your PATH. I think it's weird that gcc didn't complain about it though.

因此,除非将 MinGW 添加到您的 PATH,否则它似乎无法正常工作。我认为 gcc 没有抱怨它很奇怪。

回答by Am_I_Helpful

I would suggest you go through this compilation instruction :-

我建议您阅读此编译说明:-

gcc -o test.exe test.c

gcc -o test.exe test.c

I believe this code runs perfectly on your windows system.Please inform if it doesn't!

我相信这段代码可以在您的 Windows 系统上完美运行。如果没有,请告知!

回答by Joseph Quinsey

One possibility is Microsoft's use of VirtualStore.

一种可能性是微软使用VirtualStore.

This can cause problems with "missing" files with Cygwin. See for example, Cygwin sees a file that windows can't--I want to access this file from pythonand https://superuser.com/questions/400600/file-only-visible-to-cygwin-not-windows.

这可能会导致 Cygwin 出现“丢失”文件的问题。例如,Cygwin 看到一个 windows 不能看到的文件——我想从 pythonhttps://superuser.com/questions/400600/file-only-visible-to-cygwin-not-windows访问这个文件

To verify whether this is the case, try doing a search of your entire hard drive for the file test.exe. Or try MinGW's lsrather than dir.

要验证是否是这种情况,请尝试在整个硬盘驱动器中搜索该文件test.exe。或者尝试 MinGW 的ls而不是dir.

And since the OP "partially solved" the problem by moving to another directory, this could be the cause.

由于 OP 通过移动到另一个目录“部分解决了”问题,这可能是原因。