【问题标题】:Error when linking C libraries downloaded from Homebrew to Xcode Project将从 Homebrew 下载的 C 库链接到 Xcode 项目时出错
【发布时间】:2017-07-19 17:03:01
【问题描述】:

伙计们,我已经苦苦挣扎了两天,试图让 GDAL 库与我的 iOS 应用程序一起工作。每当我尝试构建我的程序时,都会收到如下错误:

Ld /Users/scottdriggers/Library/Developer/Xcode/DerivedData/Mapper-goynotoybcqaezfpmrcmfwumcokh/Build/Products/Debug-iphoneos/Mapper.app/Mapper normal arm64
cd /Users/scottdriggers/Documents/XcodeProjects/Mapper
export IPHONEOS_DEPLOYMENT_TARGET=9.0
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.3.sdk -L/Users/scottdriggers/Library/Developer/Xcode/DerivedData/Mapper-goynotoybcqaezfpmrcmfwumcokh/Build/Products/Debug-iphoneos -L-L/usr/local/Cellar/gdal/1.11.5_2/lib -L-lgdal -L/usr/local/Cellar/gdal/1.11.5_2/lib -F/Users/scottdriggers/Library/Developer/Xcode/DerivedData/Mapper-goynotoybcqaezfpmrcmfwumcokh/Build/Products/Debug-iphoneos -F/Users/scottdriggers/Documents/XcodeProjects/Mapper/Pods/DJI-SDK-iOS/iOS_Mobile_SDK -F/Users/scottdriggers/Documents/XcodeProjects/Mapper/Pods/DJI-UILibrary-iOS/iOS_UILibrary -filelist /Users/scottdriggers/Library/Developer/Xcode/DerivedData/Mapper-goynotoybcqaezfpmrcmfwumcokh/Build/Intermediates/Mapper.build/Debug-iphoneos/Mapper.build/Objects-normal/arm64/Mapper.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks -Xlinker -rpath -Xlinker @executable_path/Frameworks -miphoneos-version-min=9.0 -dead_strip -Xlinker -object_path_lto -Xlinker /Users/scottdriggers/Library/Developer/Xcode/DerivedData/Mapper-goynotoybcqaezfpmrcmfwumcokh/Build/Intermediates/Mapper.build/Debug-iphoneos/Mapper.build/Objects-normal/arm64/Mapper_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -fobjc-arc -fobjc-link-runtime -ObjC -framework DJISDK -framework DJIUILibrary -ObjC -framework DJISDK -framework DJIUILibrary -I/usr/local/Cellar/gdal/1.11.5_2/include -lgdal.1 -framework CoreGraphics -framework MapKit -framework CoreLocation -framework UIKit -framework Foundation /Users/scottdriggers/Library/Developer/Xcode/DerivedData/Mapper-goynotoybcqaezfpmrcmfwumcokh/Build/Products/Debug-iphoneos/VideoPreviewer.framework/VideoPreviewer -lPods-Mapper -Xlinker -dependency_info -Xlinker /Users/scottdriggers/Library/Developer/Xcode/DerivedData/Mapper-goynotoybcqaezfpmrcmfwumcokh/Build/Intermediates/Mapper.build/Debug-iphoneos/Mapper.build/Objects-normal/arm64/Mapper_dependency_info.dat -o /Users/scottdriggers/Library/Developer/Xcode/DerivedData/Mapper-goynotoybcqaezfpmrcmfwumcokh/Build/Products/Debug-iphoneos/Mapper.app/Mapper

ld: warning: directory not found for option '-L-L/usr/local/Cellar/gdal/1.11.5_2/lib'
ld: warning: directory not found for option '-L-lgdal'
ld: warning: ignoring file /usr/local/Cellar/gdal/1.11.5_2/lib/libgdal.1.dylib, file was built for x86_64 which is not the architecture being linked (arm64): /usr/local/Cellar/gdal/1.11.5_2/lib/libgdal.1.dylib
Undefined symbols for architecture arm64:
"_GDALGetRasterXSize", referenced from:
  -[GEOTIFFManager averageFile:] in GEOTIFFManager.o
"_GDALGetRasterYSize", referenced from:
  -[GEOTIFFManager averageFile:] in GEOTIFFManager.o

...许多类似的问题...

ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

像我这样的EXPERT LEVEL PROGRAMMER,立马去google搜索其他有类似问题的,发现有不少。按照他们的建议,我按照不同的顺序多次做了以下所有事情:

我清理并尝试再次构建项目
我删除了 Xcode 的派生数据文件夹的内容
我将标题搜索路径、其他链接器标志、框架搜索路径和库搜索路径更新为我推荐的所有内容,但没有任何帮助。这些设置的当前状态如下:
框架搜索路径:
$(inherited)
标头搜索路径:
$(继承)
"${PODS_ROOT}/Headers/Public/DJI-SDK-iOS"
"${PODS_ROOT}/Headers/Public/DJI-UILibrary-iOS"
/usr/local/Cellar/gdal/1.11.5_2/include
/usr/local/include
库搜索路径:
-L/usr/local/Cellar/gdal/1.11.5_2/lib
-lgdal
/usr/local/Cellar/gdal/1.11.5_2/lib
运行路径搜索路径:
$(继承)
@executable_path/框架

