OSX 上的预编译标头和编译通用对象

发布于 2024-08-11 21:09:47 字数 995 浏览 6 评论 0原文

我们在项目中使用带有 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 那样完全独立地构建每个架构,但我更愿意利用同时构建它们的可能性,而不必搞乱不同的构建配置。

We are using precompiled headers with GCC for our project and build them like this:

gcc $(CFLAGS) precompiledcommonlib.h

Now I'm building the project on OSX 10.6 and trying to use the nifty feature of building for all architectures at the same time like this:

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

However, it seems this does not work for the precompiled headers:

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)

Edit:
As Mark pointed out as per XCode the precompiled header has to be built separately for each architecture, so my question is rather if there is any way to have gcc use the right precompiled header when building universal objects.

I do realise that I could build each architecture completely separate like XCode does it but I would much rather take advantage of the possibility to build them at the same time and not have to mess around with different build configurations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十年不长 2024-08-18 21:09:47

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

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

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

Your problem is not the architectures. Both are failing

The issue is that you are trying to build a executable without a main function.

As the file name is commonlib.c I suspect you want to build a library if so start the project with a library template in XCode.

云裳 2024-08-18 21:09:47

我刚刚遇到了同样的问题,并跟进了 @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

I just ran into the same questions and followed up with the link provided by @lucas, so I thought I would provide what I found here.

First of note, if you are porting your gcc code from Linux to MacOS, the version of gcc provided by apple does not properly detect .hpp file extension.

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

As mentioned in another answer, it's best to specify the -x argument to make sure gcc knows what type of file you are compiling.

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

This creates the expected test.hpp.gch.

You can specify any architecture on the command line and the gch builds properly

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

or

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

If you provide more than one architecture, you get the error the poster mentioned.

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)

The key is to compile the architectures you need separately then use the -Xarch_ argument to load the appropriate one during compilation:

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
凉城已无爱 2024-08-18 21:09:47

这可能对你有用

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)

This might work for you

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文