c - 为什么 `ld`无法链接该程序?

标签 c xcode

这个命令没有将我的文件链接到我想要使用的库是什么?

> ld elements.o ~/Code/MyLibraries/usr/local/lib/libexpat.a

背景故事:

所以...我是一名 C 新手,正在尝试在我的一个项目中使用 libexpat

我能够编译该库并使其正常工作。此外,我还能够运行示例程序。极好的。现在我想修改示例程序,看看能走多远。

这就是我陷入困境的地方,因为我不太明白编译是如何工作的。 ./configuremake 在幕后做了很多事情。

现在,链接库时,ldlibexpat.a 中找不到 (xmlparse.o)(请参阅下面的完整输出) ...但是我知道该库可以工作,因为我可以在 XCode 中运行它。我暂时将其拖放到那里。

我很乐意从命令行构建它,以便我知道 XCode 在幕后做了什么以使这一切正常工作。

根据这个article ...ld 应该可以工作,但它让我失败。

尝试对构建示例文件的 Makefile 进行逆向工程也有点令人困惑,我无法理解它。这是当我使用示例程序 elements.c grep 时 Makefile 中的内容:

elements_SOURCES = elements.c
elements_LDADD = ../lib/libexpat.la
elements$(EXEEXT): $(elements_OBJECTS) $(elements_DEPENDENCIES) $(EXTRA_elements_DEPENDENCIES)

你能帮我弄清楚这个命令吗? XCode/Make 在幕后做了什么才能使其发挥作用?

Undefined symbols for architecture x86_64:
  "___assert_rtn", referenced from:
      _XML_GetParsingStatus in libexpat.a(xmlparse.o)
      _doProlog in libexpat.a(xmlparse.o)
  "___bzero", referenced from:
      _lookup in libexpat.a(xmlparse.o)
  "___stack_chk_fail", referenced from:
      _main in elements.o
      _lookup in libexpat.a(xmlparse.o)
      _storeAtts in libexpat.a(xmlparse.o)
      _findEncoding in libexpat.a(xmltok.o)
      _findEncodingNS in libexpat.a(xmltok.o)
  "___stack_chk_guard", referenced from:
      _main in elements.o
      _lookup in libexpat.a(xmlparse.o)
      _storeAtts in libexpat.a(xmlparse.o)
      _findEncoding in libexpat.a(xmltok.o)
      _findEncodingNS in libexpat.a(xmltok.o)
  "___stderrp", referenced from:
      _main in elements.o
      _startParsing in libexpat.a(xmlparse.o)
  "___stdinp", referenced from:
      _main in elements.o
  "_arc4random_buf", referenced from:
      _startParsing in libexpat.a(xmlparse.o)
  "_fprintf", referenced from:
      _main in elements.o
      _startParsing in libexpat.a(xmlparse.o)
  "_fread", referenced from:
      _main in elements.o
  "_free", referenced from:
      _parserCreate in libexpat.a(xmlparse.o)
  "_getenv", referenced from:
      _startParsing in libexpat.a(xmlparse.o)
  "_malloc", referenced from:
      _parserCreate in libexpat.a(xmlparse.o)
  "_memcmp", referenced from:
      _doContent in libexpat.a(xmlparse.o)
  "_memcpy", referenced from:
      _parserInit in libexpat.a(xmlparse.o)
      _XML_SetEncoding in libexpat.a(xmlparse.o)
      _XML_Parse in libexpat.a(xmlparse.o)
      _XML_GetBuffer in libexpat.a(xmlparse.o)
      _poolGrow in libexpat.a(xmlparse.o)
      _storeRawNames in libexpat.a(xmlparse.o)
      _storeAtts in libexpat.a(xmlparse.o)
      ...
  "_memmove", referenced from:
      _XML_GetBuffer in libexpat.a(xmlparse.o)
  "_memset", referenced from:
      _handleUnknownEncoding in libexpat.a(xmlparse.o)
  "_printf", referenced from:
      _startElement in elements.o
  "_putchar", referenced from:
      _startElement in elements.o
  "_realloc", referenced from:
      _parserCreate in libexpat.a(xmlparse.o)
  "_strcmp", referenced from:
      _startParsing in libexpat.a(xmlparse.o)
ld: symbol(s) not found for architecture x86_64

最佳答案

这里是让链接器工作的完整命令:

ld -o my_linked_elements elements.o ~/Code/MyLibraries/usr/local/lib/libexpat.a \
   -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/lib/darwin/libclang_rt.osx.a

关于c - 为什么 `ld`无法链接该程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980007/

相关文章:

c - 为什么 malloc(sizeof(pointer)) 有效?

c - C 中人类可读的十六进制时间戳

iphone - UIWebView 中的谷歌地图从核心传递数据

Xcode 无法创建输出文件,权限被拒绝

ios - 以 NSException 类型的未捕获异常终止

c - 我的 if 语句不起作用

c - 为什么此c编程给出需要的结果?

c - 如何将表存储到链表中?

objective-c - iOS 中的 ViewDidLoad 和 UINavigationController?

iphone - 如何渲染内存中 UIViewController 的 View 景观?