CygWin:最简单的 C++ 程序会导致分段错误

问题描述 投票:0回答:1

我有这个非常基本的 C++ 程序:

#include <iostream>

int main( int argc, char* argv[] )
{
    std::cout << "Hello World\n";
}

我用

g++ main.cpp -o main
编译它并用
./main
运行它,但不幸的是我得到:

$ ./main
Segmentation fault

使用

gdb
进行调试:

(gdb) run
Starting program: /cygdrive/c/Users/user/Documents/Projects/tools/test/main
[New Thread 1400.0x1aa4]
[New Thread 1400.0x86c]
[New Thread 1400.0x29d8]
[New Thread 1400.0x5ccc]

Thread 1 "main" received signal SIGSEGV, Segmentation fault.
0x00000003ef753916 in cygstdc++-6!_ZNSo6sentryC1ERSo () from /usr/bin/cygstdc++-6.dll
(gdb) backtrace
#0  0x00000003ef753916 in cygstdc++-6!_ZNSo6sentryC1ERSo () from /usr/bin/cygstdc++-6.dll
#1  0x00000003ef7d7049 in cygstdc++-6!_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l ()
   from /usr/bin/cygstdc++-6.dll
#2  0x00000003ef7e6259 in cygstdc++-6!_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc ()
   from /usr/bin/cygstdc++-6.dll
#3  0x00000001004010ad in main ()

g++
-版本:

$ g++ --version
g++ (GCC) 13.2.1 20240426
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

你们对我的 CygWin 设置出了什么问题有什么想法吗?

c++ segmentation-fault gdb cygwin
1个回答
0
投票

我相信使用:

g++ main.cpp -o main -static-libstdc++

应该可以解决这个问题。这会将 C++ 标准库静态链接到您的可执行文件中,从而可能解决任何运行时库兼容性问题。

© www.soinside.com 2019 - 2024. All rights reserved.