简体   繁体   English

使用cout <打印字符串时CodeBlocks中的C ++运行时错误

[英]C++ runtime errors in CodeBlocks when printing strings with cout <<

I recently started using CodeBlocks and began encountering odd runtime errors which I have traced back to printing strings using cout <<. 我最近开始使用CodeBlocks并开始遇到奇怪的运行时错误,我已经追溯到使用cout <<打印字符串。 For example, even the following.. 例如,甚至以下..

#include <string>
#include <iostream>


int main()
{
    std::string str;
    str = "Hi!";
    std::cout << str << std::endl;
    return 0;
}

results in an error. 导致错误。 It will compile fine (using Borland) but when I run it I get a pop up window saying 'test.exe has stopped working' and in the console I get the message: 它会编译好(使用Borland),但是当我运行它时,我得到一个弹出窗口,说“test.exe已经停止工作”,在控制台中我收到消息:

Process returned -1073741819 (0xC0000005)   execution time : 1.526 s
Press any key to continue.

It compiles and runs fine in MS Visual C++ and with G++ in Ubuntu.. any thoughts would be greatly appreciated! 它在MS Visual C ++中编译并运行良好,在Ubuntu中使用G ++。任何想法都将非常感谢!

Cheers, Weatherwax 干杯,Weatherwax

My one-off comment ended up helping solve the problem so here it is packaged up as an answer for future users: 我的一次性评论最终帮助解决了问题所以在这里它被打包成未来用户的答案:

This guy had a similar issue and it ended up being a linker issue which he fixed. 家伙有一个类似的问题,最终成为他修复的链接器问题。 The fix is the last post in the thread, although reading the whole thread could be useful for you. 修复是线程中的最后一个帖子,虽然读取整个线程可能对您有用。

Long Story short: Borland compiler is a bit dated and annoying to use. 长话短说:Borland编译器有点过时且使用起来很烦人。 Ended up being a linker issue within borland. 结束了作为borland内部的链接器问题。 Better off using a different compiler like GCC/G++ or Visual Studio compiler. 最好使用不同的编译器,如GCC / G ++或Visual Studio编译器。

This answer is here to elaborate on the root cause of the issue. 这个答案是为了详细说明问题的根本原因。

The reason for your crashing program is because the wrong runtime library is being linked. 崩溃程序的原因是因为链接了错误的运行时库。 Specifically, your example is compiled as a single threaded object file(the default) but the linking step is using the multithreaded cw32mt.lib runtime -- the "mt" suffix at the end means multithreaded. 具体来说,您的示例编译为单线程对象文件(默认),但链接步骤使用多线程 cw32mt.lib运行时 - cw32mt.lib的“mt”后缀表示多线程。

The solution is to make sure the runtime your program is compiled to use matches with the runtime you're linking against. 解决方案是确保编译程序的运行时使用与您链接的运行时匹配。 A few ways to do this. 有几种方法可以做到这一点。

Important bcc32 compile switches: 重要的bcc32编译开关:

-tW    Windows GUI program. WinMain() is expected
-tWC   Windows Console program. main() is expected. default.
-tWR   Use dynamically linked runtime. Absence implies static runtime linkage.
-tWM   Use multithreaded runtime. Absence implies single thread.

Compiling your example program as single threaded like this works: 将您的示例程序编译为单线程,如下所示:

bcc32 -oexample.obj -c example.cpp bcc32 -oexample.obj -c example.cpp
ilink32 -ap example.obj c0x32, example.exe,, cw32.lib import32.lib,, ilink32 -ap example.obj c0x32,example.exe ,, cw32.lib import32.lib ,,

or you can compile it as multithreaded like this(note the -tWM switch matching up with cw32mt.lib ): 或者你可以像这样编译多线程(注意-tWM开关与cw32mt.lib匹配):

bcc32 -tWM -oexample.obj -c example.cpp bcc32 -tWM -oexample.obj -c example.cpp
ilink32 -ap example.obj c0x32, example.exe,, cw32mt.lib import32.lib,, ilink32 -ap example.obj c0x32,example.exe ,, cw32mt.lib import32.lib ,,

A third approach that is easier and less error prone is to not call the linker yourself. 第三种方法更容易,更容易出错,就是不要自己调用链接器。 Instead, let the compiler drive the linker indirectly(similar to gcc): 相反,让编译器间接驱动链接器(类似于gcc):

bcc32 -tWM -oexample.obj -c example.cpp bcc32 -tWM -oexample.obj -c example.cpp
bcc32 -tWM example.obj -eexample.exe bcc32 -tWM example.obj -eexample.exe

For your simple example, it can even be shortened to: 对于您的简单示例,它甚至可以缩短为:

bcc32 -eexample.exe example.cpp bcc32 -eexample.exe example.cpp

Lastly, you can pass the -tW switch multiple times. 最后,您可以多次传递-tW开关。 For example, this command compiles your example as a console program with multithread support and dynamic runtime linkage: 例如,此命令将您的示例编译为具有多线程支持和动态运行时链接的控制台程序:

bcc32 -tWM -tWR -tWC -eexample.exe example.cpp bcc32 -tWM -tWR -tWC -eexample.exe example.cpp

The produced example.exe executable is much smaller and its import table has an entry for CC3250MT.DLL confirming that the borland runtime is dynamically linked. 生成的example.exe可执行文件要小得多,其导入表有一个CC3250MT.DLL条目,确认borland运行时是动态链接的。

We should not assume that a non-functioning program is caused by nonconformity to the standard or a bug in the tool we're using without first investigating user error as a potential cause (even though in this case it's tempting to do so). 我们不应该假设一个不起作用的程序是由不符合标准或我们正在使用的工具中的错误引起的, 而没有首先将用户错误作为潜在原因进行调查(即使在这种情况下它很有诱惑力)。 In the OP's case, the code::block IDE didn't have the right commands setup for the toolchain being used. 在OP的情况下,code :: block IDE没有为正在使用的工具链设置正确的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM