1.如果你将gnu的代码拿到vs命令行里面编译,经常会遇到很多错误,其中一些错误时因为gnu的很多代码是用c写的,而vs中c语法和gnu的c语法有很多差别,例如gnu里面的c中可以定义变量和调用函数初始化这个变量一块做;gnu里面会出现__inline__等。这时,你可以对cl.exe加上/TP参数就可以避免很多错误。更多cl的参数可以参考http://hi.baidu.com/liugang585570/blog/item/1ef2820e68a407206159f34c.html。
2。当你在编译时出现WinSock2.h和WinSock.h冲突时,可以定义宏WIN32_LEAN_AND_MEAN来解决这个问题(参考http://www.codeguru.com/forum/archive/index.php/t-286587.html)。当你的工程中包含windows ddk的头文件时可能会出现这种情况。
c:/Program Files/Microsoft Visual Studio .NET/Vc7 /PlatformSDK/Include/WinSock2.h(153): error C2011: 'timeval' : 'struct' type redefinition
3。当出现很多系统函数,例如CreateWindow,在链接的时候找不到时,可以先检查一下有没有包含kernel32.lib和user32.lib,如果有包含,就需要检查一下有没有定义_MSC_VER这个宏,例如可以定义在cl.exe的参数里面加上/D”_MSC_VER=800”。
dxut.obj : error LNK2019: unresolved external symbol __imp__SystemParametersInfoW@16 referenced in function "long __stdcall DXUTInit(bool,bool)" (?DXUTInit@@YGJ_N0@Z)
dxutgui.obj : error LNK2001: unresolved external symbol __imp__SystemParametersInfoW@16
dxut.obj : error LNK2019: unresolved external symbol __imp__CommandLineToArgvW@8 referenced in function "void __cdecl DXUTParseCommandLine(void)" (?DXUTParseCommandLine@@YAXXZ)
dxut.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExW@48 referenced in function "long __stdcall DXUTCreateWindow(wchar_t const *,struct HINSTANCE__ *,struct HICON__ *,struct HMENU__ *,int,int)" (?DXUTCreateWindow@@YGJPB_WPAUHINSTANCE__@@PAUHICON__@@PAUHMENU__@@HH@Z)
dxut.obj : error LNK2019: unresolved external symbol __imp__AdjustWindowRect@12 referenced in function "long __stdcall DXUTCreateWindow(wchar_t const *,struct HINSTANCE__ *,struct HICON__ *,struct HMENU__ *,int,int)" (?DXUTCreateWindow@@YGJPB_WPAUHINSTANCE__@@PAUHICON__@@PAUHMENU__@@HH@Z)
dxut.obj : error LNK2019: unresolved external symbol __imp__SetRect@20 referenced in function "long __stdcall DXUTCreateWindow(wchar_t const *,struct HINSTANCE__ *,struct HICON__ *,struct HMENU__ *,int,int)" (?DXUTCreateWindow@@YGJPB_WPAUHINSTANCE__@@PAUHICON__@@PAUHMENU__@@HH@Z)
DXUTcamera.obj : error LNK2001: unresolved external symbol __imp__SetRect@20
dxutgui.obj : error LNK2001: unresolved external symbol __imp__SetRect@20
misc.obj : error LNK2001: unresolved external symbol __imp__SetRect@20
4.visual studio的crt的链接:除了在cl.exe的参数里面要加上/MT这类的选项后,最好加上宏定义(如下表的最后一列)。
http://msdn.microsoft.com/en-us/library/abx4dbyh(VS.80).aspx
C run-time library(without iostreamor standard C++ library) |
Associated DLL | Characteristics | Option | Preprocessor directives |
---|---|---|---|---|
libcmt.lib |
None, static link. |
Multithreaded, static link |
_MT |
|
msvcrt.lib |
msvcr80.dll |
Multithreaded, dynamic link (import library for MSVCR80.DLL). Be aware that if you use the Standard C++ Library, your program will need MSVCP80.DLL to run. |
_MT, _DLL |
|
libcmtd.lib |
None, static link |
Multithreaded, static link (debug) |
/MTd |
_DEBUG, _MT |
msvcrtd.lib |
msvcr80d.dll |
Multithreaded, dynamic link (import library for MSVCR80D.DLL) (debug). |
/MDd |
_DEBUG, _MT, _DLL |
msvcmrt.lib |
msvcm80.dll |
C Runtime import library. Used for mixed managed/native code. |
/clr |
|
msvcurt.lib |
msvcm80.dll |
C Runtime import library compiled as 100% pure MSIL code. All code complies with the ECMA URT spec for MSIL. |
/clr:pure |
|
5. 在编com程序时,出现找不到函数定义是可以看看有没有包含comutil.h和comdef.h。出现链接错误时,可以看看有没有包含ole32.lib。如果出现下面的错误:
CMsOutlookHelper.obj : error LNK2019: unresolved external symbol "void __stdcall
_com_issue_error(long)" (?_com_issue_error@@YGXJ@Z) referenced in function "pub
lic: __thiscall _com_ptr_t<class _com_IIID<struct _Application,&struct __s_GUID
const _GUID_00063001_0000_0000_c000_000000000046> >::_com_ptr_t<class _com_IIID<
则需要包含comsupp.lib(也可以尝试comsuppw.lib或comsuppd.lib),如果包含comsupp.lib出现了下面的错误:
comsupp.lib(comsupp.obj) : error LNK2019: unresolved external symbol __imp__GetE
rrorInfo@8 referenced in function "void __stdcall _com_issue_errorex(long,struct
IUnknown *,struct _GUID const &)" (?_com_issue_errorex@@YGXJPAUIUnknown@@ABU_GU
ID@@@Z)
comsupp.lib(invkprxy.obj) : error LNK2019: unresolved external symbol __imp__Var
iantChangeType@16 referenced in function "long __cdecl _com_invoke_helper(struct
IDispatch *,long,unsigned short,unsigned short,void *,unsigned short const *,ch
ar *,struct IErrorInfo * *)" (?_com_invoke_helper@@YAJPAUIDispatch@@JGGPAXPBGPAD
PAPAUIErrorInfo@@@Z)
则需要包含Oleaut32.lib。至于为什么要包含Oleaut32.lib,是因为在msdn里面可以查到GetErrorInfo在Oleaut32.lib里面。有时候msdn查msdn的时候找不到这个函数在那个lib的时候,可以看看这个函数在wince里面对应于那个库。
6. 在链接出现问题时,可以给link.exe加上/verbose:lib选项来看详细的链接过程。verbose的详细解释在http://msdn.microsoft.com/zh-cn/library/wdsk6as6(VS.80).aspx。verbose的打印消息大致如下:
Starting pass 1
Processed /DEFAULTLIB:uuid.lib
Processed /DEFAULTLIB:comsupp.lib
Processed /DEFAULTLIB:user32.lib
Processed /DEFAULTLIB:ole32.lib
Processed /DEFAULTLIB:oleaut32.lib
Processed /DEFAULTLIB:LIBCMTD
Processed /DEFAULTLIB:OLDNAMES
Searching libraries
Searching C:/Program Files/Microsoft Visual Studio 8/VC/LIB
/comsupp.lib:
Found "void __stdcall _com_issue_error(long)" (?
_com_issue_error@@YGXJ@Z)
Referenced in command.obj
Loaded comsupp.lib(comsupp.obj)
Found "void __stdcall _com_raise_error(long,struct IErrorInfo
*)" (?_com_raise_error@@YGXJPAUIErrorInfo@@@Z)
Referenced in comsupp.lib(comsupp.obj)
Loaded comsupp.lib(comraise.obj)