简体   繁体   English

在Code :: Blocks IDE中为GCC链接多线程运行时

[英]Linking multithreaded runtimes for GCC in Code::Blocks IDE

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). 对于Internet上的多线程Win32应用程序(至少对于MS Visual Studio)建议是,必须与多线程C运行时库而不是单线程库(例如LIBCMT.LIB或LIBCPMT.LIB而不是LIBC.LIB)链接。或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. 我目前在GCC编译器中使用Code :: Blocks,并且不确定上述建议是否适用,或者不确定如何更改这些链接设置。 I can't find any of the above files listed anywhere in the build settings in the IDE. 我在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. 如果我在一个简单的测试程序中调用“ _beginthread”函数,它似乎可以编译并运行而没有任何问题,但是我不确定这是否能证明任何事情。 I can call the “printf” function from the new thread, and the output appears in the main console window. 我可以从新线程中调用“ printf”函数,并且输出出现在主控制台窗口中。 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. 我不知道在此IDE中的哪里可以找到编译器标志-这就是问题的一部分。 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;
}

The version of GCC shipped with Code::Blocks is TDM-GCC. Code :: Blocks附带的GCC版本是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. 此版本的GCC使用MinGW C运行时库,该库几乎完全围绕Visual Studio 6多线程C运行时的DLL版本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. (请注意,MSVCRT.DLL现在被视为Windows操作系统的一部分。)MinGW运行时没有静态库版本,无论是否是多线程的,因此没有等效于LIBCMT.LIB或LIBC.LIB的版本。 It's the equivalent to linking with with MSVCRT.LIB. 等效于与MSVCRT.LIB链接。

On the other hand, GCC uses it's own C++ runtime library. 另一方面,GCC使用它自己的C ++运行时库。 The TDM-GCC release provides two versions of it, a static multithreaded version and a DLL multithreaded version. TDM-GCC发行版提供了两个版本,一个是静态多线程版本,另一个是DLL多线程版本。

So the advice you heard about using the multithreaded runtime libraries with Visual Studio doesn't apply. 因此,您听到的关于将多线程运行时库与Visual Studio一起使用的建议不适用。 You don't have a choice, your GCC compiled projects will always be linked with the multithreaded runtimes. 您别无选择,您的GCC编译项目将始终与多线程运行时链接。 (Note that this is also true for modern versions of Visual Studio, they dropped support for the single threaded runtimes with Visual Studio 2005.) (请注意,这对于现代版本的Visual Studio也是如此,因为他们放弃了对Visual Studio 2005中的单线程运行时的支持。)

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

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