简体   繁体   中英

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 <<. 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:

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!

Cheers, 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. Ended up being a linker issue within borland. Better off using a different compiler like GCC/G++ or Visual Studio compiler.

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.

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:

-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
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 ):

bcc32 -tWM -oexample.obj -c example.cpp
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):

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

For your simple example, it can even be shortened to:

bcc32 -eexample.exe example.cpp

Lastly, you can pass the -tW switch multiple times. 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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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