英文:
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<char, std::char_traits<char> >::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 <iostream>
using namespace std;
int main()
{
string abc = "a";
std::cout << "hi";
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 >> 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 'C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll'. Symbols loaded.
So yeah, feel pretty stupid, but problem solved.
答案1
得分: 5
问题是加载了 Julia libstdc++-6.dll 文件,而不是 mingw64 的文件。如果你遇到类似问题,请确保彻底查看你的 PATH 变量。Visual Studio Code 调试控制台还会告诉你正在加载哪些文件。
BAD:
Loaded 'C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll'. Symbols loaded.
GOOD:
Loaded 'C:\msys64\mingw64\bin\libstdc++-6.dll'. 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 'C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll'. Symbols loaded.
GOOD:
Loaded 'C:\msys64\mingw64\bin\libstdc++-6.dll'. Symbols loaded.
答案2
得分: 1
这是关于VS Code可执行文件的问题。
解决Linux的方法是从终端运行/测试它。
Linux:
- (如果尚未安装) 在终端中使用以下命令安装g++:
- Ubuntu/Debian:
sudo apt update
然后sudo apt install g++
- Fedora/Red Hat:
sudo yum install g++
- Arch Linux:
pacman -Syu
然后pacman -S gcc
- Ubuntu/Debian:
- 进入您的文件夹,然后使用以下命令编译脚本:
g++ -o myfile myfile.cpp
- 使用
./myfile
运行它
解决Windows的方法是从MinGW环境中运行/测试它(最简单的修复方法)。
Windows:
- 从这里下载MSYS2。
- 运行安装程序。MSYS2需要64位的Windows 8.1或更新版本。
- 输入安装文件夹(我建议保持默认设置)。
- 完成后,取消选中现在运行MSY2S选项,然后点击完成(或者只需关闭窗口)。
- 搜索程序MSYS2 MINGW64并打开它。
- 现在您在MinGW的Linux终端中(这是一个迷你Arch Linux)。
- 在文件资源管理器中打开您安装的位置(默认
C:\msys64\
)。 - 现在进入
home
文件夹(应该是您的用户文件夹),然后进入您的用户文件夹。 - 这里是终端窗口的根文件夹(基本上是您的工作区)。
- 现在您可以创建文件/文件夹,它们可以从终端中找到。您还可以在此位置打开VS Code并在此处开发应用程序。
- 使用
pacman -S gcc
安装g++。 - 完成后,在终端中使用以下命令:
g++ -o myfile myfile.cpp
- 然后用
./myfile
运行它。
英文:
It's a problem with the VS Code executable.
To slove it for Linux you should run/test it from the terminal.
Linux:
  <p>
- (Optional if not already installed) Install g++ in the terminal using the command:<br>
  Ubuntu/Debian:sudo apt update
thensudo apt install g++
<br>
  Fedora/Red Hat:sudo yum install g++
<br>
  Arch Linux:pacman -Syu
thenpacman -S gcc
<br> - Go into your folder and then compile the script using:
g++ -o myfile myfile.cpp
- Run it using
./myfile
</p>
To slove it for Windows you should run/test it from a MinGW enviorment (easiest fix).
Windows:
  <p>
- Download MSYS2 from [here](https://github.com/msys2/msys2-installer/releases/download/2023-05-26/msys2-x86_64-20230526.exe "MSYS2 Download Link").
- Run the installer. MSYS2 requires 64 bit Windows 8.1 or newer.
- Enter the Installation Folder (I recommend leaving it default).
- When done, uncheck option Run MSY2S now, and then click Finsh (or just close the window).
- Search the program MSYS2 MINGW64 and open it.
- Now you are in Linux terminal for MinGW (this is a mini Arch Linux).
- Open in File Explorer the location where you installed it (default
C:\msys64\
) - Now enter in the
home
folder (there should be your user's folder) then enter in your user's folder. - Here is the root folder of the terminal window (basically your workspace).
- 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.
- Install g++ using
pacman -S gcc
- After you are done use the command in the terminal
g++ -o myfile myfile.cpp
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论