我如何用C链接目标文件?适用于建筑x86_64的&QUOT未定义的符号与......失败; [英] How do I link object files in C? Fails with "Undefined symbols for architecture x86_64"

查看:167
本文介绍了我如何用C链接目标文件?适用于建筑x86_64的&QUOT未定义的符号与......失败;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想尝试使用在我的文件(file2.c中)另一个C(file1.c中)文件中定义的函数。我包括为了做到这一点文件1(file1.h)的标题。

So I'm trying trying to use a function defined in another C (file1.c) file in my file (file2.c). I'm including the header of file1 (file1.h) in order to do this.

不过,我不断收到以下错误,每当我尝试使用gcc来编译我的文件:

However, I keep getting the following error whenever I try to compile my file using gcc:

Undefined symbols for architecture x86_64:
  "_init_filenames", referenced from:
      _run_worker in cc8hoqCM.o
  "_read_list", referenced from:
      _run_worker in cc8hoqCM.o
ld: symbol(s) not found for architecture x86_64

我已经告诉我需要链接对象文件一起才能使用的功能从文件2文件1,但我不知道这意味着什么:(

I've been told I need to "link the object files together" in order to use the functions from file1 in file2, but I have no clue what that means :(

推荐答案

我假设你正在使用 GCC ,简单地链接目标文件做的:

I assume you are using gcc, to simply link object files do:

$ gcc -o output file1.o file2.o

要获得对象的文件,只需使用编译

To get the object-files simply compile using

$ gcc -c file1.c

这会产生file1.o等。

this yields file1.o and so on.

如果您想将文件链接到可执行DO

If you want to link your files to an executable do

$ gcc -o output file1.c file2.c

这篇关于我如何用C链接目标文件?适用于建筑x86_64的&QUOT未定义的符号与......失败;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