编译时错误“未定义架构 x86_64 的符号”是什么意思?意思是?

发布于 2024-12-20 10:45:47 字数 961 浏览 2 评论 0原文

我正在尝试使用 C++ 教科书中示例中的相邻列表来对图形类进行编程,当我使用以下命令进行编译时: 代码: g++ -o prog 程序.cpp ...我收到以下错误:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

...这到底意味着什么?这可能是我的代码的问题,但我觉得它可能比这更深层,因为我在几个不同的项目中都遇到了同样的看似莫名其妙的错误,其中许多都是以不同的方式解决的,不幸的是完全解决了偶然。

我在某处读到,这可能与我使用 32 位还是 64 位库有关,并且可能需要使用标签 -m32 或 -m64,但我不确定这是否适用于这里。有趣的是,当我尝试添加 -m64 标签时,我得到了同样的错误,但是当我尝试使用 -m32 标签时,我得到了同样的错误,只是它说的

Undefined symbols for architecture i386:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

是。

主要是我只是想知道这个错误到底在说什么。我习惯于调试编译时错误,这些错误会在代码中给出特定行等,但我无法从中辨别出类似的内容。有什么想法吗?

如果有帮助的话,我使用的是 2008 年末配备 Intel Core 2 Duo(64 位)的 Macbook,并且运行的是 OS X Lion (10.7.2),我认为这是最新版本。另外,我使用的是 gcc 版本 4.2.1。

I'm trying to program a graph class using an adjacent list from an example in my C++ text book, and when I compile using this command:
Code:
g++ -o prog program.cpp
...I get the following error:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

... what in the world does this mean? It may turn out to be an issue with my code, but I feel like it may be deeper than that, because I've gotten this same seemingly inexplicable error for several different projects, many of which were solved in different ways, and unfortunately completely by accident.

I read somewhere that it may have to do with whether I'm using 32 bit or 64 bit libraries, and that the tags -m32 or -m64 may need to be used, but I'm not sure if this applies here. Interestingly enough, when I tried adding the -m64 tag I got the same exact error, but when I tried using the -m32 tag I got the same error, except it said

Undefined symbols for architecture i386:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

instead.

Mainly I just want to know what in the world the error is saying. I'm used to debugging compile-time errors that give a specific line in the code, etc., but I can't discern anything like that from this. Any ideas?

If it helps, I'm using a late 2008 Macbook with Intel Core 2 Duo, (so 64-bit), and I'm running OS X Lion (10.7.2), which I think is the latest version. Also, I'm using gcc version 4.2.1.

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

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

发布评论

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

评论(1

行至春深 2024-12-27 10:45:47

当您编译文件时,编译器会调用尝试生成可执行文件的链接器。但它不能,因为您没有提供名为 main 的函数,该函数将在程序启动时执行。

要么您不想运行链接器,因为您想单独编译多个文件,然后将它们组合起来。在这种情况下,请使用 -c 标志告诉编译器跳过链接阶段。

或者您想执行编译后的文件。然后你需要实现函数main

When you compile the file, the compiler invokes the linker which tries to generate an executable. But it cannot because you didn't provide a function named main which is the function that will be executed when your program is launched.

Either you don't want to run the linker because you want to compile several files separately then combine them. In that case, use the -c flag to tell the compiler to skip the link stage.

Or either you want to execute the compiled file. Then you need to implement the function main.

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