【问题标题】:Precompiled headers and compiling universal objects on OSX在 OSX 上预编译头文件和编译通用对象
【发布时间】:2010-12-17 23:15:04
【问题描述】:

我们正在为我们的项目使用带有 GCC 的预编译头文件,并像这样构建它们:

gcc $(CFLAGS) precompiledcommonlib.h

现在我正在 OSX 10.6 上构建项目,并尝试使用同时为所有架构构建的漂亮功能,如下所示:

gcc $(CFLAGS) -c -arch i386 -arch x86_64 commonlib.c  

但是,这似乎不适用于预编译的标头:

gcc $(CFLAGS) -arch i386 -arch x86_64 precompiledcommonlib.h
Undefined symbols for architecture i386:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
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
lipo: can't open input file: /var/folders/z1/z1A0sPkqGDyPrZWo9ysVK++++TI/-Tmp-//cc3W2gwd.out (No such file or directory)

编辑: 正如 Mark 根据 XCode 指出的那样,必须为每个架构单独构建预编译头文件,所以我的问题是,是否有任何方法可以让 gcc 在构建通用对象时使用正确的预编译头文件。

我确实意识到我可以像 XCode 那样完全独立地构建每个架构,但我更愿意利用同时构建它们的可能性,而不必搞乱不同的构建配置。

【问题讨论】:

  • 我只是尝试做同样的事情,据我所知,您无法生成一个 PCH 文件,同时为 Apple 的 GCC 提供两个 -arch 标志。 Qt 家伙似乎在做一些奇怪的事情来让它工作,你可能想检查一下...qt.gitorious.org/qt/qt/merge_requests/2193

标签: macos gcc x86-64 precompiled-headers


【解决方案1】:

这可能对你有用

valid output formats for -f are (`*' denotes default):
  * bin       flat-form binary files (e.g. DOS .COM, .SYS)
    ith       Intel hex
    srec      Motorola S-records
    aout      Linux a.out object files
    aoutb     NetBSD/FreeBSD a.out object files
    coff      COFF (i386) object files (e.g. DJGPP for DOS)
    elf32     ELF32 (i386) object files (e.g. Linux)
    elf64     ELF64 (x86_64) object files (e.g. Linux)
    elfx32    ELFX32 (x86_64) object files (e.g. Linux)
    as86      Linux as86 (bin86 version 0.3) object files
    obj       MS-DOS 16-bit/32-bit OMF object files
    win32     Microsoft Win32 (i386) object files
    win64     Microsoft Win64 (x86-64) object files
    rdf       Relocatable Dynamic Object File Format v2.0
    ieee      IEEE-695 (LADsoft variant) object file format
    macho32   NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files
    macho64   NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files
    dbg       Trace of all info passed to output stage
    elf       ELF (short name for ELF32)
    macho     MACHO (short name for MACHO32)
    win       WIN (short name for WIN32)

【讨论】:

    【解决方案2】:

    我刚刚遇到了同样的问题并跟进了@lucas 提供的链接,所以我想我会提供我在这里找到的内容。

    首先需要注意的是,如果您将 gcc 代码从 Linux 移植到 MacOS,Apple 提供的 gcc 版本无法正确检测 .hpp 文件扩展名。

    mac:openstudio lefticus$ g++ test.hpp
    ld: warning: ignoring file test.hpp, 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
    

    正如另一个答案中提到的,最好指定 -x 参数以确保 gcc 知道您正在编译的文件类型。

    g++ -x c++-header test.hpp
    

    这将创建预期的test.hpp.gch

    您可以在命令行上指定任何架构,并且 gch 可以正确构建

    g++ -x c++-header test.hpp -arch i386
    

    g++ -x c++-header test.hpp -arch x86_64
    

    如果您提供多个架构,则会收到海报提到的错误。

    mac:openstudio lefticus$ g++ -xc++-header test.hpp -arch i386 -arch x86_64
    Undefined symbols for architecture i386:
      "_main", referenced from:
          start in crt1.10.6.o
    ld: symbol(s) not found for architecture i386
    collect2: ld returned 1 exit status
    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
    lipo: can't open input file: /var/folders/DM/DMTpbjzHHX08IEqGgEAORE+++TI/-Tmp-//ccDeWigf.out (No such file or directory)
    

    关键是分别编译你需要的架构,然后在编译时使用-Xarch_参数加载合适的架构:

    g++ -x c++-header -arch x86_64 x86_64/test.hpp
    g++ -x c++-header -arch i386 i386/test.hpp
    
    g++ -arch i386 -arch x86_64 test.cpp -Xarch_i386 -Ii386 -Xarch_x86_64 -Ix86_64
    

    【讨论】:

    • 很好的发现!非常感谢!
    【解决方案3】:

    您的问题不在于架构。两者都失败了

    问题是您正在尝试构建没有主函数的可执行文件。

    由于文件名是 commonlib.c,我怀疑您想构建一个库,如果是这样,请在 XCode 中使用库模板启动项目。

    【讨论】:

    • 感谢您抽出宝贵时间。试试“gcc foo.h”。它输出什么?您仍然认为我正在尝试构建可执行文件吗?仓促,更不用说明显的错误答案和傲慢的态度并不是一个特别迷人的组合。
    • 对不起,我确实混淆了命令行。但是错误消息和命令行正在尝试构建可执行文件。 - 这就是为什么 ld 通过给出错误是命令行的原因,我仍然会在 Xcode 中设置它以使所有标志都正确。如果您有的话,您会看到 Apple 会为每个架构分别预编译每个标头,并且命令行包括 -x objective-c-header -arch x86_64 Apple docs developer.apple.com/Mac/library/documentation/DeveloperTools/…> 还建议使用 -x 参数。 amd 关于不同架构的详细信息。
    • 好电话。当我在没有 main 方法的情况下构建项目时出现该错误
    猜你喜欢
    • 2015-01-03
    • 2012-09-08
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2014-02-23
    • 1970-01-01
    相关资源
    最近更新 更多