R_X86_64_PC32 针对 cygwin 上的未定义符号 `WinMain'

R_X86_64_PC32 against undefined symbol `WinMain' on cygwin

当我在命令行上编译我的代码时,没有问题:

$ gcc -Wall -O2 -o mess main.c getmatrix.c toktoint.c prtmatrix.c getdist.c

但是当我通过 make 编译时,它失败了:

$ make clean
$ make
/usr/bin/gcc -O2 -Wall -c toktoint.c -o toktoint.o
/usr/bin/gcc -O2 -Wall -c getmatrix.c -o getmatrix.o
/usr/bin/gcc -O2 -Wall -c prtmatrix.c -o prtmatrix.o
/usr/bin/gcc -O2 -Wall -c getdist.c -o getdist.o
/usr/bin/gcc -O2 -Wall   -c -o main.o main.c
/usr/bin/gcc -O2 -Wall -o mess toktoint.o
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/10/../../../../lib/libcygwin.a(libcmain.o): in function `main':
/usr/src/debug/cygwin-3.1.7-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
/usr/src/debug/cygwin-3.1.7-1/winsup/cygwin/lib/libcmain.c:37:(.text.startup+0x82): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status
make: *** [Makefile:44: mess] Error 1

这是我的 Makefile:

CC=/usr/bin/gcc
OPTIMIZATION=2
CFLAGS=-O$(OPTIMIZATION) -Wall
LFLAGS=
TARGET=mess
OBJECTS=toktoint.o \
    getmatrix.o \
    prtmatrix.o \
    getdist.o \
    main.o
SOURCES=toktoint.c \
    getmatrix.c \
    prtmatrix.c \
    getdist.c \
    main.c
HEADERS=getmatrix.h \
    toktoint.h \
    prtmatrix.h \
    getdist.h
all: $(TARGET)
mess: $(OBJECTS)
    $(CC) $(CFLAGS) -o $@ $<
%.o: %.c %.h
    $(CC) $(CFLAGS) -c $< -o $@
clean:
    @rm -f $(OBJECTS) $(TARGET)

我尝试更改各种标志,例如“-m64”。以及我在 Whosebug 上找到的其他建议,但没有成功。

如果我在命令行编译每个模块,它也可以工作:

$ make clean
$ gcc -O2 -Wall -c toktoint.c -o toktoint.o
$ gcc -O2 -Wall -c getmatrix.c -o getmatrix.o
$ gcc -O2 -Wall -c prtmatrix.c -o prtmatrix.o
$ gcc -O2 -Wall -c getdist.c -o getdist.o
$ gcc -O2 -Wall -c main.c -o main.o
$ gcc -Wall -O2 -o mess main.o getdist.o getmatrix.o prtmatrix.o toktoint.o

看来问题出在 make 或 Makefile 上。

再次查看 make 的输出,尤其是链接器行:

/usr/bin/gcc -O2 -Wall -o mess toktoint.o

它不使用所有目标文件构建。最值得注意的是,它错过了 main.o,我认为它包含您的 main 函数。

变量$<只有

The name of the first prerequisite

(来自 this make manual,强调我的)。

要获取所有先决条件(所有目标文件),请使用 $^:

mess: $(OBJECTS)
    $(CC) $(CFLAGS) -o $@ $^