C++中的Hello World程序出现分段错误,涉及cin和cout。

huangapple go评论85阅读模式
英文:

C++ Segmentation fault on Hello World, cin and cout

问题

我已经尝试在休息了2年后重新开始使用C++。第一天一切都很顺利。但第二天回来时,我的所有包含cout的项目都无法编译。

错误信息如下:

发生异常。分段错误

调试控制台显示:

线程1收到信号SIGSEGV,分段错误。
0x00007ffd9109f436位于std::basic_ostream<char, std::char_traits<char>>::sentry::sentry (this=this@entry=0x5ffda0, __os=...)位置:/workspace/srcdir/gcc_build/x86_64-w64-mingw32/libstdc++-v3/include/bits/ostream.tcc:51

这是出现此问题的示例代码:

#include <iostream>

using namespace std;

int main()
{
    string abc = "a";
    std::cout << "hi";

    return 0;
}

如前所述,错误发生在std::cout处,std::string正常工作。如果我使用命名空间std,然后只写cout,或者如果我不使用命名空间,而是写整个内容,结果都一样。

如果我尝试使用cin >> abc;也会出现相同的问题。

我正在使用Visual Studio Code。我不知道是否相关,但是在我的.vscode文件夹中没有找到c_cpp_properties.json文件。但是,我获得了其他文件,并且我安装了C++插件。

这是在Windows上使用g++进行的。

我已经尝试过:

  • 重新启动
  • 卸载并重新安装g++
  • 在新区域创建新文件
  • 检查我的路径以查找问题文件。
  • 搜索问题,但没有找到相关的解决方法

我希望比我聪明的人能够找出问题所在。

编辑:
我找到了问题,正如我所猜测的,它是一个libstdc++-6.dll文件:

已加载'C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll'。已加载符号。

所以,感觉相当愚蠢,但问题解决了。

英文:

I have been trying to get back into C++ after a break of 2 years. On the first day, things went fine. But when I came back the day after, none of my projects containing cout would compile.

The error is:

> Exception has occurred. Segmentation fault

The debug console gives me:

Thread 1 received signal SIGSEGV, Segmentation fault.
0x00007ffd9109f436 in std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;::sentry::sentry (this=this@entry=0x5ffda0, __os=...) at /workspace/srcdir/gcc_build/x86_64-w64-mingw32/libstdc++-v3/include/bits/ostream.tcc:51

This is an example of my code where this happens:

#include &lt;iostream&gt;

using namespace std;

int main()
{
    string abc = &quot;a&quot;;
    std::cout &lt;&lt; &quot;hi&quot;;

    return 0;
}

As mentioned, the error happens on std::cout, std::string works fine. There is no difference if I use the namespace std and then only write cout, or if I don't use the namespace and write the whole thing.

The same issue happens if I try to use cin &gt;&gt; abc;

I am using Visual Studio Code. I don't know if it is related, but I'm not getting a c_cpp_properties.json file in my .vscode folder. I am, however, getting the rest, and I do have the c++ addon.

This is on Windows using g++.

I have tried:

  • Rebooting
  • Uninstalling and reinstalling g++
  • Creating new files in new areas
  • Checking my path for problematic files.
  • Googling the issue, found nothing that seemed relevant

I hope any of you smarter than me can figure out what's wrong.

EDIT:
I found the problem, as assumed it was a libstdc++-6.dll file:

Loaded &#39;C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll&#39;. Symbols loaded.

So yeah, feel pretty stupid, but problem solved.

答案1

得分: 5

问题是加载了 Julia libstdc++-6.dll 文件,而不是 mingw64 的文件。如果你遇到类似问题,请确保彻底查看你的 PATH 变量。Visual Studio Code 调试控制台还会告诉你正在加载哪些文件。

BAD:

Loaded &#39;C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll&#39;. Symbols loaded.

GOOD:

Loaded &#39;C:\msys64\mingw64\bin\libstdc++-6.dll&#39;. Symbols loaded.
英文:

The problem was a Julia libstdc++-6.dll file that was being loaded before the mingw64 one. If you have a similar problem, make sure to comb through your PATH variables. The Visual Studio Code debug console also tells you what files are being loaded.

BAD:

Loaded &#39;C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll&#39;. Symbols loaded.

GOOD:

Loaded &#39;C:\msys64\mingw64\bin\libstdc++-6.dll&#39;. Symbols loaded.

答案2

得分: 1

这是关于VS Code可执行文件的问题。

解决Linux的方法是从终端运行/测试它。

Linux:

  1. (如果尚未安装) 在终端中使用以下命令安装g++:
    • Ubuntu/Debian: sudo apt update 然后 sudo apt install g++
    • Fedora/Red Hat: sudo yum install g++
    • Arch Linux: pacman -Syu 然后 pacman -S gcc
  2. 进入您的文件夹,然后使用以下命令编译脚本: g++ -o myfile myfile.cpp
  3. 使用 ./myfile 运行它

解决Windows的方法是从MinGW环境中运行/测试它(最简单的修复方法)。

Windows:

  1. 这里下载MSYS2。
  2. 运行安装程序。MSYS2需要64位的Windows 8.1或更新版本。
  3. 输入安装文件夹(我建议保持默认设置)。
  4. 完成后,取消选中现在运行MSY2S选项,然后点击完成(或者只需关闭窗口)。
  5. 搜索程序MSYS2 MINGW64并打开它。
  6. 现在您在MinGWLinux终端中(这是一个迷你Arch Linux)。
  7. 文件资源管理器中打开您安装的位置(默认 C:\msys64\)。
  8. 现在进入home文件夹(应该是您的用户文件夹),然后进入您的用户文件夹
  9. 这里是终端窗口的根文件夹(基本上是您的工作区)。
  10. 现在您可以创建文件/文件夹,它们可以从终端中找到。您还可以在此位置打开VS Code并在此处开发应用程序。
  11. 使用 pacman -S gcc 安装g++。
  12. 完成后,在终端中使用以下命令:g++ -o myfile myfile.cpp
  13. 然后用 ./myfile 运行它。
英文:

It's a problem with the VS Code executable.

To slove it for Linux you should run/test it from the terminal.

Linux:
&emsp; <p>

  1. (Optional if not already installed) Install g++ in the terminal using the command:<br>
    &emsp;&emsp;Ubuntu/Debian: sudo apt update then sudo apt install g++<br>
    &emsp;&emsp;Fedora/Red Hat: sudo yum install g++<br>
    &emsp;&emsp;Arch Linux: pacman -Syu then pacman -S gcc<br>
  2. Go into your folder and then compile the script using: g++ -o myfile myfile.cpp
  3. Run it using ./myfile
    </p>

To slove it for Windows you should run/test it from a MinGW enviorment (easiest fix).

Windows:
&emsp; <p>

  1. Download MSYS2 from [here](https://github.com/msys2/msys2-installer/releases/download/2023-05-26/msys2-x86_64-20230526.exe "MSYS2 Download Link").
  2. Run the installer. MSYS2 requires 64 bit Windows 8.1 or newer.
  3. Enter the Installation Folder (I recommend leaving it default).
  4. When done, uncheck option Run MSY2S now, and then click Finsh (or just close the window).
  5. Search the program MSYS2 MINGW64 and open it.
  6. Now you are in Linux terminal for MinGW (this is a mini Arch Linux).
  7. Open in File Explorer the location where you installed it (default C:\msys64\)
  8. Now enter in the home folder (there should be your user's folder) then enter in your user's folder.
  9. Here is the root folder of the terminal window (basically your workspace).
  10. Now you can make your files/folders and they can be found from the terminal. Also you can open VS Code in this location and develop your apps here.
  11. Install g++ using pacman -S gcc
  12. After you are done use the command in the terminal g++ -o myfile myfile.cpp
  13. And run it with ./myfile
    </p>

Hope that I helped you!

Edit: didn't see you solved it. I was writing it while you solved the problem.

huangapple
  • 本文由 发表于 2023年7月7日 03:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定