ºìÁªLinuxÃÅ»§
Linux°ïÖú

WindowsÓëLinux±àÒëÆ÷µÄ²îÒì

·¢²¼Ê±¼ä:2007-04-17 00:51:21À´Ô´:ºìÁª×÷Õß:Message
¿ªÊ¼ÒÆÖ² ¹¤×÷ºóµÄµÚÒ»²½¾ÍÊÇÔÚÄ¿±êƽ̨LinuxÉϽøÐбàÒ룬²¢Á´½ÓÔ´´úÂë¡£ÓÉÓÚÐèÒªÒÆÖ²µÄÈí¼þͨ³£²¢Î´ÔÚLinuxƽ̨ÉϱàÒë¹ý£¬±àÒëµÄ¹ý³Ì¿ÉÄÜ»áÓöµ½ºÜ´óµÄÀ§ ÄÑ¡£Ò»°ãÇé¿öÏ£¬ÓÉÀàÐÍÉùÃ÷ÒýÆðµÄ±àÒë´íÎóÊDZȽÏÈÝÒ×ÐÞ¸´µÄ¡£±ÈÈçMicrosoft C/C++µÄÍ·ÎļþʹÓÃ__declspec( dllimport/dllexport )À´ÊäÈëºÍÊä³öDLLº¯Êý£¬ÔÚLinuxÉÏ£¬°Ñº¯ÊýÉùÃ÷³Éextern ¡°C¡±£¬»òÕßÔÙ½áºÏʹÓÃDEFÎļþ£¬Ê¹ÓÃÏàÓ¦µÄÁ´½ÓÃüÁî¾Í¿ÉÒÔ½â¾öÕâЩÎÊÌâ¡£µ«À§Äѵĵط½ÔÚÓÚ±àÒëÆ÷Ö®¼ä´æÔÚ²îÒìµÄ²¿·Ö£¬Í¬Ê±ÕâÒ²ÊÇ¿ÉÄÜÒýÆðºÜ¶àÔËÐÐʱÎÊ ÌâµÄÖØÒªÒòËØ£¬¶ÁÕßÓбØÒªÔÚ¿ªÊ¼ÒÆֲ֮ǰ¾Í³ä·ÖÁ˽⡣±¾½Ú²»ÄܸÅÀ¨ËùÓвî±ð£¬Ö»½²ÊöһЩÈÝÒ×±»ºöÂÔ¶øÇÒºó¹û±È½ÏÑÏÖصķ½Ãæ¡£

ÒÔVisual C++ 2003ºÍGCC 4.1.0ΪÀý¡£Ç°ÕßÊÇWindowsƽ̨µÄÖ÷Á÷±àÒëÆ÷£¬¼æÈÝÐÔÁ¼ºÃ£¬µ«ÊǶÔC++±ê×¼µÄ×ñÑ­²¢²»Ñϸñ¡£ÕâÒâζ׿´Ê¹¿ª·¢Õßд³ö²»Ì«·ûºÏ±ê×¼µÄ³ÌÐò£¬±àÒë Æ÷Ò²¿ÉÄÜÄÜÈÝÈÌ¡£Ïà·´µÄÊÇ£¬GCC¶Ô±ê×¼µÄ×ñÑ­Ïà¶ÔÑϸñµÃ¶à£¬ÕâÑùºÜÈÝÒ×Ôì³ÉÔÚWindowsÔËÐÐÁ¼ºÃµÄ³ÌÐò£¬ÔÚLinuxÉÏÈ´ÒýÆðÒâÏë²»µ½µÄ±àÒëÉõÖÁÔË ÐÐʱ´íÎó¡£

£¨1£©»ù±¾ÀàÐÍ´óСºÍ½á¹¹¶ÔÆë

