使用 gdb 在 Eclipse 中调试简单 C++ 时出现“没有可用于 main() 的源”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2324602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:02:40  来源:igfitidea点击:

"No source available for main()" error when debugging simple C++ in Eclipse with gdb

c++eclipsedebuggingmacosgdb

提问by thoughton

I'm having trouble debugging a C++ program in Eclipse (the latest RC of Helios, updated with latest CDT from within itself) on OSX.

我在 OSX 上的 Eclipse(Helios 的最新 RC,从其内部使用最新的 CDT 更新)中调试 C++ 程序时遇到问题。

The program is very simple (esentially Lesson 2 from NeHe's OpenGL tutorials), consisting of one cpp file and, using OpenGL and Cocoa frameworks, and linking with libSDL.a and libSDLmain.a.

该程序非常简单(本质上是 NeHe 的 OpenGL 教程中的第 2 课),由一个 cpp 文件组成,使用 OpenGL 和 Cocoa 框架,并与 libSDL.a 和 libSDLmain.a 链接。

The structure of the project is very simple: the source file(s) are in a subdirectory of the project called src/ and the executable is built to the project's root directory.

项目的结构非常简单:源文件位于名为 src/ 的项目子目录中,可执行文件构建到项目的根目录中。

The problem is that whenever I try to add breakpoints and debug it, the breakpoints seem to get hit perfectly but no source is displayed - instead I just get a "No source available for main()" error in the code window.

问题是,每当我尝试添加断点并对其进行调试时,断点似乎都被完美命中,但没有显示源 - 相反,我只是在代码窗口中收到“没有可用于 main() 的源”错误。

The compiler flags have optimisations set to none, and both the compiler and linker have the debug symbols flag set (-g).

编译器标志的优化设置为无,编译器和链接器都设置了调试符号标志 (-g)。

The debugging setting in Eclipse is set to "Standard spawn progess" and the debugger is set to "gdb".

Eclipse 中的调试设置设置为“Standard spawn progess”,调试器设置为“gdb”。

Now the strangest thing is that if I try to debug the exact same executable - ie. the exact same one that was built by Eclipse - using gdb from the Terminal (shell) then everything works fine. Breakpoints are hit, source code is displayed, no problems at all.

现在最奇怪的是,如果我尝试调试完全相同的可执行文件 - 即。与 Eclipse 构建的完全相同 - 使用终端(shell)中的 gdb 然后一切正常。断点命中,源代码显示,完全没有问题。

I've made sure that both Eclipse and the shell are using the same gdb executable, and they are (it's /usr/bin/gdb).

我已经确保 Eclipse 和 shell 都使用相同的 gdb 可执行文件,它们是(它是 /usr/bin/gdb)。

Now I may be wrong, but this all suggests to me that there can't be a problem with the compiler and linker flags (because the same executable is debuggable from the shell), so presumably the problem must be with how gdb is being invoked from within Eclipse? Perhaps when run from Eclipse gdb is picking up different config files or something than when it's run from the shell? (Anyone know?)

现在我可能错了,但这一切都向我表明编译器和链接器标志不会有问题(因为可以从 shell 调试相同的可执行文件),所以大概问题必须出在 gdb 的调用方式上从 Eclipse 内部?也许当从 Eclipse 运行时 gdb 会选择不同的配置文件或其他东西而不是从 shell 运行时?(有人知道吗?)

I'd really appreciate any help with this because it's slowly driving me loopy!

我真的很感激这方面的任何帮助,因为它正在慢慢地让我发疯!

Please let me know if there are any other details that would be useful - exact version numbers of Eclipse/cdt/gdb, exact linker/compiler command lines, etc. - and I'll very gladly update this post with them.

请让我知道是否有任何其他有用的细节 - Eclipse/cdt/gdb 的确切版本号、确切的链接器/编译器命令行等 - 我很乐意用它们更新这篇文章。

Many thanks in advance,

