c - 链接时间 "undefined reference to "错误

标签 c makefile variables extern undefined-reference

我很难编写 makefile。我有使用 extern 变量的经验,当我在不使用 makefile 的情况下构建项目时,我绝对没有错误,我可以运行该程序。但是从我编写 makefile 来构建项目时,我收到了 undefined reference to 错误。

我有超过 3 个文件,但为了简单起见,我将在此处使用 3 个文件来解释设置。

/************Project********/
/* main.c */
int x;
main()
{
...

}

/* File1.c*/
extern int x;
fn1()
{
 ...
 }

/* File2.c*/
extern int x;
fn2()
{
 ...
 }
/*******************************/

在使用 makefile 时,在指向 File1.c 和 File2.c 的链接期间出现undefined reference to 错误?

我是否犯了 eclipse 自行修复的错误(当未使用 makefile 时)以及我使用 makefile 时的表面?

我的最终 makefile 看起来像这样 -

OBJ1 =算法/main.o算法/File1.o算法/File2.o

all: final

final: main.o algorithm/File1.o algorithm/File2.o
 @echo "Linking - making Final"
 $(CC) -o $@ $(OBJ1)

最佳答案

您的 Makefile 中存在差异。您的 final 目标取决于 main.o,而您将 $(OBJ1) 提供给链接器,其中包括 algorithm/main。 o.

关于c - 链接时间 "undefined reference to "错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3139944/

相关文章:

javascript - 简单的 js FOR 循环返回 'undefined'

c - *** 检测到 glibc *** ./a.out : double free or corruption (top): 0x08901d70 *** while attempting to free a BST

makefile - GNU make 相当于 BSD make 的 $(var :Q)?

makefile - makefile 的诊断输出(如 make[N])的含义

动态 CSS 中的 PHP 变量

python - 我想引用另一个 python 脚本中的变量

c - 测量上下文切换时间 C (Windows)

c++ - 为什么这个 OpenGL 旋转也会平移我的对象?

c - 未找到 mqueue.h

c - 智能 Makefile : way scan directory tree and compile . c 文件