Ê×ÏÈÊÇ C/C++ÓïÑÔ»ù±¾ÀàÐ͵ĴóС£¬ÒÔ¼°ÏàÓ¦µÄ½á¹¹¶ÔÆëÎÊÌâ¡£µäÐ͵ÄÀý×ÓÊÇlong¹Ø¼ü×Ö¡£ÔÚVisual C++ 2003Ï£¬sizeof(long double)ÊÇ8£¬Æä´óСºÍdoubleÒ»Ö¡£µ«ÊÇÔÚGCC 4.1.0ÉÏ£¬sizeof(long double)µÈÓÚ12£¬Æä´óС±Èdouble¶à4¡£ÁíÒ»¸öºÍ´óСÏà¹ØµÄÎÊÌâÊǶÔÆëÎÊÌâ¡£²»Í¬±àÒëÆ÷µÄĬÈ϶ÔÆë´óСÊDz»Ò»ÑùµÄ¡£Ò»°ãÇé¿öϳÌÐòÂß¼­¶¼¸ú¶Ô ÆëÎ޹أ¬µ«ÊÇÉæ¼°´Ó´ÅÅÌ»òÕßÍøÂçÎļþÖжÁÈ¡½á¹¹Ê±£¨Èç½âÎö×ÊÔ´£©£¬¾«È·µÄ¶ÔÆë¾ÍÊDZØÐèµÄ¡£¿¼²ìÏÂÃæµÄ³ÌÐò¶Î£º

#include

struct A

{

char a;

double b;

};

int main()

{

printf("%d %d %d\n", sizeof(long double),

sizeof(long long), sizeof(A) );

return 0;

}

ÉÏÃæÕâ¶Î³ÌÐòÔÚ Visual C++ 2003±àÒëÆ÷ĬÈÏÉèÖÃÏ£¬Êä³ö½á¹ûΪ8 8 16£»ÔÚGCC 4.1.0±àÒëÆ÷ĬÈÏÉèÖÃÏ£¬ÆäÊä³öΪ12 8 12¡£´Ósizeof(A)µÄ´óС¿ÉÒÔ¿´³ö£¬Visual C++ 2003ÊÇ°´8×Ö½Ú¶ÔÆëµÄ£¬¶øgccÊÇ4×Ö½Ú¶ÔÆëµÄ¡£ÕâʱÐèҪʹÓÃ#pragma packÔ¤±àÒëÖ¸ÁîÀ´ÐÞ¸ÄÍ·ÎļþÖеĽṹÉùÃ÷£¬»òÕßÔÚÔËÐÐʱµ÷ÕûÄÚ´æÖнṹ³ÉÔ±µÄλÖᣲ»¹Ü²ÉÓúÎÖÖ·½·¨£¬¶ÔÆ붼ÊÇÐèҪСÐÄ´¦ÀíµÄÊÂÇé¡£

Ò»¸öÒýÆð ×î´óÂé·³µÄ»ù±¾ÀàÐÍÊÇwchar_t¡£ÔÚVisual C/C++ 2003±àÒëÆ÷ÖУ¬wchar_tµÄ´óСÊÇ2×Ö½Ú£¬²¢ÇÒ¿ÉÒÔºÍunsigned shortÀàÐÍ»¥Ïำֵ¡£Óë´Ë¹ØÁªµÄһϵÁÐUnicodeÏà¹Øº¯Êý£¬±ÈÈçwcslen,wcscmpµÈ£¬¶¼½ÓÊÜUTF16¸ñʽµÄUnicode´®¡£ÔÚ GCCÖУ¬Æä´óСÊÇ32λ¡£Óë´ËÏà¹ØµÄwcslen,wcscmpº¯Êý¶¼½ÓÊÜUTF32¸ñʽµÄUnicode´®¡£Îª´Ë£¬±ØÐëÔÚLinuxÉÏ¿ª·¢Ò»Ì× UTF16½Ó¿ÚµÄwcsϵÁк¯Êý£¬ÒÔ±£Ö¤UTF16µÄ×Ö·û´®±»ÕýÈ·´¦Àí¡£Óë´Ëͬʱ£¬Ê¹Óú궨ÒåÀ´Ìæ»»wchar_t¹Ø¼ü×ÖΪunsigned short£¬ÒÔ±£Ö¤º¯ÊýÉùÃ÷µÄ¼æÈÝ¡£

£¨2£©new²Ù×÷·ûµÄ³ö´í´¦Àí

ÁíÒ»¸öÎÊÌâÊÇnew²Ù×÷·ûµÄ³ö´í´¦Àí¡£ÓÉÓÚ±àÒëÆ÷µÄÉèÖò»Í¬£¬new²Ù×÷·û¿ÉÄܾßÓв»Í¬µÄÐÐΪ¡£¿¼²ìÈçϵĴúÂë¶Î£º

