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

发布于 2024-11-19 14:56:50 字数 4152 浏览 1 评论 0原文

我在 Xcode 4(最新的非 beta 版本)中有一个项目,在 Xcode 本身构建时构建得很好。具体来说,Ld 命令正确使用派生数据目录(其中放置构建产品,包括依赖的静态库)。

但是,当我从命令行构建相同的项目时,Ld 命令失败,因为它尝试使用项目内的 /build 文件夹,而该文件夹未填充。

我尝试调整我所知道的每个构建设置,无论是在父项目还是从属项目中。

关于从哪里开始调试这个有什么想法吗?我可以根据需要提供更多信息。

编辑 1: 完整的 Xcode 构建命令:

xcodebuild -project AppName.xcodeproj -target AppName -configuration "Config Name"

其中 AppNameConfig Name 都是构建的正确值。

编辑 2:链接 (Ld) 命令。

当在 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

当使用上面的构建命令从命令行构建时(这失败):

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"

返回:

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

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).

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.

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:

xcodebuild -project AppName.xcodeproj -target AppName -configuration "Config Name"

Where AppName and Config Name are both the correct values for the build.

Edit 2: Link (Ld) commands.

When built in Xcode (this works):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

梦过后 2024-11-26 14:56:50

好的,差不多 6(收费)小时后,我已经让构建在 Xcode 和命令行上正常工作(以及在构建服务器上,这是本次练习的重点)。

一路上,我会修复一个问题只是为了引起另一个问题 - 我显然会修复链接器/Ld问题,只会导致编译中的问题(“SomeClass未声明(在此函数中首次使用)”或“SomeHeader.h:没有这样的文件”或目录”错误很常见)。

那一次我调整了几乎所有我能找到的设置,所以很难说到底哪里出了问题以及到底是什么解决了它。

我认为可能有帮助的事情如下:

  • 转换构建以使用Xcode工作区和方案(而不是项目和目标)
  • 重新排列工作区,使应用程序项目和静态库作为同级(而不是父/子)
  • 更改 Xcode 和工作区设置以使用目标中指定的构建位置
  • 更改要使用的应用程序和库的构建产品路径../build(两个项目文件都包含在主目录的同级子文件夹中,因此将它们构建到同一文件夹中解决了原始链接器/Ld 命令问题,我认为
  • 将应用程序方案编辑为显式构建库目标,并在应用程序目标之前构建它
  • 在应用程序目标的构建阶段中,在“链接二进制文件与库”下显式添加库
  • 将库的 .a 文件引用的位置类型更改为“相对于构建产品”
  • 添加了“复制标头”构建阶段到库项目,将适当的标头添加到公共部分
  • 将库项目的公共标头文件夹路径更改为“/include”
  • 将库的安装目录更改为$(BUILT_PRODUCTS_DIR)
  • 将应用程序目标的库搜索路径和用户标头搜索路径更改为 $(BUILT_PRODUCTS_DIR) (递归)
  • 在我的构建之前添加了 Clean 命令Jenkins 构建服务器
  • 在构建命令中添加了显式 SDK 和 Arch 参数
  • 从构建配置名称中删除了空格

最终构建命令如下所示:

xcodebuild -workspace ClientName.xcworkspace -scheme AppName -configuration "ProdAdHoc" -sdk iphoneos -arch "armv6 armv7"

我在调试此问题时使用的一些有用资源:

无论如何,我希望我在上面添加了足够的关键字,以便将来遇到任何类似构建问题的任何人都能偶然发现这一点并发现它很有用。我不知道当我迁移到 Xcode 4 时,我在 Xcode 3.x 中多次执行的工作流程是如何变得如此混乱的,希望 Apple 能够在未来的版本中解决这个问题。

这对我来说是一次很棒的学习经历,经历这一切似乎确实解决了我之前遇到的自动完成问题。我会说事情本来可能会更糟;我仍然可以为 SharePoint 进行开发。

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).

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).

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)
  • 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

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.

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.

半透明的墙 2024-11-26 14:56:50

我昨天遇到了同样的问题并且能够解决它。为了缩小对詹姆斯有用的范围,我将指出我必须做的事情。我必须添加一个工作区并切换到使用工作区/方案而不是项目/目标来运行 xcodebuild。

使用workspace/scheme强制xcodebuild使用DerivedData文件夹而不是主项目下的构建输出文件夹。这允许链接器找到关联的静态库。

这篇博客文章非常有帮助:

http://blog。 Carbon Five.com/2011/05/04/automated-ad-hoc-builds-using-xcode-4/

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.

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.

This blog post was hugely helpful:

http://blog.carbonfive.com/2011/05/04/automated-ad-hoc-builds-using-xcode-4/

墨落画卷 2024-11-26 14:56:50

当我试验我的文件时,我收到此错误,我将 @implementation 添加到 .h 文件并将 .m 文件留空。我不相信这是你的错误,但如果其他人得到它,也许会检查你是否没有这样做。

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.

究竟谁懂我的在乎 2024-11-26 14:56:50

检查一下你的头文件中是否没有导入.m文件!将 .m 更改为 .h 为我解决了这个问题!

Check if you didn't import the .m files in your header files! Changing .m to .h fixed this for me!

百变从容 2024-11-26 14:56:50

我不知道这是否适合您,但就我而言,我有多个 main.m 文件。我所要做的就是从目标中分离 main.m 之一,它就起作用了。确保您的项目中没有多个 main.m

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.

你在看孤独的风景 2024-11-26 14:56:50

如果您查看构建日志,要求查看所有消息,您应该会看到一行简短的内容,上面写着“链接...”,其中几乎没有详细信息。但是,如果您右键单击该行并选择“展开所有记录”,您会得到一条非常详细的行,告诉您从 XCode 中发出了哪些命令。

这应该可以帮助您调试问题。

戴夫

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.

That should help you to debug the issue.

Dave

通知家属抬走 2024-11-26 14:56:50

我也遇到过类似的异常,
事实证明我在 project.pbxproj 中得到了一些(空)引用
在我清理了project.pbxproj中的那些空引用后,命令行构建成功,就像之前的xcode一样。
看看 Xcode 4 项目:清理 pbxproj 文件的实用程序?
utility-to-clean-up-pbxproj-file

以获取更多参考

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

for more reference

深陷 2024-11-26 14:56:50

突然我在清理后遇到了同样的问题,一开始我惊慌失措,当我看到:

链接器命令失败,退出代码1(使用-v查看调用)

...但它变得非常容易修复,不需要命令行!

我在导航器中单击了项目的根目录(顶部带有带有“A”的蓝图图标的根目录),然后单击“项目”部分(您也可以单击“目标”部分),然后单击底部的按钮-中间称为“验证设置”。

XCode 本身验证了项目文件,并告诉我问题是重复的目标定义,并提出修复它......瞧,问题消失了!

祝你好运!

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".

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!

Good luck!

为你拒绝所有暧昧 2024-11-26 14:56:50

我通过转到“库搜索路径”解决了该问题,并确保所有条目都正确。

I resolve the issue by going to "Library search path", and make sure all entries are correct.

东京女 2024-11-26 14:56:50

就我个人而言,我在开发静态库时遇到了这个问题。我有包含所有生产代码的静态库目标,以及将 MyStaticLib.a 文件作为框架引入的测试目标。

测试在 Xcode 中运行得很好,但在使用 xcodebuild 的终端中则不然。问题最终是 静态库 目标正在针对标准架构进行编译,而测试目标想要针对标准架构(包括 64 位)进行编译< /代码>。将测试目标切换到标准架构修复了一切。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文