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参数下没有错误?谢谢!
在不同的线程里使用了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参数下没有错误?谢谢!
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
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_library_support_for_multithreading.asp感谢回复
对于问题2,我问的是:
/MT参数和在project->setting->link->input里加入libcmt.lib的作用有和不同?
^^^而不是/ML