我收到了更多此类错误,我可以通过在目标的构建阶段中将一些库添加到“将二进制文件与库链接”选项卡中来修复其中的一些错误,但这些 GDAL 错误不会消失,即使当我从 /usr/local/lib 文件链接库。

Link Binaries With Libraries

背景:我需要从我的程序的 GEOTiff 文件中查看一个像素。我正在用 OBJC 编写我的应用程序,但 GDAL 库适用于 C++ 或 C。
我使用 Homebrew 安装了这些库,一切顺利,所有文件都链接在 /usr/local 中,但它们不能在 Xcode 中正常工作。我最初开始用 C++ 编写这个文件,但在经历了一天这样的错误后切换到 C。在切换到 C 之后(所以我不必在 OBJC++ 中编译),我仍然会遇到同样的错误。

注意:我也在使用 Cocoapods 来运行其他一些库,但 Cocoapods 没有 GDAL 库,否则我会尝试这条路线。

情节变深:当我为 iPhone 模拟器运行程序而不是尝试在我的 iPhone 7 上运行它时,程序编译得很好并显示“构建成功”,但我得到了另一个尝试将其附加到模拟器时出错:

dyld: Library not loaded: /usr/local/opt/gdal/lib/libgdal.1.dylib
Referenced from: /Users/scottdriggers/Library/Developer/CoreSimulator/Devices/1618C362-7271-4E1D-93C7-DE7CE2F15F59/data/Containers/Bundle/Application/905B2454-C731-42DC-8963-5B5C3F4A62A6/Mapper.app/Mapper
Reason: no suitable image found.  
Did find: /usr/local/opt/gdal/lib/libgdal.1.dylib: mach-o, but not built for iOS simulator

我认为没有必要包含导致错误的文件,但是如果有人想看一下,这里有一个示例:

#include "GEOTIFFManager.h"
#include "gdal.h"
#include "cpl_conv.h"
@interface GEOTIFFManager()
@end
@implementation GEOTIFFManager
- (id) init{
    self = [super init];
    GDALAllRegister();
    return self;
}
- (float) getPixelValueOfCoordinate:(double *)coordinate FromFile:(char *)name{
    GDALDatasetH* inFile = (GDALDatasetH*)GDALOpen(name, GA_ReadOnly);
    double* transformation = NULL;
    GDALGetGeoTransform(inFile, transformation);
    double* transformedCoord;
    transformedCoord = reverseTransform(coordinate, transformation);
    int pixelX = floor(transformedCoord[0]);
    int pixelY = floor(transformedCoord[1]);
    float tempArray[1] = {-1.0};
    float* output = tempArray;
    GDALRasterBandH Band = GDALGetRasterBand(inFile, 1);
    GDALRasterIO(Band, GF_Read, pixelX, pixelY, 1, 1, output, 1, 1, GDT_CFloat32, 0, 0);
    return output[0];
}

无论如何,谢谢大家的帮助,我很感激。希望大家能提供一些有用的建议。

【问题讨论】:

    标签: ios c xcode frameworks gdal


    【解决方案1】:

    我没有做过任何 Xcode 或 iOS 开发,但我建议在你做任何其他事情之前修复链接器错误:

    ld: warning: directory not found for option '-L-L/usr/local/Cellar/gdal/1.11.5_2/lib'

    链接器找不到目录-L/usr/local/Cellar/gdal/1.11.5_2/lib。看起来您一个接一个地有两个 -L 选项,所以我会删除第一个,然后仔细检查目录 /usr/local/Cellar/gdal/1.11.5_2/lib 是否存在并包含您想要的库。这可能是 Xcode 配置问题。查看您配置链接器的位置。

    ld: warning: directory not found for option '-L-lgdal'

    同样的问题,-L 选项指定了一个额外的目录来查找库,-l 选项提供了一个要包含的特定库(在本例中为 gdal)。字符串-L-lgdal 没有意义,但-lgdal 确实有意义。

    ld: warning: ignoring file /usr/local/Cellar/gdal/1.11.5_2/lib/libgdal.1.dylib, file was built for x86_64 which is not the architecture being linked (arm64): /usr/local/Cellar/gdal/1.11.5_2/lib/libgdal.1.dylib

    找到的库适用于 x86_64 架构,但您正在尝试构建 arm64 应用程序。如果您将库作为预构建的二进制文件获取,则查找 arm64 二进制文件。如果您构建了这些库,那么您希望为 arm64 架构重新构建它们。

    【讨论】:

    • 当我在 Xcode 中将这些路径添加到“库搜索路径”时,我添加它们时没有使用“-L”,它提供了“-L”我应该添加路径“/usr/local/ Cellar/gdal/1.11.5_2/lib-lgdal”这样的?
    • @ScottDriggers 我从未使用过 Xcode,抱歉。我只能说ld 没有得到你想要的参数。
    • @ScottDriggers 但是图书馆搜索路径听起来像是指定该路径的正确位置,而在其他地方您需要指定图书馆gdal
    【解决方案2】:

    我找到了答案。我试图将 Mac OSX 应用程序安装到我的 iOS 应用程序中。是的,我知道,非常愚蠢。我通过谷歌搜索解决了这个问题,直到我发现有人为此解决方案提供了tutorial。希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多