文件是为不支持的文件格式而构建的,而不是被链接的体系结构(x86_64) [英] file was built for unsupported file format which is not the architecture being linked (x86_64)

查看:397
本文介绍了文件是为不支持的文件格式而构建的,而不是被链接的体系结构(x86_64)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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:忽略文件hello.o,文件是为不支持的文件格式构建的,而不是被链接的架构(x86_64)
体系结构x86_64的未定义符号:
_main,引用来自:
开始于crt1.10.6.o
ld:符号(s)未找到架构x86_64
collect2 :ld返回1退出状态

我试过使用 -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 如本答案所述文件是为i386而建立的,它不是在Mac OSX 10.6上编译OpenCV2.2 for iOS 4.2时链接的架构(x86_64), t为我工作。



命令行的输出为:

  gcc -I include -std = gnu99 -m64 -c include / hello.h -o hello.o 
gcc -I include -std = gnu99 -m64 hello.o -o hello
ld:警告:忽略文件hello.o,文件是为不支持的文件格式而构建的,而不是被链接的体系结构(x86_64)
体系结构x86_64的未定义符号:
_main,引用自:
从crt1.10.6.o开始
ld:架构x86_64未找到符号
collect2:ld返回1退出状态us
make:*** [hello]错误1


解决方案


删除所有目标文件。 修改makefile文件更像: pre> 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 =# - 选项
UFLAGS =#仅在make命令行上设置
CFLAGS = $(SFLAGS)$(DFLAGS)$(IFLAGS)$(OFLAGS)$(WFLAGS)$(UFLAGS)

这样我就可以在命令行上调整C编译器的任何单个参数。例如,要执行32位构建,我可以运行:

  make SFLAG2 = -m32 

等等。缺点是我永远不会记得哪一个 xFLAGn 选项影响哪个。但是,快速查看makefile可以解决这个问题,我可以在不修改makefile的情况下更改编译。



(我也经常使用 CC =gcc -m64可以在其他人的软件上强制执行64位编译。)


I have a build file on 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 $@

But when I try and run this make file I get the following error

    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

I have tried using the flag -arch x86_64 but still get the same error.

Running the arch command gives: i386.

uname -a tells me: 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

I've also tried to add the switch -march=x86-64 as described in this answer 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 but that hasn't worked for me.

The output from the command line is:

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

解决方案

  1. Remove all the object files.
  2. Revise the makefile more like:

    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)
    

Note that I've macro-ized everything on the command lines. The CFLAGS are used in all compilations. They are not enclosed in double quotes. The -m64 option requests a 64-bit build; it shouldn't be necessary, but it makes it explicit. You don't yet need the LDFLAGS or LDLIBS macros (so you could omit them without causing yourself problems), but they show how you might proceed when you do need some libraries at link time.

For my own makefiles, I do things like:

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)

That way I can adjust just about any single argument to the C compiler on the command line. For example, to do 32-bit builds, I can run:

make SFLAG2=-m32

Etc. The downside is I can never remember which xFLAGn option affects which. However, a quick look at the makefile rectifies that, and I can change the compilation without modifying the makefile at all.

(I also often use CC="gcc -m64" to force 64-bit compilations on other people's software.)

这篇关于文件是为不支持的文件格式而构建的,而不是被链接的体系结构(x86_64)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