我在 OSX Lion 上有一个构建文件
VPATH = src include
CFLAGS ="-I include -std=gnu99"
hello: hello.o
gcc $^ -o $@
hello.o: hello.h hello.c
gcc $(CFLAGS) -c $< -o $@
但是当我尝试运行这个 make 文件时,我收到以下错误
ld: warning: ignoring file hello.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
我试过使用标志
-arch x86_64
但仍然得到同样的错误。运行
arch
命令给出:i386
.uname -a
告诉我:Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64
我也尝试添加开关
-march=x86-64
如本答案中所述 file was built for i386 which is not the architecture being linked (x86_64) while compiling OpenCV2.2 for iOS 4.2 on Mac OSX 10.6但这对我不起作用。命令行的输出是:
gcc -I include -std=gnu99 -m64 -c include/hello.h -o hello.o
gcc -I include -std=gnu99 -m64 hello.o -o hello
ld: warning: ignoring file hello.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [hello] Error 1
最佳答案
VPATH = src include
CFLAGS = -I include -std=gnu99 -m64
CC = gcc
LDLIBS =
LDFLAGS =
hello: hello.o
$(CC) $(CFLAGS) $^ -o $@
hello.o: hello.c hello.h
$(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) $(LDLIBS)
请注意,我已经对命令行上的所有内容进行了宏化。 CFLAGS 用于所有编译。它们没有用双引号括起来。
-m64
选项请求 64 位构建;它不应该是必要的,但它使它明确。您还不需要 LDFLAGS 或 LDLIBS 宏(因此您可以省略它们而不会给自己带来问题),但是它们显示了当您在链接时确实需要一些库时可以如何进行。对于我自己的 makefile,我会执行以下操作:
IFLAGS = -Iinclude
WFLAG1 = -Wall
WFLAG2 = -Werror
WFLAG3 = -Wextra
WFLAGS = $(WFLAG1) $(WFLAG2) $(WFLAG3)
OFLAGS = -g -O3
SFLAG1 = -std=c99
SFLAG2 = -m64
SFLAGS = $(SFLAG1) $(SFLAG2)
DFLAGS = # -Doptions
UFLAGS = # Set on make command line only
CFLAGS = $(SFLAGS) $(DFLAGS) $(IFLAGS) $(OFLAGS) $(WFLAGS) $(UFLAGS)
这样我就可以在命令行上调整 C 编译器的任何单个参数。例如,要进行 32 位构建,我可以运行:
make SFLAG2=-m32
等等。缺点是我永远不记得哪个 xFLAGn 选项会影响哪个。然而,快速查看 makefile 可以纠正这一点,我可以在不修改 makefile 的情况下更改编译。
(我也经常使用
CC="gcc -m64"
在其他人的软件上强制进行 64 位编译。)
关于macos - 文件是为不受支持的文件格式构建的,这不是所链接的体系结构 (x86_64),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183053/