C gcc编译问题和makefile
甚至不太确定我的问题是什么。缺点是,对于一个类,我应该向这个 c 文件添加一些功能,并且它附带了一个方便的 makefile。
CFLAGS=-DUNIX -lreadline -lcurses -ansi -pedantic-errors
DEBUG=-g
#DEBUG=
all: shell
shell: shell.c parse.c parse.h
gcc $(CFLAGS) $(DEBUG) shell.c parse.c -o shell
clean:
rm -f shell *~
我必须向 shell.c 添加功能。我对 C 非常陌生(通常使用 c++ 或 c#),所以我在一个单独的 test.c 文件中测试一些小东西。比如,查看某些系统调用到底返回什么,如何正确 printf 等。无论如何,tests.c 似乎符合不同的 c 编译器标准,或者我编译错误。如果我不小心使用 // 注释掉某些内容或在 shell.c 开头以外的位置声明变量,编译器就会对我大喊大叫。它不关心tests.c。
我用“gcctests.c -o测试”编译tests.c 如果我使用“gcc shell.c parse.c -o shell”编译 shell,它编译得很好,但运行它只会给我一个分段错误。我很想问我的助教这件事,但每次我为他做某件事时,他都会回答一个完全不同的问题……
对这里发生的事情有什么想法吗?或许至少有一个方向是正确的?
Not even quite sure what my question is. The short of it is, for a class I'm supposed to add some functionality to this c file, and it came with a handy makefile.
CFLAGS=-DUNIX -lreadline -lcurses -ansi -pedantic-errors
DEBUG=-g
#DEBUG=
all: shell
shell: shell.c parse.c parse.h
gcc $(CFLAGS) $(DEBUG) shell.c parse.c -o shell
clean:
rm -f shell *~
I have to add features to shell.c. I'm very new to C (usually use c++ or c#) so I'm testing out little things in a separate little tests.c file. Things like, see what exactly certain system calls return, how to printf them right, etc. Anyway, tests.c seems to be conforming to different c compiler standards or I'm compiling it wrong. If I accidentally use // to comment something out or declare a variable somewhere other than at the start in shell.c, the compiler yells at me. It doesn't care in tests.c.
I compile tests.c with "gcc tests.c -o tests"
If I compile the shell using "gcc shell.c parse.c -o shell" it compiles fine, but running it simply gives me a segmentation fault. I would love to ask my TA about this, but every time I as him something he answers a completely different question...
Any thoughts on what's going on here? Perhaps a point in the right direction at least?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您的 makefile 包含编译器的
-ansi -pedantic-errors
标志。这迫使它使用一个非常旧的 C 版本。也许这个 Makefile 是由你的导师提供的,他想要这样?这并不罕见。要使用这些新功能(
//
注释、块中任意位置的自动变量),只需删除这两个标志即可。如果您有自由,我建议您也使用-std=c99 -Wall
。The problem is that your makefile includes
-ansi -pedantic-errors
flags for the compiler. This forces it to use a very old version of C. Perhaps this Makefile was provided by your instructor and he wants like that? It is not uncommon.To use these new features (
//
comments, automatic variables anywhere in a block) just drop these two flags. If you have the freedom, I recommend also using-std=c99 -Wall
.要让 GCC 接受 C99 约定,请告诉它这样做:
因此,将
-std=gnu99
添加到 CFLAGS 值,并删除-ansi
,它相当于-std=c89
。如果必须按照 C89 标准进行编码,请勿使用//
注释。我们无法判断是什么原因导致了核心转储 - 但可能是您试图在某处修改字符串文字,或者存在大量其他问题。
To get GCC to accept C99 conventions, tell it to do so:
So, add
-std=gnu99
to your CFLAGS value, and remove-ansi
which is equivalent to-std=c89
. If you must code to C89 standards, do not use//
comments.We can't tell what causes the core dump - but it could be that you're trying to modify a string literal somewhere, or any of a large number of other problems.
-ansi -pedantic-errors
可以防止函数中间出现//
和变量定义等杂质。把它去掉,你就可以犯罪了。至于分段错误,最好的办法是通过 gdb 运行程序以查看崩溃的位置。
The
-ansi -pedantic-errors
prevents the impurities like//
and variable definitions in the middle of the function. Remove that and you should be able to sin away.As for the segmentation fault, your best bet is to run your program through
gdb
to see where it crashes.为什么直接调用
gcc
而不是使用makefile进行编译? makefile 添加了许多可能很重要的附加命令行gcc
选项。如果使用make all
进行编译,您是否会看到相同的行为?由于您是 C 语言新手,我建议您将
-Wall
添加到CFLAGS
行。这将启用所有编译器警告,这可能会提醒您注意否则可能会错过的细微错误。Why are you compiling by calling
gcc
directly instead of using the makefile? The makefile adds a number of additional command-linegcc
options which are most likely important. Do you see the same behavior if you compile usingmake all
?Since you are new to C, I would recommend adding
-Wall
to theCFLAGS
line. This will enable all compiler warnings, which may alert you to a subtle error that you might have otherwise missed.