gpt4 book ai didi

What does the compile-time error "Undefined symbols for architecture x86_64" mean?(编译时错误“未定义的体系结构符号x86_64”是什么意思?)

转载 作者:bug小助手 更新时间:2023-10-24 21:21:53 27 4
gpt4 key购买 nike



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:

我正在尝试使用C++课本中一个示例中的相邻列表来编写图形类,当我使用以下命令进行编译时:code:g++-o prog Program.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


... 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

我在某个地方读到,这可能与我使用的是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


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.

如果有帮助的话,我使用的是2008年末搭载英特尔酷睿2双核处理器的Macbook(64位),我运行的是OS X Lion(10.7.2),我认为这是最新版本。还有,我用的是GCC 4.2.1版。


更多回答

Does your project have .hpp files? See stackoverflow.com/questions/1757509/…

您的项目是否有.hpp文件?参见Stackoverflow.com/Questions/1757509/…

does your program have main() function?

您的程序有main()函数吗?

I got this from a typo naming main() as man(). Silly mistake and the editor language server didn't have any warnings about it either.

这是因为一个打字错误,将main()命名为man()。愚蠢的错误,编辑语言服务器也没有任何关于它的警告。

优秀答案推荐

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.

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


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.

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


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

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


更多回答

Where would you use this -c flag? Can you give an example?

您将在哪里使用这个-c标志?你能举个例子吗?

@Goldname I was able to pass the -c flag to my C compiler (gcc) in my command line terminal (I'm on a Macbook and am using Terminal). I typed the following into the CLI: gcc -c myfilename.c

@Goldname我能够在我的命令行终端(我在Macbook上,正在使用终端)中将-c标志传递给我的C编译器(GCC)。我在CLI中键入了以下内容:GCC-c myfilename.c

Helped me a lot when I was trying to create a Swift package using swift package manager. However. Swift package manager expects a file called main in the target directory. So if you have a target named Foo that is executable you should also have a file Sources/Foo/main.swift.

当我尝试使用SWIFT包管理器创建SWIFT包时,这对我帮助很大。然而。SWIFT包管理器希望目标目录中有一个名为Main的文件。因此,如果您有一个名为foo的可执行目标,那么您还应该有一个文件Sources/foo/main.swft。

What makes you to say OP didn't provided main function?

是什么让你说OP没有提供主函数?

@RichieThomas I've encountered this using ld command. How do I skip this? ld doesn't have -c option in MacOSX

@RichieThomas我在使用ID命令时遇到过这种情况。我怎么跳过这个?LD在MacOSX中没有-c选项

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com