简体   繁体   English

在 Mac 终端上编译和运行 C 程序时出错

[英]Error compiling and running C program on Mac terminal

I'm having a bit of trouble compiling and running my .c file in terminal.我在终端中编译和运行我的 .c 文件时遇到了一些麻烦。 First, when compiling, I see:首先,在编译时,我看到:

HW3.c: In function ‘main’:
HW3.c:87:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
 for(int j = 0; j < 10; j++) {
 ^
HW3.c:87:5: note: use option -std=c99 or -std=gnu99 to compile your code
HW3.c:100:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
 for(int j = 0; j < 10; j++) {
 ^

All of my variables are declared and assigned at the beginning of the program, including j, so I'm not sure why I'm seeing an error about 'for' loop initial declarations.我的所有变量都是在程序开始时声明和分配的,包括 j,所以我不确定为什么我会看到关于 'for' 循环初始声明的错误。

Secondly, when attempting to run my program, I type:其次,在尝试运行我的程序时,我输入:

./a.out HW3.c

and see error并查看错误

./a.out: Command not found.

What could possibly be the issue here?这里可能是什么问题? Is it not running because of the error in compiling?是不是因为编译出错而没有运行? I'm sure I have the command right, right..?我确定我的命令是对的,对吧..? Let me know if you need to see the whole program to help, it's not too long, I could copy it over.如果您需要查看整个程序以提供帮助,请告诉我,不会太长,我可以复制它。 Thanks!谢谢!

If j is already declared at the beginning of the program, then remove the int part of for (int j :如果j在节目开始时已经声明,然后取出int的一部分for (int j

 for(j = 0; j < 10; j++) {

You can declare j inside the for loop, as you seem to have attempted to do, but you would need to tell your compiler to support a newer revision of the C standard.可以在 for 循环中声明j ,就像您似乎已经尝试过的那样,但是您需要告诉编译器支持 C 标准的更新版本。

You need to add a more recent C standard revision to your compiler options.您需要向编译器选项添加更新的 C 标准修订版。 Try adding the flag --std=c99 and it should work.尝试添加标志--std=c99 ,它应该可以工作。

As for your second problem, a.out is the executable produced by the compiler.至于你的第二个问题, a.out是编译器生成的可执行文件。 If there are errors in the program, it won't produce an executable, so you have to fix the errors.如果程序中存在错误,则不会生成可执行文件,因此您必须修复错误。

You can also specify the name of the executable with the -o flag:您还可以使用-o标志指定可执行文件的名称:

gcc -std=c99 HW3.c -o HW3

This will produce an executable named HW3 .这将生成一个名为HW3的可执行文件。

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

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