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

时间:2012-04-16 23:24:00

标签: 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

我还尝试按照此回复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中的说明添加切换-march=x86-64,但这对我没有用。

命令行的输出是:

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

3 个答案:

答案 0 :(得分:3)

  1. 删除所有目标文件。
  2. 修改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)
    
  3. 请注意,我已经在命令行中对所有内容进行了宏观化处理。 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位编译。)

答案 1 :(得分:1)

当我意外地在档案中包含.h文件时,我遇到了这个问题...

答案 2 :(得分:0)

就我而言,-M选项正在创建此问题。我将此选项添加到项目依赖项中,但不知何故它导致了问题。