#include

class A

{

public:

void *operator new( size_t size )

{

return NULL;

}

A()

{

printf("Constructor called\n");

a = 0;

}

private:

int a;

};

int main()

{

A *p = new A();

printf("%x\n", p );

return 0;

}

ÔÚVisual C++ 2003ÖУ¬ÉÏÃæµÄ³ÌÐòÊä³ö0¡£¶øGCC 4.1.0±àÒëÆ÷µÄÊä³ö½á¹ûΪ£º

Constructor called

Segmentation fault

Ò²¾ÍÊÇ˵£¬Visual C++ 2003µÄ±àÒëÆ÷»á¼ì²énewµÄ·µ»ØÖµ£¬Èç¹û·µ»ØΪ¿Õ£¬¹¹Ô캯Êý¾Í²»ÔÙÖ´ÐС£µ«ÊÇgcc±ØÐë¼ÓÉÏ-fcheck-new±àÒë²ÎÊý²Å¾ßÓÐÕâÒ»ÐÐΪ£ºg++ -fcheck-new test.cpp¡£ÕâÑùÔÚLinuxÉÏÉÏÊö³ÌÐòÒ²»áÊä³ö0¡£

£¨3£©½á¹¹»¯Òì³£ºÍC++Òì³£

»¹ÓÐÒ»¸ö¸üÒþ±ÎµÄ²îÒì´æÔÚÓÚÒì³£´¦Àí¡£Visual C++²¢²»×ñÑ­Òì³£´¦ÀíµÄC++¹æ·¶¡£¿¼²ìÈçϵijÌÐò¶Î£º

#include

int main()

{

int* p = NULL;

try

{

*p = 0;

}

catch (...)

{

printf("caught the exception\n");

return 1;

}

return 0;

}

¶ÁÕß¿ÉÒÔ×Ô¼ºÓà Visual C++ 2003ºÍGCC·Ö±ð¼ìÑéÕâ¶Î³ÌÐò¡£Ç°ÕßÉú³ÉµÄ³ÌÐòÔÚWindowsÉÏÕý³£ÔËÐУ¬Êä³öcaught the exception£¬È»ºóÕý³£Í˳ö¡£¶øGCCÉú³ÉµÄ³ÌÐòÖ»ÊÇÊä³öSegmentation fault¡£ËùÒÔÔÚWindowsÉÏ£¬catchÓï¾äץסÁËÒ»¸öÒì³£¡£°´ÕÕC++µÄ±ê×¼£¬Ö»ÓÐʹÓÃthrowÓï¾ä£¬²ÅÄܲúÉúÒì³£¡£µ«ÊÇÔÚÉÏÃæµÄ³ÌÐò¶ÎÖУ¬ Ö»ÊÇÒ»¸ö¼òµ¥µÄ¸³ÖµÓï¾ä¡£Ô­ÒòÔÚÓÚ£¬Visual C++ 2003½«C++µÄÒì³£´¦ÀíÓ³Éä³ÉÁËWindowsµÄ½á¹¹»¯Òì³£´¦Àí¡£ÔÚÉÏÃæµÄÓï¾äÖУ¬*p = 0½«ÒýÆðÒ»¸öWindowsµÄÒì³££¬Visual C++½«Ëü´¦Àí³ÉÒ»¸öC++Òì³££¬²¢½øÈëcatch¿é¡£ÔÚLinuxÉÏ£¬ÓÉÓÚûÓÐC++Òì³£·¢Éú£¬³ÌÐòÖ±½Ó±ÀÀ£¡£

ÉÏÊö¸ø³öÁËһЩµäÐ͵ıàÒëÆ÷²î±ð£¬ÆäËûϸ΢²î±ðδÄÜÈ«²¿×ܽᲢÔÚ´ËÂÞÁУ¬¶ÁÕßÔÚʵ¼Ê¿ª·¢¹ý³ÌÖУ¬Ð辡ÔçÒâʶµ½ÕâЩÎÊÌ⣬ÒÔ±ÜÃâ²»±ØÒªµÄµ÷ÊÔŬÁ¦¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