如何在 C 中链接目标文件?因“架构 x86_64 的未定义符号"而失败; [英] How do I link object files in C? Fails with "Undefined symbols for architecture x86_64"
问题描述
所以我试图在我的文件 (file2.c) 中使用另一个 C (file1.c) 文件中定义的函数.为了做到这一点,我包含了 file1 (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.
如果要将文件链接到可执行文件,请执行
If you want to link your files to an executable do
$ gcc -o output file1.c file2.c
这篇关于如何在 C 中链接目标文件?因“架构 x86_64 的未定义符号"而失败;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!