预编译头文件和编译OSX上的通用对象 [英] Precompiled headers and compiling universal objects on OSX

查看:153
本文介绍了预编译头文件和编译OSX上的通用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为我们的项目使用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 
架构i386的未定义符号:
_main,引用来自:
开始于crt1.10.6.o
ld:架构中没有找到符号i386
collect2:ld返回1退出状态
架构x86_64的未定义符号:
_main,引用来自:
开始于crt1.10.6.o
ld:符号(s)找不到架构x86_64
collect2:ld返回1退出s tatus
lipo:无法打开输入文件:/var/folders/z1/z1A0sPkqGDyPrZWo9ysVK++++TI/-Tmp-//cc3W2gwd.out(没有这样的文件或目录)

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



我意识到我可以像XCode一样将每个体系结构完全分开但我更愿意利用它们同时构建它们的可能性,而不必混淆不同的构建配置。

解决方案



首先要注意的是,我会提供我在这里找到的内容。 ,如果你将你的gcc代码从Linux移植到MacOS,苹果提供的gcc版本不能正确地解决ct .hpp文件扩展名。

  mac:openstudio lefticus $ g ++ test.hpp 
ld:warning:忽略文件测试.hpp文件是为不支持的文件格式而构建的,它不是被链接的体系结构(x86_64)
体系结构x86_64的未定义符号:
_main,引用自:
start in crt1.10.6 .o
ld:找不到架构x86_64的符号
collect2:ld返回1退出状态

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

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

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



您可以指定命令行和gch上的任何体系结构都可以正确构建

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

  g ++ -x c ++  -  header te st.hpp -arch x86_64 

如果您提供多个体系结构, 。

  mac:openstudio lefticus $ g ++ -xc ++  -  header test.hpp -arch i386 -arch x86_64 
未定义的符号对于架构i386:
_main,引用来自:
开始于crt1.10.6.o
ld:符号(s)未找到架构i386
collect2:ld返回1退出状态
体系结构x86_64的未定义符号:
_main,引用来自:
开始于crt1.10.6.o
ld:找不到架构x86_64的符号
collect2:ld返回1退出状态
lipo:无法打开输入文件:/var/folders/DM/DMTpbjzHHX08IEqGgEAORE+++TI/-Tmp-//ccDeWigf.out(No such file or directory )

关键是编译您需要的架构,然后使用 - Xarch _ 参数在编译过程中加载相应的参数:

  g ++ -x c ++  - 头文件-arch x86_64 x86_64 / test.hpp 
g ++ -x c ++ - 头文件-arch i386 i386 / test.hpp

g ++ -arch i386 -arch x86_64 test.cpp -Xarch_i386 -Ii386 - Xarch_x86_64 -Ix86_64


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.

解决方案

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

这篇关于预编译头文件和编译OSX上的通用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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