提前谢谢了,

thoughton.

思想。

--- edited @ "14 hours ago"---

---编辑@“14 小时前”---

I tried the "add filesystem path" (with "search sub-folders") option, but that didn't work. I also tried creating a new completely flat project, but that didn't work either. I even tried getting a Galileo release (eclipse-SDK-3.5.2RC4 with CDT update), but that made no difference (apart from gdb being slower to launch).

我尝试了“添加文件系统路径”(带有“搜索子文件夹”)选项,但这不起作用。我也尝试创建一个新的完全扁平的项目,但这也不起作用。我什至尝试获得 Galileo 版本(带有 CDT 更新的 eclipse-SDK-3.5.2RC4),但这没有任何区别(除了 gdb 启动速度较慢)。

And here's something else strange I noticed: once I get the "No source available" message, if I then switch Eclipse's Console to display the "gdb" console, and also turn on "Verbose console mode" so I can communicate it, I can then issue "l" and "bt" commands and have them work succesfully, showing the correct source and stack where my breakpoint was hit. Which, correct me if I'm wrong, must mean that the information is there and gdb is being invoked correctly - so why will Eclipse not see this information?

我注意到还有一些奇怪的事情:一旦我收到“无源可用”消息,如果我然后切换 Eclipse 的控制台以显示“gdb”控制台,并打开“详细控制台模式”以便我可以进行通信,我可以然后发出“l”和“bt”命令并让它们成功工作,显示正确的源和堆栈,我的断点被击中。如果我错了,请纠正我,这一定意味着信息在那里并且 gdb 被正确调用 - 那么为什么 Eclipse 看不到这些信息?

I'm getting close to giving up on Eclipse to be honest... I came to it with such high hopes, too.

老实说,我已经快要放弃 Eclipse 了……我也怀着如此高的期望来到了它。

Any additional help or thoughts would be hugely appreciated.

任何额外的帮助或想法将不胜感激。

t.

吨。

采纳答案by thoughton

I found the answer! And it's embarrassingly simple.

我找到了答案!而且简单得令人尴尬。

The problem was that I was using the Release version of SDL instead of the Debug version! (I had 'libsdl' from MacPorts whereas I should have had 'libsdl-devel'.)

问题是我使用的是 SDL 的发布版本而不是调试版本!(我有来自 MacPorts 的 'libsdl' 而我应该有 'libsdl-devel'。)

So my generic answer is: make sure the libs you're linking against were compiled with debug flags set too, it's not always enough to just make sure your own code has them set.

所以我的通用答案是:确保你链接的库也是用设置的调试标志编译的,仅仅确保你自己的代码设置了它们并不总是足够的。

回答by VonC

This threadsuggests:

该线程建议:

-g -O0

for debug flags to be set for Eclipse CDT compilation.
Sometime, it is simple a problem of rebuilding completely the application (like here)

