简体   繁体   English

Xcode 4 构建成功,命令行构建失败?

[英]Xcode 4 build succeeds, command line build fails?

I have a project in Xcode 4 (the latest non-beta version) that builds fine when built in Xcode itself.我在 Xcode 4(最新的非测试版)中有一个项目,在 Xcode 本身构建时构建良好。 Specifically, the Ld command correctly uses the derived data directory (where build products, including a dependent static library, are placed).具体来说,Ld 命令正确使用派生数据目录(其中放置构建产品,包括依赖的 static 库)。

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.其中AppNameConfig 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

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 undeclared (first use in this function)”或“SomeHeader.h: No such file或目录”错误很常见)。

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 might have helped are are as follows:我认为可能有帮助的事情如下:

  • Converted build to use an Xcode workspace & scheme (instead of project & target)将构建转换为使用 Xcode 工作区和方案(而不是项目和目标)
  • Rearranged workspace to have the App project and static library as siblings (not as parent/child)重新排列工作区以将 App 项目和 static 库作为兄弟姐妹(而不是作为父/子)
  • Changed Xcode and workspace settings to use build locations specified in targets更改了 Xcode 和工作区设置以使用目标中指定的构建位置
  • 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 )更改应用程序和库的构建产品路径以使用../build(两个项目文件都包含在主目录的同级子文件夹中,因此让它们构建到同一个文件夹中解决了原始链接器/Ld 命令问题,我认为
  • Edited the App scheme to explicitly build the Library target, and build it before the App target编辑 App 方案以显式构建 Library 目标,并在 App 目标之前构建它
  • In the Build Phases for the App target, explicitly add the Library under "Link Binary With Libraries"在 App 目标的 Build Phases 中,在“Link Binary With Libraries”下显式添加库
  • Change the location type of the Library's.a file reference to "Relative to Build Products"将 Library's.a 文件引用的位置类型更改为“相对于构建产品”
  • 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"将 Library 项目的 Public Headers 文件夹路径更改为“/include”
  • Changed the Installation Directory of the Library to $(BUILT_PRODUCTS_DIR)将库的安装目录更改为$(BUILT_PRODUCTS_DIR)
  • Changed the Library Search Paths and the User Header Search Paths of the App target to $(BUILT_PRODUCTS_DIR) (recursive)将 App 目标的 Library Search Paths 和 User Header Search Paths 更改为$(BUILT_PRODUCTS_DIR) (递归)
  • Added a Clean command before the build on my Jenkins build server在我的 Jenkins 构建服务器上构建之前添加了一个 Clean 命令
  • Added explicit SDK and Arch arguments to the build command在构建命令中添加了显式 SDK 和 Arch arguments
  • Removed spaces from build configuration name从构建配置名称中删除了空格

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:我在调试此问题时使用的一些有用资源:

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 开发。

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.使用工作空间/方案强制 xcodebuild 使用 DerivedData 文件夹,而不是主项目下的 build output 文件夹。 This allowed the linker to find the associated static library.这允许 linker 找到相关的 static 库。

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/

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.我在试验文件时遇到此错误,我将@implementation 添加到.h 文件并将.m 文件留空。 I don't believe this is your error but if anyone else gets it maybe check that you haven't done this.我不相信这是你的错误,但如果其他人得到它,可能会检查你没有这样做。

Check if you didn't import the.m files in your header files.检查您是否没有在 header 文件中导入 .m 文件。 Changing.m to .h fixed this for me!将.m 更改为 .h 为我解决了这个问题!

I don't know if this will work for you but in my case, I had more than one main.m file.我不知道这是否对您有用,但就我而言,我有多个main.m文件。 All I had to do was detach one of the main.m from the target and it worked.我所要做的就是将一个main.m从目标中分离出来,它就可以工作了。 Make sure you don't have more than one main.m in your project.确保项目中的main.m不超过一个。

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.但是,如果您右键单击该行并 select “展开所有成绩单”,您会得到一个非常详细的行,告诉您从 XCode 中发出了什么命令。

That should help you to debug the issue.这应该可以帮助您调试问题。

Dave戴夫

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.我遇到了类似的异常,事实证明,在我清理了 project.pbxproj 中的 null 引用之后,我在 project.pbxproj 中获得了一些(空)引用,命令行构建成功,就像之前的 xcode 一样。 Take a look at Xcode 4 project: utility to clean up pbxproj file?看看Xcode 4 项目:清理 pbxproj 文件的实用程序? utility-to-clean-up-pbxproj-file实用程序清理 pbxproj 文件

for more reference更多参考

All of a sudden I got the same problem after a Clean, at first I panicked wen I watched:突然间我在清洁后遇到了同样的问题,起初我看到我惊慌失措:

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”的蓝图图标的那个),然后单击了项目部分(您也可以单击目标部分),然后单击底部的按钮-middle 称为“验证设置”。

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!祝你好运!

I resolve the issue by going to "Library search path", and make sure all entries are correct.我通过转到“库搜索路径”解决了这个问题,并确保所有条目都正确。

Personally, I had this issue when I was developing a static library .就个人而言,我在开发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目标,以及一个将MyStaticLib.a文件作为框架拉入的测试目标。

Tests ran just fine in Xcode, but not in the terminal using xcodebuild .测试在 Xcode 中运行得很好,但在使用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) .问题最终是static library目标正在为Standard architectures编译,而测试目标想要为Standard architectures (including 64-bit)编译。 Switching the test target to Standard architectures fixed everything.将测试目标切换到Standard architectures修复了所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM