write-a-C-interpreter icon indicating copy to clipboard operation
write-a-C-interpreter copied to clipboard

代码在windows gcc-6.3.0下无法编译通过

Open AlexChingEraser opened this issue 2 years ago • 4 comments

您好~我直接从仓库拉代码下来无法编译通过: os: win10 64bit gcc: MinGW.org GCC-6.3.0-1

PS C:\Users\AlexC\Documents\GitHub\write-a-C-interpreter> gcc -m32 -o xc xc.c
xc.c: In function 'next':
xc.c:111:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
                 if (current_id[Hash] == hash && !memcmp((char *)current_id[Name], last_pos, src - last_pos)) {
                                                         ^
xc.c:121:32: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
             current_id[Name] = (int)last_pos;
                                ^
xc.c:188:29: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                 token_val = (int)last_pos;
                             ^
xc.c: In function 'expression':
xc.c:363:30: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
             data = (char *)(((int)data + sizeof(int)) & (-sizeof(int)));
                              ^
xc.c:363:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
             data = (char *)(((int)data + sizeof(int)) & (-sizeof(int)));
                    ^
xc.c:629:25: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                 *addr = (int)(text + 3);
                         ^
xc.c:633:25: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                 *addr = (int)(text + 1);
                         ^
.....

编译模板代码的时候会报open,read,close未定义先使用的错误:

PS C:\Users\AlexC\Documents\compiler> gcc -m32 .\compiler.c .\helloworld.c
.\compiler.c: In function 'main':
.\compiler.c:46:15: warning: implicit declaration of function 'open' [-Wimplicit-function-declaration]
     if ((fd = open(*argv, 0)) < 0) {
               ^~~~
.\compiler.c:56:14: warning: implicit declaration of function 'read' [-Wimplicit-function-declaration]
     if ((i = read(fd, src, poolsize-1)) <= 0) {
              ^~~~
.\compiler.c:61:5: warning: implicit declaration of function 'close' [-Wimplicit-function-declaration]
     close(fd);

在论坛上找到相似问题,但还是无法解决,如引入以下头文件:

#include <fcntl.h> // for open
#include <unistd.h> // for close

是我本地的gcc版本有问题吗?还是必须要在linux环境下运行?

AlexChingEraser avatar May 02 '22 02:05 AlexChingEraser

@AlexChingEraser 一般 warning 错误应该不影响编译,有 error 错误吗?

lotabout avatar May 09 '22 01:05 lotabout

没有error,但是程序会退出,显示usage:[-s] [-d] file ...

18842839821 avatar Mar 28 '24 17:03 18842839821

没有error,但是程序会退出,显示usage:[-s] [-d] file ...

是怎么运行的?显示 usage: .... 代表你需要传入要运行的 c 文件,例如:

./xc hello.c
./xc -s hello.c

./xc xc.c hello.c
./xc xc.c xc.c hello.c

lotabout avatar Mar 29 '24 09:03 lotabout

这个在wimdows上就会这样,我在linux上跑就没问题了

18842839821 avatar May 06 '24 11:05 18842839821