用于为 Eclipse CDT 编译设置调试标志。
有时,完全重建应用程序很简单(就像这里

See also this threaddescribing a similar situation:

另请参阅此线程,描述类似情况:

I have noticed that sometimes in Eclipse I have to go and specifically add the path to my source files using the "add filesystem path" (with "search sub-folders") in the Debug Dialog (even when they are in the same project I am debugging), but I have not noticed a pattern to when I have to do this. But it may be worth a try.

我注意到有时在 Eclipse 中,我必须在调试对话框中使用“ add filesystem path”(带有“ search sub-folders”)专门添加源文件的路径(即使它们在我正在调试的同一个项目中),但我有当我必须这样做时没有注意到模式。但这可能值得一试。

回答by user206622

Here is another reason for this problem. My configuration used -g3 as the option to gcc. Changing it to -g solved the problem. There seems to be some incompatibility between gcc and gdb. I checked that gdb was the latest revision (using apt-get).

这是这个问题的另一个原因。我的配置使用 -g3 作为 gcc 的选项。将其更改为 -g 解决了问题。gcc 和 gdb 之间似乎有些不兼容。我检查了 gdb 是最新版本(使用 apt-get)。

回答by ulyssis2

I would like to add a little new blood to this old thread.

我想为这个旧线程添加一点新鲜血液。

I encountered this problem when I tried to compile and debug a gnu arm project.

我在尝试编译和调试 gnu arm 项目时遇到了这个问题。

I solved the problem by modifying the Makefile: adding "-g -O0" at the end of this line "CFLAGS += -Wall -Werror -O3"

我通过修改 Makefile 解决了这个问题:在“CFLAGS += -Wall -Werror -O3”这一行的末尾添加“-g -O0”

回答by Almir

Go to project Properties, C/C++ Build -> Settings. On the first tab (Tool Settings) under Cross GCC Compiler click Debugging and set Debug Level to Maximum (-g3)

转到项目属性,C/C++ 构建 -> 设置。在 Cross GCC Compiler 下的第一个选项卡(Tool Settings)上,单击 Debugging 并将 Debug Level 设置为 Maximum (-g3)

回答by Brian

I had this issue when I compiled the latest gcc, but did not update to the most recent gdb. After the update, it worked properly.

我在编译最新的 gcc 时遇到了这个问题,但没有更新到最新的 gdb。更新后,它工作正常。

回答by Guy Avraham

Thought to mention, that in case you are using cmake to build the project, one approach to the solution will be to add the "debug flag" to the cmake command, i.e. -

想提一下,如果您使用 cmake 来构建项目,解决方案的一种方法是将“调试标志”添加到 cmake 命令,即 -

$ cmake /path/to/main/cmake_file -DCMAKE_BUILD_TYPE=Debug

$ cmake /path/to/main/cmake_file -DCMAKE_BUILD_TYPE=Debug

回答by Jeff Pal

I had a similar problem. I was using CFLAGS=-Wall -O2 -fPIC -DPIC -lm -lasoundand never had problem to compile it but when I tried to debbug it on Eclipse IDE I get this error: No source available for "main() at 0x401080"then I added -gto this line and it worked well:

我有一个类似的问题。我正在使用CFLAGS=-Wall -O2 -fPIC -DPIC -lm -lasound并且编译它从来没有问题但是当我尝试在 Eclipse IDE 上调试它时我收到这个错误:No source available for "main() at 0x401080"然后我添加-g到这一行并且它运行良好:

CFLAGS=-g -Wall -O2 -fPIC -DPIC -lm -lasound

CFLAGS=-g -Wall -O2 -fPIC -DPIC -lm -lasound

回答by Jon

For anyone else who may experience this issue,

对于可能遇到此问题的任何其他人,

I installed the linuxtools/valgrind plugin last night to do some memory profiling, and it seems that this broke the normal gdb. when i removed the linuxtools plugins everything started working as normal again.

我昨晚安装了 linuxtools/valgrind 插件来做一些内存分析,这似乎打破了正常的 gdb。当我删除 linuxtools 插件时,一切又开始正常工作。

So you might like to try that.

所以你可能想尝试一下。

回答by iheanyi

This issue depends on how gdb is being invoked. I found I needed to manually specify the source file locations when I got that error. Even though I'd already configured that under project properties. After doing so, Eclipse no longer had a problem supplying the appropriate source.

这个问题取决于 gdb 是如何被调用的。我发现当我收到该错误时,我需要手动指定源文件位置。即使我已经在项目属性下配置了它。这样做之后,Eclipse 不再有提供适当源的问题。

Using the release versus debug version of a library may be your specific problem (if you were building a library from source then debugging it). If someone is using a precompiled library, they'd never be able to set breakpoints within it and so that fix wouldn't apply to them.

使用库的发布版本与调试版本可能是您的特定问题(如果您从源代码构建库然后对其进行调试)。如果有人使用预编译库,他们将永远无法在其中设置断点,因此该修复将不适用于他们。