运行时字符串错误C ++

时间:2013-11-07 08:42:25

标签: c++ gcc compilation compiler-errors

我有这样的代码

#include <iostream>
#include <string>

using namespace std;

void Test(){
    string line;
}

int main(){
    cout << "test " << endl;
    return 0;
}

代码是可编译的但是当我尝试运行它时,程序停止工作。然后使用gdb来发现我的程序有什么问题

 (gdb) run
 Starting program: E:\CPP\Program dinamis\a.exe
 [New Thread 4892.0x1d4c]
 test
 Program received signal SIGILL, Illegal instruction.
 0x6fcc43c3 in libstdc++-6!_ZSt4cout () from C:\MinGw\bin\libstdc++-6.dll

我不明白它有什么问题。我通过输入g++ -v

使用MinGW(G ++)作为我的编译器
 E:\CPP\Program dinamis>g++ -v
 Using built-in specs.
 COLLECT_GCC=g++
 COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe
 Target: mingw32
 Configured with: ../gcc-4.6.2/configure --enable-
 languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 
 --enable-shared --enable-libgo
 mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-  
 runtime-libs --build=mingw32 --prefix=/mingw
 Thread model: win32
 gcc version 4.6.2 (GCC)

但如果我在我的visual studio上编写相同的代码,程序运行良好且没有错误。我该怎么办我的G ++

3 个答案:

答案 0 :(得分:0)

您实际上并没有使用测试函数进行输出,您可以按如下方式使用它,但不确定为什么会出现任何错误,因为在GNU GCC版本4.8.1下运行正常

以下是使用Test()

的代码示例
#include <iostream>
#include <string>

using namespace std;

string Test(){
    string line = "My text Line here!";
    return line;
}

string Test2(){
    return "My text Line2 here!";
}

int main(){
    cout << Test() << endl;
    cout << Test2() << endl;
    return 0;
}

第二个测试用于说明返回字符串的简单用法,注意,函数也需要定义为返回字符串。

答案 1 :(得分:0)

最后我找到了解决方案。我在Windows 8操作系统上运行,似乎主要问题是在编译源代码时链接过程。

所以,假设文件名为mycode.cpp,我只需运行带有-static-libgcc -static-libstdc++选项的命令,如下所示:

 g++ "mycode.cpp" -o mycode -static-libgcc -static-libstdc++

答案 2 :(得分:-1)

只是盲目拍摄,尝试处理主要功能参数:

int main(int, char**)
{
//...

也许这是堆栈的东西......?

相关问题