在OSX上预编译的头文件和编译通用对象

ras*_*usb 8 macos gcc x86-64 precompiled-headers

我们在项目中使用GCC预编译头文件,并按如下方式构建它们:

gcc $(CFLAGS) precompiledcommonlib.h
Run Code Online (Sandbox Code Playgroud)

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

gcc $(CFLAGS) -c -arch i386 -arch x86_64 commonlib.c  
Run Code Online (Sandbox Code Playgroud)

但是,似乎这对预编译的头文件不起作用:

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)
Run Code Online (Sandbox Code Playgroud)

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

我确实意识到我可以像XCode一样完全分离构建每个架构,但我更愿意利用同时构建它们的可能性,而不必使用不同的构建配置.

lef*_*cus 3

我刚刚遇到了同样的问题,并跟进了 @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
Run Code Online (Sandbox Code Playgroud)

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

g++ -x c++-header test.hpp
Run Code Online (Sandbox Code Playgroud)

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

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

g++ -x c++-header test.hpp -arch i386
Run Code Online (Sandbox Code Playgroud)

或者

g++ -x c++-header test.hpp -arch x86_64
Run Code Online (Sandbox Code Playgroud)

如果您提供多个架构,您会收到发帖人提到的错误。

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)
Run Code Online (Sandbox Code Playgroud)

关键是单独编译您需要的架构,然后-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
Run Code Online (Sandbox Code Playgroud)