2009-11-18 86 views
7
编译通用对象

我们正在使用预编译的头与GCC为我们的项目,并建立他们是这样的:预编译头和OSX

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那样完全分离构建每个架构,但我更愿意利用可能性来同时构建它们,而不必混淆不同的构建配置。

+0

我只是试图做同样的,从我可以收集,你不能在给Apple的GCC两个'-arch'标志时生成一个PCH文件。Qt人似乎正在做一些奇怪的事情,让这个工作,你可能想检查出来... http://qt.gitorious.org/qt/qt/merge_requests/2193 – Lucas 2010-02-07 17:25:18

回答

2

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

首先需要注意的是,如果您将gcc代码从Linux移植到MacOS,苹果提供的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参数,以确保海合会知道你正在编译什么类型的文件。

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 
+0

非常好找!非常感谢! – rasmusb 2012-01-14 15:21:45

3

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

问题是,您正在尝试构建不带main函数的可执行文件。

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

+0

感谢您花时间。 尝试'gcc foo.h'。它输出什么?你还认为我正在尝试构建一个可执行文件吗? 草率的,更不用说平淡的错误答案和光顾的态度并不是一个特别迷人的组合。 – rasmusb 2009-11-18 22:19:14

+0

我确实混淆了命令行抱歉。但是,错误消息和命令行正在尝试构建可执行文件。 - 这就是为什么ld是命令行通过给出错误 我仍然会在Xcode中设置它以使所有标志都正确。如果您希望Apple为每个架构分别预编译每个头文件,并且命令行包含-x objective-c-header -arch x86_64 Apple文档也会提示-x参数。 amd在单独的架构上的细节。 – Mark 2009-11-19 12:19:01

+0

良好的通话。当我建立一个没有主要方法的项目时,我得到这个错误 – surajz 2011-03-19 16:54:21