macos - 文件是为不受支持的文件格式构建的,这不是所链接的体系结构 (x86_64)

标签 macos gcc makefile gnu-make

我在 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

最佳答案

  • 删除所有目标文件。
  • 修改 makefile 更像是:
    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/

    相关文章:

    c++ - 如何在 GCC 为 C++03 的 CentOS 6 上部署 C++11 程序(具有依赖项)?

    makefile - 使-j RAM限制

    编译所有 C 文件并使用 Makefile 创建可执行文件

    macos - 在 Mac OS X 窗口中显示自定义视频(RGB 位图数据)

    objective-c - 在 OS X 上唯一标识事件窗口

    windows - Qt 跨平台 Windows & Mac : Font size

    macos - 如何在 Mac 上使用 SDL 的操纵杆?

    c++ - 哪个版本的 GNU GCC 支持 TR1 外部模板?

    c - 从 64 位机器中的 C 函数访问返回地址时出现段错误

    c++ - "make"是否知道如何在子目录中搜索包含文件?