1、多线程是否一定用/MT或/MTd参数编译?
在不同的线程里使用了fopen, fprintf函数,用单线程编译连接总出现文件不能打开的问题,并且各个线程里打开读写的文件各不相同。在project->setting->link->input里加入libcmt.lib文件后,问题解决,但是编译时出现警告:
LINK : warning LNK4098: defaultlib "LIBCD" conflicts with use of other libs; use /NODEFAULTLIB:library2、/MT参数和在project->setting->link->input里加入libcmt.lib的作用有和不同?3、加了/MT或/MTd后编译不能通过?
在/ML参数编译通过的代码在什么情况下在/MT下不能通过了?
代码:xxxx.h...
ERRORCODE_FATAL_BEGIN(ERR_FATAL)   //     <-出错行!!!!!
...yyyy.cpp
...
#define ERRORCODE_FATAL_BEGIN(name) { name, "First Fatal Error Message"},
...错误:
error C2440: 'initializing' : cannot convert from 'enum error_codes_enum' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast在/ML和/MLd参数下没有错误?谢谢!

解决方案 »

  1.   

    1、是必须的,这决定程序连接 libcmt(d).lib 还是 libc(d).lib ,这两个(加上 debug 版是四个)分别是 C run-time library 的多线程和单线程版本,强制连结 libcmt.lib 的警告就是因为默认的 libcd.lib 与 libcmt.lib 的同名输出符号冲突。 2、强制加入 libcmt.lib 后,因为已存在的 /ML(d) 参数会自动连结 libc(d).lib , 两者实际是同一库的不同版本,导致同名输出符号冲突。3、/MT 编译选项会定义宏 _MT ,可以利用 #ifdef _MT 消除错误
      

  2.   

    在工程-》设置里的C++项选code generation,在下面的选项选debug mutithreaded
      

  3.   

    对于问题3,我的现象是:设置了单线程和设置了多线程(在工程-》设置里的C++项选code generation,在下面的选项选debug mutithreaded)两种情况下,编译一个不出错(单线程),另一个出错(多线程),这段代码我是要的。昨天又在网上查了一下,一下是MSDN中对于多线程的的一些解释,希望对遇到和我一样问题的朋友有用:Visual C++ Concepts: Adding Functionality   Library Support for Multithreading
    If one thread is suspended by the Win32 scheduler while executing the printf function, one of the program's other threads might start executing. If the second thread also calls printf, data might be corrupted. To avoid this situation, access to static data used by the function must be restricted to one thread at a time. You do not need to serialize access to stack-based (automatic) variables because each thread has a different stack. Therefore, a function that uses only automatic (stack) variables is reentrant. The standard C run-time libraries, such as LIBC, have a limited number of reentrant functions. A multithread program needing to use C run-time library functions that are normally not reentrant should be built with the multithread library LIBCMT.LIB.1. The Multithread C Libraries: LIBCMT.LIB and MSVCRT.LIBThe support library LIBCMT.LIB is a reentrant library for creating multithread programs. The MSVCRT.LIB library, which calls code in the shared MSVCRT70.DLL, is also reentrant. When your application calls functions in these libraries, the following rules may apply: 1a. All library calls must use the C (__cdecl) calling convention; programs compiled using other calling conventions (such as __fastcall or __stdcall) must use the standard include files for the run-time library functions they call. 1b. Variables passed to library functions must be passed by value or cast to a pointer. Programs built with LIBCMT.LIB do not share C run-time library code or data with any dynamic-link libraries they call.2. Alternatives to LIBCMT.LIB and MSVCRT.LIBIf you build a multithread program without using LIBCMT.LIB, you must do the following: 2a. Use the standard C libraries and limit library calls to the set of reentrant functions. 2b. Use the Win32 API thread management functions, such as CreateThread. 23. Provide your own synchronization for functions that are not reentrant by using Win32 services such as semaphores and the EnterCriticalSection and LeaveCriticalSection functions. 
    Warning   The multithread library LIBCMT.LIB includes the _beginthread and _endthread functions. The _beginthread function performs initialization without which many C run-time functions will fail. You must use _beginthread instead of CreateThread in C programs built with LIBCMT.LIB if you intend to call C run-time functions.3. The Multithread Libraries Compile OptionTo build a multithread application that uses the C run-time libraries, you must tell the compiler to use a special version of the libraries (LIBCMT.LIB). To select these libraries, first open the project's Property Pages dialog box (View menu) and click the C/C++ folder. Select the Code Generation page. From the Runtime Library drop-down box, select Multi-threaded. Click OK to return to editing.From the command line, the Multithread Library compiler option (/MT) is the best way to build a multithread program with LIBCMT.LIB. This option, which is automatically set when you specify a multithreaded application when creating a new project, embeds the LIBCMT library name in the object file.See Also
    Multithreading with C and Win32
      

  4.   

    原文在:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_library_support_for_multithreading.asp感谢回复
      

  5.   

    In355Hz:
    对于问题2,我问的是:
    /MT参数和在project->setting->link->input里加入libcmt.lib的作用有和不同?
    ^^^而不是/ML