ios Xcode 4 构建成功,命令行构建失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6653188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 4 build succeeds, command line build fails?
提问by James J
I have a project in Xcode 4 (the latest non-beta version) that builds fine when built in Xcode itself. Specifically, the Ld command correctly uses the derived data directory (where build products, including a dependent static library, are placed).
我在 Xcode 4(最新的非测试版)中有一个项目,在 Xcode 本身中构建时可以很好地构建。具体来说,Ld 命令正确使用派生数据目录(其中放置构建产品,包括依赖的静态库)。
However, when I build the same project from the command line, the Ld command fails, as it is trying to use the /build folder within the project, which is not being populated.
但是,当我从命令行构建同一个项目时,Ld 命令失败,因为它试图使用项目中的 /build 文件夹,该文件夹没有被填充。
I've tried adjusting every build setting I know about, both in the parent and the dependent project.
我已经尝试在父项目和从属项目中调整我所知道的每个构建设置。
Any ideas on where to start debugging this? I can provide more info as needed.
关于从哪里开始调试这个的任何想法?我可以根据需要提供更多信息。
Edit 1:Full Xcode build command:
编辑 1:完整的 Xcode 构建命令:
xcodebuild -project AppName.xcodeproj -target AppName -configuration "Config Name"
Where AppName
and Config Name
are both the correct values for the build.
WhereAppName
和Config Name
都是构建的正确值。
Edit 2:Link (Ld) commands.
编辑 2:链接 (Ld) 命令。
When built in Xcode (this works):
在 Xcode 中构建时(这有效):
Ld /Users/james/Library/Developer/Xcode/DerivedData/AppName-apkmkuhwuccsbpblulxcsafyxkwa/Build/Products/Debug-iphonesimulator/AppName.app/AppName normal i386
cd /Users/james/Code/ClientName-Depot/NameOfProject/trunk/AppName
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/bin:/usr/local/git/bin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-gcc-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -L/Users/james/Library/Developer/Xcode/DerivedData/AppName-apkmkuhwuccsbpblulxcsafyxkwa/Build/Products/Debug-iphonesimulator -L/Users/james/Code/ClientName-Depot/NameOfProject/trunk/AppName -F/Users/james/Library/Developer/Xcode/DerivedData/AppName-apkmkuhwuccsbpblulxcsafyxkwa/Build/Products/Debug-iphonesimulator -filelist /Users/james/Library/Developer/Xcode/DerivedData/AppName-apkmkuhwuccsbpblulxcsafyxkwa/Build/Intermediates/AppName.build/Debug-iphonesimulator/AppName.build/Objects-normal/i386/AppName.LinkFileList -mmacosx-version-min=10.6 -lxml2 -all_load -ObjC -licucore -Xlinker -objc_abi_version -Xlinker 2 -lMyClientLibrary -lxml2 -lsqlite3.0 -framework Security -framework MessageUI -framework QuartzCore -framework MediaPlayer -framework MapKit -framework CoreLocation -framework AudioToolbox -lz.1.2.3 -framework MobileCoreServices -framework SystemConfiguration -framework CFNetwork -framework UIKit -framework Foundation -framework CoreGraphics -o /Users/james/Library/Developer/Xcode/DerivedData/AppName-apkmkuhwuccsbpblulxcsafyxkwa/Build/Products/Debug-iphonesimulator/AppName.app/AppName
When built from command line using build command above (this fails):
当使用上面的构建命令从命令行构建时(失败):
Ld "build/AppName.build/Prod Ad Hoc-iphoneos/AppName.build/Objects-normal/armv6/AppName" normal armv6
cd /Users/james/Code/ClientName-Depot/NameOfProject/trunk/AppName
setenv IPHONEOS_DEPLOYMENT_TARGET 4.0
setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin:/opt/local/bin"
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk "-L/Users/james/Code/ClientName-Depot/NameOfProject/trunk/AppName/build/Prod Ad Hoc-iphoneos" -L/Users/james/Code/ClientName-Depot/NameOfProject/trunk/AppName "-F/Users/james/Code/ClientName-Depot/NameOfProject/trunk/AppName/build/Prod Ad Hoc-iphoneos" -filelist "/Users/james/Code/ClientName-Depot/NameOfProject/trunk/AppName/build/AppName.build/Prod Ad Hoc-iphoneos/AppName.build/Objects-normal/armv6/AppName.LinkFileList" -dead_strip -lxml2 -all_load -ObjC -licucore -miphoneos-version-min=4.0 -lMyClientLibrary -lxml2 -lsqlite3.0 -framework Security -framework MessageUI -framework QuartzCore -framework MediaPlayer -framework MapKit -framework CoreLocation -framework AudioToolbox -lz.1.2.3 -framework MobileCoreServices -framework SystemConfiguration -framework CFNetwork -framework UIKit -framework Foundation -framework CoreGraphics -o "/Users/james/Code/ClientName-Depot/NameOfProject/trunk/AppName/build/AppName.build/Prod Ad Hoc-iphoneos/AppName.build/Objects-normal/armv6/AppName"
Which returns:
返回:
ld: library not found for -lMyClientLibrary
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
回答by James J
Ok, so nearly 6 (billable) hours later, I've gotten the build to work correctly in Xcode and on the command line (and on the build server, the whole point of this exercise).
好的,将近 6(可计费)小时后,我已经让构建在 Xcode 和命令行上正常工作(以及在构建服务器上,本练习的重点)。
Along the way I would fix one problem just to cause another - I would apparently fix the linker/Ld problem, only to cause problems in compilation ("SomeClass undeclared (first use in this function)" or "SomeHeader.h: No such file or directory" errors were common).
在此过程中,我会修复一个问题只是为了导致另一个问题 - 我显然会修复链接器/Ld 问题,只会导致编译出现问题(“SomeClass 未声明(首次在此函数中使用)”或“SomeHeader.h:没有这样的文件”或目录”错误很常见)。
It was one of those times that I adjusted nearly every setting I could find, so it's hard to say what exactly what wrong and what exactly fixed it.
那是我调整几乎所有我能找到的所有设置的时候之一,所以很难说到底出了什么问题以及究竟是什么修复了它。
Things I think mighthave helped are are as follows:
我认为可能有帮助的事情如下:
- Converted build to use an Xcode workspace & scheme (instead of project & target)
- Rearranged workspace to have the App project and static library as siblings (not as parent/child)
- Changed Xcode and workspace settings to use build locations specified in targets
- Change Build Products Path for App and Library to use ../build (both project files are contained in sibling subfolders of a master directory, so having them build into the same folder solved the original linker/Ld command problem, I think)
- Edited the App scheme to explicitly build the Library target, and build it before the App target
- In the Build Phases for the App target, explicitly add the Library under "Link Binary With Libraries"
- Change the location type of the Library's .a file reference to "Relative to Build Products"
- Added a "Copy Headers" build phase to the Library project, added the appropriate headers to the Public section
- Changed the Public Headers Folder Path of the Library project to "/include"
- Changed the Installation Directory of the Library to
$(BUILT_PRODUCTS_DIR)
- Changed the Library Search Paths and the User Header Search Paths of the App target to
$(BUILT_PRODUCTS_DIR)
(recursive) - Added a Clean command before the build on my Jenkins build server
- Added explicit SDK and Arch arguments to the build command
- Removed spaces from build configuration name
- 将构建转换为使用 Xcode 工作区和方案(而不是项目和目标)
- 重新安排工作区以将 App 项目和静态库作为兄弟(而不是父/子)
- 更改了 Xcode 和工作区设置以使用目标中指定的构建位置
- 更改应用程序和库的构建产品路径以使用 ../build (两个项目文件都包含在主目录的同级子文件夹中,因此将它们构建到同一文件夹中解决了原始链接器/Ld 命令问题,我认为)
- 编辑 App 方案以明确构建 Library 目标,并在 App 目标之前构建它
- 在 App 目标的 Build Phases 中,在“Link Binary With Libraries”下明确添加库
- 将库的 .a 文件引用的位置类型更改为“相对于构建产品”
- 向库项目添加了“复制标题”构建阶段,向公共部分添加了适当的标题
- 将库项目的公共标题文件夹路径更改为“/include”
- 将库的安装目录更改为
$(BUILT_PRODUCTS_DIR)
- 将应用程序目标的库搜索路径和用户标题搜索路径更改为
$(BUILT_PRODUCTS_DIR)
(递归) - 在我的 Jenkins 构建服务器上构建之前添加了一个 Clean 命令
- 向构建命令添加了明确的 SDK 和 Arch 参数
- 从构建配置名称中删除空格
Final build command looks like this:
最终构建命令如下所示:
xcodebuild -workspace ClientName.xcworkspace -scheme AppName -configuration "ProdAdHoc" -sdk iphoneos -arch "armv6 armv7"
Some useful resources I used while debugging this issue:
我在调试此问题时使用的一些有用资源:
- http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/
- https://devforums.apple.com/thread/91711?start=25&tstart=0
- http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/
- https://devforums.apple.com/thread/91711?start=25&tstart=0
Anyway, I hope I've peppered enough keywords above that anybody who has any similar build issues in the future stumbles upon this and finds it useful. I have no clue how a workflow I did many times in Xcode 3.x got so messed up when I moved to Xcode 4, here's hoping Apple is able to clean this up in future releases.
无论如何,我希望我已经在上面添加了足够多的关键字,以便将来遇到任何类似构建问题的人偶然发现它并发现它很有用。当我转移到 Xcode 4 时,我不知道我在 Xcode 3.x 中多次执行的工作流程是如何变得如此混乱,希望 Apple 能够在未来的版本中解决这个问题。
This was a heck of a learning experience for me, and going through all of this did seem to clear up issues with autocomplete I was having beforehand. I will say things could have been much worse; I could still be developing for SharePoint.
这对我来说是一次非常棒的学习经历,经历所有这些似乎确实解决了我之前遇到的自动完成问题。我会说事情可能会更糟;我仍然可以为 SharePoint 进行开发。
回答by bromanko
I ran in to the same issue yesterday and was able to work it out. In an effort to narrow down what worked for James, I'll point to what I had to do. I had to add a workspace and switch to running xcodebuild with workspace/scheme instead of project/target.
我昨天遇到了同样的问题,并且能够解决它。为了缩小对詹姆斯有用的范围,我将指出我必须做的事情。我不得不添加一个工作区并切换到使用工作区/方案而不是项目/目标运行 xcodebuild。
Using workspace/scheme forced xcodebuild to use the DerivedData folder instead of the build output folder under the main project. This allowed the linker to find the associated static library.
使用工作区/方案强制 xcodebuild 使用 DerivedData 文件夹而不是主项目下的构建输出文件夹。这允许链接器找到关联的静态库。
This blog post was hugely helpful:
这篇博文非常有帮助:
http://blog.carbonfive.com/2011/05/04/automated-ad-hoc-builds-using-xcode-4/
http://blog.carbonfive.com/2011/05/04/automated-ad-hoc-builds-using-xcode-4/
回答by Popeye
I got this error when I was experimenting with my files I add the @implementation to the .h file and left the .m file empty. I don't believe this is your error but if anyone else gets it maybe check that you haven't done this.
我在试验我的文件时遇到了这个错误,我将 @implementation 添加到 .h 文件并将 .m 文件留空。我不相信这是您的错误,但如果其他人得到它,请检查您是否没有这样做。
回答by nemesis
Check if you didn't import the .m files in your header files! Changing .m to .h fixed this for me!
检查您是否没有在头文件中导入 .m 文件!将 .m 更改为 .h 为我解决了这个问题!
回答by uneakharsh
I don't know if this will work for you but in my case, I had more than one main.m
file. All I had to do was detach one of the main.m
from the target and it worked. Make sure you don't have more than one main.m
in your project.
我不知道这是否适合你,但就我而言,我有不止一个main.m
文件。我所要做的就是将其中一个main.m
从目标中分离出来并且它起作用了。确保您main.m
的项目中不超过一个。
回答by laiBilly
I had a similar exception encountered, it turn out that i got some (null) reference in project.pbxproj after I clean up those null reference in the project.pbxproj, the command line build was success just like the xcode previously does. Take a look at Xcode 4 project: utility to clean up pbxproj file?utility-to-clean-up-pbxproj-file
我遇到了类似的异常,结果是在我清理了 project.pbxproj 中的那些空引用后,我在 project.pbxproj 中得到了一些(空)引用,命令行构建成功,就像之前的 xcode 一样。看看Xcode 4 项目:清理 pbxproj 文件的实用程序?实用程序清理 pbxproj 文件
for more reference
更多参考
回答by Dave Stampf
If you look at the log of the build, asking to see all messages, you should see a terse line that says "link ..." that has little in the way of details. However if you right click that line and select "expand all transcripts" you get a very detailed line telling you what command was issued from within XCode.
如果您查看构建日志,要求查看所有消息,您应该会看到一条简短的行,上面写着“link ...”,其中几乎没有详细信息。但是,如果您右键单击该行并选择“展开所有成绩单”,您会得到一个非常详细的行,告诉您从 XCode 中发出了什么命令。
That should help you to debug the issue.
这应该可以帮助您调试问题。
Dave
戴夫
回答by Oscar Salguero
All of a sudden I got the same problem after a Clean, at first I panicked wen I watched:
在一次 Clean 之后突然我遇到了同样的问题,起初我惊慌失措,我看着:
linker command failed with exit code 1 (use -v to see invocation)
linker command failed with exit code 1 (use -v to see invocation)
... but it turned to be really easy to fix, no command line needed!
...但它变得非常容易修复,不需要命令行!
I clicked on my project's root (the one on the top with the blueprint icon with an "A") in the Navigator, then clicked the PROJECT section (you can click on the TARGET section as well) and then clicked the button in the bottom-middle called "Validate Settings".
我在导航器中单击了我的项目的根(顶部带有带有“A”的蓝图图标的根),然后单击了项目部分(您也可以单击目标部分),然后单击底部的按钮-中间称为“验证设置”。
XCode itself validated the project files and told me that the problem was a duplicated target definition, and offered to fix it... and voilá, the problem its gone!
XCode 本身验证了项目文件并告诉我问题是重复的目标定义,并提供修复它......瞧,问题消失了!
Good luck!
祝你好运!
回答by Paul
I resolve the issue by going to "Library search path", and make sure all entries are correct.
我通过转到“库搜索路径”来解决该问题,并确保所有条目都正确无误。
回答by marklar
Personally, I had this issue when I was developing a static library
. I had the static library
target with all the production code, and a test target that pulled in the MyStaticLib.a
file as a framework.
就个人而言,我在开发static library
. 我有一个static library
包含所有生产代码的目标,以及一个将MyStaticLib.a
文件作为框架引入的测试目标。
Tests ran just fine in Xcode, but not in the terminal using xcodebuild
. The problem ended up being that the static library
target was compiling for Standard architectures
, while the test target wanted to compile for Standard architectures (including 64-bit)
. Switching the test target to Standard architectures
fixed everything.
测试在 Xcode 中运行得很好,但在使用xcodebuild
. 问题最终是static library
目标正在编译Standard architectures
,而测试目标想要编译为Standard architectures (including 64-bit)
. 切换测试目标以Standard architectures
修复所有问题。