链接时间“未定义的引用”错误

发布于 2024-09-07 06:53:20 字数 837 浏览 2 评论 0原文

我在编写 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 = Algorithm/main.o Algorithm/File1.o Algorithm/File2.o

all: final

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

I'm having a hard time writing makefiles. I have experience in using the extern variables, when I build the project without using makefiles I get absolutely no errors and I can run the program. But from the time I wrote the makefile to build the project, I'm getting undefined reference to errors.

I have more than 3 files with me but, for simplicity's sake I will make use of 3 files here to explain the setup.

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

}

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

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

Upon using the makefile I get undefined reference to errors during the linking time pointing to the File1.c and File2.c?

Am I making any mistake which the eclipse fixes on its own(when makefile is not used) and which surfaces when I use makefile?

My final makefile looks like this -

OBJ1 = algorithm/main.o algorithm/File1.o algorithm/File2.o

all: final

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆依然 2024-09-14 06:53:20

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

There is a discrepancy in your Makefile. Your final target depend on main.o, whereas you give $(OBJ1) to the linker, which includes algorithm/main.o.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文