Reputation: 295
Advice is all over the Internet that for multithreaded Win32 apps – at least for MS Visual Studio – you have to link with the multithreaded C runtime library instead of the single-threaded one (so LIBCMT.LIB or LIBCPMT.LIB instead of LIBC.LIB or LIBCP.LIB). I’m currently using Code::Blocks with the GCC Compiler, and I’m not sure if the above advice is even applicable, or how to change those link settings if it is. I can’t find any of the above files listed anywhere in the build settings in the IDE. If I call the “_beginthread” function in a simple test program, it seems to compile and run without any problem, but I’m not sure that proves anything. I can call the “printf” function from the new thread, and the output appears in the main console window. I don’t know if that’s correct behaviour or not.
Any help would be much appreciated.
EDIT:
Thanks Tim for your response. I don’t know where to find the compiler flags in this IDE – that’s part of the problem. The other part is I don’t know what to change when I do find them. But here’s my simple test program – I hope it’s helpful:
#include <windows.h>
#include <process.h>
#include <stdio.h>
HANDLE ThreadHandle;
void RunThreadFunc(void *This) {
printf("%s", "Squeak.\n\n");
_endthread();
}
int main () {
Sleep(2000);
ThreadHandle = (HANDLE) _beginthread(RunThreadFunc, 0, NULL);
//printf("%s", "Woo.\n\n");
Sleep(2000);
return 0;
}
Upvotes: 1
Views: 1057
Reputation: 39591
The version of GCC shipped with Code::Blocks is TDM-GCC. This version of GCC uses the MinGW C runtime library, which is almost entirely built around the DLL version of Visual Studio 6 multithreaded C runtime, MSVCRT.DLL. (Note that MSVCRT.DLL is now considered part of the Windows operating system.) There's no static library version of the MinGW runtime, whether multithreaded or not, so there is no equivalent to either LIBCMT.LIB or LIBC.LIB. It's the equivalent to linking with with MSVCRT.LIB.
On the other hand, GCC uses it's own C++ runtime library. The TDM-GCC release provides two versions of it, a static multithreaded version and a DLL multithreaded version.
So the advice you heard about using the multithreaded runtime libraries with Visual Studio doesn't apply. You don't have a choice, your GCC compiled projects will always be linked with the multithreaded runtimes. (Note that this is also true for modern versions of Visual Studio, they dropped support for the single threaded runtimes with Visual Studio 2005.)
Upvotes: 3