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

½â¾ö³öÏÖµÄLNK2005¡°·ûºÅÒѶ¨Ò塱´íÎó

·¢²¼Ê±¼ä:2006-05-03 11:59:03À´Ô´:ºìÁª×÷Õß:°®µÄ±ßÔµ
Ðí¶àVisual C++µÄʹÓÃÕ߶¼Åöµ½¹ýLNK2005:symbol already definedºÍLNK1169:one or more multiply defined symbols foundÕâÑùµÄÁ´½Ó´íÎ󣬶øÇÒͨ³£ÊÇÔÚʹÓõÚÈý·½¿âʱÓöµ½µÄ¡£¶ÔÓÚÕâ¸öÎÊÌ⣬ÓеÄÅóÓÑ¿ÉÄܲ»ÖªÆäÈ»£¬¶øÓеÄÅóÓÑ¿ÉÄÜÖªÆäȻȴ²»ÖªÆäËùÒÔÈ»£¬ÄÇô±¾ÎľÍÊÔͼΪ´ó¼Ò³¹µ×½â¿ª¹ØÓÚËüµÄÖÖÖÖÒÉ»ó¡£

´ó¼Ò¶¼ÖªµÀ£¬´ÓC/C++Ô´³ÌÐòµ½¿ÉÖ´ÐÐÎļþÒª¾­ÀúÁ½¸ö½×¶Î:(1)±àÒëÆ÷½«Ô´Îļþ±àÒë³É»ã±à´úÂ룬ȻºóÓÉ»ã±àÆ÷(assembler)·­Òë³É»úÆ÷Ö¸Áî (ÔÙ¼ÓÉÏÆäËüÏà¹ØÐÅÏ¢)ºóÊä³öµ½Ò»¸ö¸öÄ¿±êÎļþ(object file,VCµÄ±àÒëÆ÷±àÒë³öµÄÄ¿±êÎļþĬÈϵĺó׺ÃûÊÇ.obj)ÖУ»(2)Á´½ÓÆ÷(linker)½«Ò»¸ö¸öµÄÄ¿±êÎļþ(»òÐí»¹»áÓÐÈô¸É³ÌÐò¿â)Á´½ÓÔÚÒ»ÆðÉú³ÉÒ»¸öÍêÕûµÄ¿ÉÖ´ÐÐÎļþ¡£

±àÒëÆ÷±àÒëÔ´Îļþʱ»á°ÑÔ´ÎļþµÄÈ«¾Ö·ûºÅ(global symbol)·Ö³ÉÇ¿(strong)ºÍÈõ(weak)Á½Àà´«¸ø»ã±àÆ÷£¬¶øËæºó»ã±àÆ÷Ôò½«Ç¿ÈõÐÅÏ¢±àÂë²¢±£´æÔÚÄ¿±êÎļþµÄ·ûºÅ±íÖС£ÄÇôºÎνǿÈõÄØ£¿±àÒëÆ÷ÈÏΪº¯ÊýÓë³õʼ»¯Á˵ÄÈ«¾Ö±äÁ¿¶¼ÊÇÇ¿·ûºÅ£¬¶øδ³õʼ»¯µÄÈ«¾Ö±äÁ¿Ôò³ÉÁËÈõ·ûºÅ¡£±ÈÈçÓÐÕâô¸öÔ´Îļþ:

extern int errorno;
int buf[2] = {1,2};
int *p;

int main()
{
return 0;
}

ÆäÖÐmain¡¢bufÊÇÇ¿·ûºÅ£¬pÊÇÈõ·ûºÅ£¬¶øerrornoÔò·ÇÇ¿·ÇÈõ£¬ÒòΪËüÖ»ÊǸöÍⲿ±äÁ¿µÄʹÓÃÉùÃ÷¡£

ÓÐÁËÇ¿Èõ·ûºÅµÄ¸ÅÄÎÒÃǾͿÉÒÔ¿´¿´Á´½ÓÆ÷ÊÇÈçºÎ´¦ÀíÓëÑ¡Ôñ±»¶à´Î¶¨Òå¹ýµÄÈ«¾Ö·ûºÅ:

¹æÔò1: ²»ÔÊÐíÇ¿·ûºÅ±»¶à´Î¶¨Òå(¼´²»Í¬µÄÄ¿±êÎļþÖв»ÄÜÓÐͬÃûµÄÇ¿·ûºÅ)£»


¹æÔò2: Èç¹ûÒ»¸ö·ûºÅÔÚij¸öÄ¿±êÎļþÖÐÊÇÇ¿·ûºÅ£¬ÔÚÆäËüÎļþÖж¼ÊÇÈõ·ûºÅ£¬ÄÇôѡÔñÇ¿·ûºÅ£»


¹æÔò3: Èç¹ûÒ»¸ö·ûºÅÔÚËùÓÐÄ¿±êÎļþÖж¼ÊÇÈõ·ûºÅ£¬ÄÇôѡÔñÆäÖÐÈÎÒâÒ»¸ö£»

ÓÉÉÏ¿ÉÖª¶à¸öÄ¿±êÎļþ²»ÄÜÖظ´¶¨ÒåͬÃûµÄº¯ÊýÓë³õʼ»¯Á˵ÄÈ«¾Ö±äÁ¿£¬·ñÔò±ØÈ»µ¼ÖÂLNK2005ºÍLNK1169Á½ÖÖÁ´½Ó´íÎ󡣿ÉÊÇ£¬ÓеÄʱºòÎÒÃDz¢Ã»ÓÐÔÚ×Ô¼ºµÄ³ÌÐòÖз¢ÏÖÕâÑùµÄÖض¨ÒåÏÖÏó£¬È´Ò²Óöµ½ÁË´ËÖÖÁ´½Ó´íÎó£¬ÕâÓÖÊǺν⣿àÅ£¬ÎÊÌâÉÔ΢Óеã¶ù¸´ÔÓ£¬ÈÝÎÒÂýÂýµÀÀ´¡£

ÖÚËùÖÜÖª£¬ANSI C/C++ ¶¨ÒåÁËÏ൱¶àµÄ±ê×¼º¯Êý£¬¶øËüÃÇÓÖ·Ö²¼ÔÚÐí¶à²»Í¬µÄÄ¿±êÎļþÖУ¬Èç¹ûÖ±½ÓÒÔÄ¿±êÎļþµÄÐÎʽÌṩ¸ø³ÌÐòԱʹÓõĻ°£¬¾ÍÐèÒªËûÃÇÈ·ÇеØÖªµÀÄĸöº¯Êý´æÔÚÓÚÄĸöÄ¿±êÎļþÖУ¬²¢ÇÒÔÚÁ´½ÓʱÏÔʽµØÖ¸¶¨Ä¿±êÎļþÃû²ÅÄܳɹ¦µØÉú³É¿ÉÖ´ÐÐÎļþ£¬ÏÔÈ»ÕâÊÇÒ»¸ö¾Þ´óµÄ¸ºµ£¡£ËùÒÔCÓïÑÔÌṩÁËÒ»ÖÖ½«¶à¸öÄ¿±êÎļþ´ò°ü³ÉÒ»¸öÎļþµÄ»úÖÆ£¬Õâ¾ÍÊǾ²Ì¬³ÌÐò¿â(static library)¡£¿ª·¢ÕßÔÚÁ´½ÓʱֻÐèÖ¸¶¨³ÌÐò¿âµÄÎļþÃû£¬Á´½ÓÆ÷¾Í»á×Ô¶¯µ½³ÌÐò¿âÖÐÑ°ÕÒÄÇЩӦÓóÌÐòȷʵÓõ½µÄÄ¿±êÄ£¿é£¬²¢°Ñ(ÇÒÖ»°Ñ)ËüÃÇ´Ó¿âÖп½±´³öÀ´²ÎÓë¹¹½¨¿ÉÖ´ÐÐÎļþ¡£¼¸ºõËùÓеÄC/C++¿ª·¢ÏµÍ³¶¼»á°Ñ±ê×¼º¯Êý´ò°ü³É±ê×¼¿âÌṩ¸ø¿ª·¢ÕßʹÓÃ(Óв»Õâô×öµÄÂð£¿)¡£

³ÌÐò¿âΪ¿ª·¢Õß´øÀ´ÁË·½±ã£¬µ«Í¬Ê±Ò²ÊÇijЩ»ìÂҵĸùÔ´¡£ÎÒÃÇÀ´¿´¿´Á´½ÓÆ÷ÊÇÈçºÎ½âÎö(resolve)¶Ô³ÌÐò¿âµÄÒýÓõġ£

ÔÚ·ûºÅ½âÎö(symbol resolution)½×¶Î£¬Á´½ÓÆ÷°´ÕÕËùÓÐÄ¿±êÎļþºÍ¿âÎļþ³öÏÖÔÚÃüÁîÐÐÖеÄ˳Ðò´Ó×óÖÁÓÒÒÀ´ÎɨÃèËüÃÇ£¬ÔÚ´ËÆÚ¼äËüҪά»¤Èô¸É¸ö¼¯ºÏ:(1)¼¯ºÏEÊǽ«±»ºÏ²¢µ½Ò»Æð×é³É¿ÉÖ´ÐÐÎļþµÄËùÓÐÄ¿±êÎļþ¼¯ºÏ£»(2)¼¯ºÏUÊÇδ½âÎö·ûºÅ(unresolved symbols£¬±ÈÈçÒѾ­±»ÒýÓõ«ÊÇ»¹Î´±»¶¨ÒåµÄ·ûºÅ)µÄ¼¯ºÏ£»(3)¼¯ºÏDÊÇËùÓÐ֮ǰÒѱ»¼ÓÈëµ½EµÄÄ¿±êÎļþ¶¨ÒåµÄ·ûºÅ¼¯ºÏ¡£Ò»¿ªÊ¼£¬E¡¢U¡¢D¶¼Êǿյġ£

(1): ¶ÔÃüÁîÐÐÖеÄÿһ¸öÊäÈëÎļþf£¬Á´½ÓÆ÷È·¶¨ËüÊÇÄ¿±êÎļþ»¹ÊÇ¿âÎļþ£¬Èç¹ûËüÊÇÄ¿±êÎļþ£¬¾Í°Ñf¼ÓÈëµ½E£¬²¢°ÑfÖÐδ½âÎöµÄ·ûºÅºÍÒѶ¨ÒåµÄ·ûºÅ·Ö±ð¼ÓÈëµ½U¡¢D¼¯ºÏÖУ¬È»ºó´¦ÀíÏÂÒ»¸öÊäÈëÎļþ¡£

(2): Èç¹ûfÊÇÒ»¸ö¿âÎļþ£¬Á´½ÓÆ÷»á³¢ÊÔ°ÑUÖеÄËùÓÐδ½âÎö·ûºÅÓëfÖи÷Ä¿±êÄ£¿é¶¨ÒåµÄ·ûºÅ½øÐÐÆ¥Åä¡£Èç¹ûij¸öÄ¿±êÄ£¿ém¶¨ÒåÁËÒ»¸öUÖеÄδ½âÎö·ûºÅ£¬ÄÇô¾Í°Ñ m¼ÓÈëµ½EÖУ¬²¢°ÑmÖÐδ½âÎöµÄ·ûºÅºÍÒѶ¨ÒåµÄ·ûºÅ·Ö±ð¼ÓÈëµ½U¡¢D¼¯ºÏÖС£²»¶ÏµØ¶ÔfÖеÄËùÓÐÄ¿±êÄ£¿éÖظ´Õâ¸ö¹ý³ÌÖ±ÖÁµ½´ïÒ»¸ö²»¶¯µã(fixed point)£¬´ËʱUºÍD²»Ôٱ仯¡£¶øÄÇЩδ¼ÓÈëµ½EÖеÄfÀïµÄÄ¿±êÄ£¿é¾Í±»¼òµ¥µØ¶ªÆú£¬Á´½ÓÆ÷¼ÌÐø´¦ÀíÏÂÒ»ÊäÈëÎļþ¡£

(3): Èç¹û´¦Àí¹ý³ÌÖÐÍùD¼ÓÈëÒ»¸öÒÑ´æÔڵķûºÅ£¬»òÕßµ±É¨ÃèÍêËùÓÐÊäÈëÎļþʱU·Ç¿Õ£¬Á´½ÓÆ÷±¨´í²¢Í£Ö¹¶¯×÷¡£·ñÔò£¬Ëü°ÑEÖеÄËùÓÐÄ¿±êÎļþºÏ²¢ÔÚÒ»ÆðÉú³É¿ÉÖ´ÐÐÎļþ¡£

VC´øµÄ±àÒëÆ÷Ãû×Ö½Ðcl.exe£¬ËüÓÐÕâô¼¸¸öÓë±ê×¼³ÌÐò¿âÓйصÄÑ¡Ïî: /ML¡¢/MLd¡¢/MT¡¢/MTd¡¢/MD¡¢/MDd¡£ÕâЩѡÏî¸æËß±àÒëÆ÷Ó¦ÓóÌÐòÏëʹÓÃʲô°æ±¾µÄC±ê×¼³ÌÐò¿â¡£/ML(ȱʡѡÏî)¶ÔÓ¦µ¥Ï߳̾²Ì¬°æµÄ±ê×¼³ÌÐò¿â(libc.lib)£»/MT¶ÔÓ¦¶àÏ߳̾²Ì¬°æ±ê×¼¿â(libcmt.lib)£¬´Ëʱ±àÒëÆ÷»á×Ô¶¯¶¨Òå_MTºê£»/MD¶ÔÓ¦¶àÏß³ÌDLL°æ (µ¼Èë¿âmsvcrt.lib£¬DLLÊÇmsvcrt.dll)£¬±àÒëÆ÷×Ô¶¯¶¨Òå_MTºÍ_DLLÁ½¸öºê¡£ºóÃæ¼ÓdµÄÑ¡Ï»áÈñàÒëÆ÷×Ô¶¯¶à¶¨ÒåÒ»¸ö _DEBUGºê£¬±íʾҪʹÓöÔÓ¦±ê×¼¿âµÄµ÷Ê԰棬Òò´Ë/MLd¶ÔÓ¦µ÷ÊÔ°æµ¥Ï߳̾²Ì¬±ê×¼¿â(libcd.lib)£¬/MTd¶ÔÓ¦µ÷ÊÔ°æ¶àÏ߳̾²Ì¬±ê×¼¿â (libcmtd.lib)£¬/MDd¶ÔÓ¦µ÷ÊÔ°æ¶àÏß³ÌDLL±ê×¼¿â(µ¼Èë¿âmsvcrtd.lib£¬DLLÊÇmsvcrtd.dll)¡£ËäÈ»ÎÒÃǵÄÈ·ÔÚ±àÒëʱÃ÷°×ÎÞÎóµØ¸æËßÁ˱àÒëÆ÷Ó¦ÓóÌÐòÏ£ÍûʹÓÃʲô°æ±¾µÄ±ê×¼¿â£¬¿ÉÊǵ±±àÒëÆ÷¸ÉÍêÁ˻ÂÖµ½Á´½ÓÆ÷¿ª¹¤Ê±ËüÓÖÈçºÎµÃÖªÒ»¸ö¸öÄ¿±êÎļþµ½µ×ÔÚ˼ÄîË­£¿ÎªÁË´«µÝÏà˼£¬ÎÒÃǵıàÒëÆ÷¾Í¸ÉÁ˵ãÃØÃܵĹ´µ±¡£ÔÚcl±àÒë³öµÄÄ¿±êÎļþÖлáÓÐÒ»¸öרÃŵÄÇøÓò(¹ØÐÄÕâ¸öÇøÓòµ½µ×ÔÚÎļþÖÐʲôµØ·½µÄÅóÓÑ¿ÉÒԲο¼COFFºÍ PEÎļþ¸ñʽ)´æ·ÅһЩָµ¼Á´½ÓÆ÷ÈçºÎ¹¤×÷µÄÐÅÏ¢£¬ÆäÖÐÓÐÒ»Ö־ͽÐȱʡ¿â(default library)£¬ÕâЩÐÅÏ¢Ö¸¶¨ÁËÒ»¸ö»ò¶à¸ö¿âÎļþÃû£¬¸æËßÁ´½ÓÆ÷ÔÚɨÃèµÄʱºòÒ²°ÑËüÃǼÓÈëµ½ÊäÈëÎļþÁбíÖÐ(µ±È»Ë³ÐòλÓÚÔÚÃüÁîÐÐÖб»Ö¸¶¨µÄÊäÈëÎļþÖ®ºó)¡£Ëµµ½ÕâÀÎÒÃÇÏÈÀ´×ö¸öСʵÑ顣д¸ö¶¥¶¥¼òµ¥µÄ³ÌÐò£¬È»ºó±£´æΪmain.c :

/* main.c */
int main() { return 0; }

¡¡¡¡ÓÃÏÂÃæÕâ¸öÃüÁî±àÒëmain.c(ʲô£¿Äã´Ó²»ÓÃÃüÁîÐÐÀ´±àÒë³ÌÐò£¿Õâ¸ö......) :

cl /c main.c

/c ÊǸæËßclÖ»±àÒëÔ´Îļþ£¬²»ÓÃÁ´½Ó¡£ÒòΪ/MLÊÇȱʡѡÏËùÒÔÉÏÊöÃüÁîÒ²Ï൱ÓÚ: cl /c /ML main.c ¡£Èç¹ûûʲôÎÊÌâµÄ»°(Òª³öÁËÎÊÌâ²ÅÊÇ»î¼û¹í£¡µ±È»³ý·ÇÄãµÄ»·¾³±äÁ¿Ã»ÓÐÉèÖúã¬ÕâʱÄãÓ¦¸ÃÈ¥VCµÄbinĿ¼ÏÂÕÒµ½vcvars32.batÎļþÈ»ºóÔËÐÐËü¡£)£¬µ±Ç°Ä¿Â¼Ï»á³öÏÖÒ»¸ömain.objÎļþ£¬Õâ¾ÍÊÇÎÒÃÇ¿É°®µÄÄ¿±êÎļþ¡£Ëæ±ãÓÃÒ»¸öÎı¾±à¼­Æ÷´ò¿ªËü(Êǵģ¬Îı¾±à¼­Æ÷£¬´óµ¨µØÈ¥×ö±ðº¦ÅÂ)£¬ËÑË÷"defaultlib"×Ö·û´®£¬Í¨³£Äã¾Í»á¿´µ½ÕâÑùµÄ¶«Î÷: "-defaultlib:LIBC -defaultlib:OLDNAMES"¡£°¡¹þ£¬Ã»´í£¬Õâ¾Í
ÊDZ£´æÔÚÄ¿±êÎļþÖеÄȱʡ¿âÐÅÏ¢¡£ÎÒÃǵÄÄ¿±êÎļþÏÔȻָ¶¨ÁËÁ½¸öȱʡ¿â£¬Ò»¸öÊǵ¥Ï߳̾²Ì¬°æ±ê×¼¿âlibc.lib(ÕâÓë/MLÑ¡ÏîÏà·û)£¬ÁíÍâÒ»¸öÊÇoldnames.lib(ËüÊÇΪÁ˼æÈÝ΢ÈíÒÔÇ°µÄC/C++¿ª·¢ÏµÍ³)¡£

VCµÄÁ´½ÓÆ÷ÊÇlink.exe£¬ÒòΪmain.obj±£´æÁËȱʡ¿âÐÅÏ¢£¬ËùÒÔ¿ÉÒÔÓÃ

link main.obj libc.lib

»òÕß

link main.obj

À´Éú³É¿ÉÖ´ÐÐÎļþmain.exe£¬ÕâÁ½¸öÃüÁîÊǵȼ۵ġ£µ«ÊÇÈç¹ûÄãÓÃ

link main.obj libcd.lib

µÄ»°£¬Á´½ÓÆ÷»á¸ø³öÒ»¸ö¾¯¸æ: "warning LNK4098: defaultlib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library"£¬ÒòΪÄãÏÔʽָ¶¨µÄ±ê×¼¿â°æ±¾ÓëÄ¿±êÎļþµÄȱʡֵ²»Ò»Ö¡£Í¨³£À´Ëµ£¬Ó¦¸Ã±£Ö¤Á´½ÓÆ÷ºÏ²¢µÄËùÓÐÄ¿±êÎļþÖ¸¶¨µÄȱʡ±ê×¼¿â°æ±¾Ò»Ö£¬·ñÔò±àÒëÆ÷Ò»¶¨»á¸ø³öÉÏÃæµÄ¾¯¸æ£¬¶øLNK2005ºÍLNK1169Á´½Ó´íÎóÔòÓÐʱ»á³öÏÖÓÐʱ²»»á¡£ÄÇôÕâ¸öÓÐʱµ½µ×ÊÇʲôʱºò£¿ºÇºÇ£¬±ð׿±£¬ÏÂÃæµÄÒ»ÇÐÕýÊÇΪϲ»¶×·¸ù¾¿µ×µÄÄã×¼±¸µÄ¡£

½¨Ò»¸öÔ´Îļþ£¬¾Í½Ðmylib.c£¬ÄÚÈÝÈçÏÂ:

/* mylib.c */
#include

void foo()
{
printf("%s","I am from mylib!\n");
}

ÓÃ

cl /c /MLd mylib.c

ÃüÁî±àÒ룬עÒâ/MLdÑ¡ÏîÊÇÖ¸¶¨libcd.libΪĬÈϱê×¼¿â¡£lib.exeÊÇVC×Ô´øµÄÓÃÓÚ½«Ä¿±êÎļþ´ò°ü³É³ÌÐò¿âµÄÃüÁËùÒÔÎÒÃÇ¿ÉÒÔÓÃ

lib /OUT:my.lib mylib.obj

½«mylib.obj´ò°ü³É¿â£¬Êä³öµÄ¿âÎļþÃûÊÇmy.lib¡£½ÓÏÂÀ´°Ñmain.c¸Ä³É:

/* main.c */
void foo();

int main()
{
foo();
return 0;
}

ÓÃ

cl /c main.c

±àÒ룬ȻºóÓÃ

link main.obj my.lib

½øÐÐÁ´½Ó¡£Õâ¸öÃüÁîÄܹ»³É¹¦µØÉú³Émain.exe¶ø²»»á²úÉúLNK2005ºÍLNK1169Á´½Ó´íÎó£¬Äã½ö½öÊǵõ½ÁËÒ»Ìõ¾¯¸æÐÅÏ¢:"warning LNK4098: defaultlib "LIBCD" conflicts with use of other libs; use /NODEFAULTLIB:library"¡£ÎÒÃǸù¾ÝÇ°ÎÄËùÊöµÄɨÃè¹æÔòÀ´·ÖÎöÒ»ÏÂÁ´½ÓÆ÷´Ëʱ×öÁËЩɶ¡£

Ò»¿ªÊ¼E¡¢U¡¢D¶¼ÊÇ¿Õ¼¯£¬Á´½ÓÆ÷Ê×ÏÈɨÃèµ½main.obj£¬°ÑËü¼ÓÈëE¼¯ºÏ£¬Í¬Ê±°Ñδ½âÎöµÄfoo¼ÓÈëU£¬°Ñmain¼ÓÈëD£¬¶øÇÒÒòΪ main.objµÄĬÈϱê×¼¿âÊÇlibc.lib£¬ËùÒÔËü±»¼ÓÈëµ½µ±Ç°ÊäÈëÎļþÁбíµÄĩβ¡£½Ó×ÅɨÃèmy.lib£¬ÒòΪÕâÊǸö¿â£¬ËùÒÔ»áÄõ±Ç°UÖеÄËùÓзûºÅ(µ±È»ÏÖÔÚ¾ÍÒ»¸öfoo)Óëmy.libÖеÄËùÓÐÄ¿±êÄ£¿é(µ±È»Ò²Ö»ÓÐÒ»¸ömylib.obj)ÒÀ´ÎÆ¥Å䣬¿´ÊÇ·ñÓÐÄ£¿é¶¨ÒåÁËUÖеķûºÅ¡£½á¹û mylib.objȷʵ¶¨ÒåÁËfoo£¬ÓÚÊÇËü±»¼ÓÈëµ½E£¬foo´ÓUתÒƵ½D£¬mylib.objÒýÓõÄprintf¼ÓÈëµ½U£¬Í¬ÑùµØ£¬ mylib.objÖ¸¶¨µÄĬÈϱê×¼¿âÊÇlibcd.lib£¬ËüÒ²±»¼Óµ½µ±Ç°ÊäÈëÎļþÁбíµÄĩβ(ÔÚlibc.libµÄºóÃæ)¡£²»¶ÏµØÔÚmy.lib¿âµÄ¸÷Ä£¿éÉϽøÐеü´úÒÔÆ¥ÅäUÖеķûºÅ£¬Ö±µ½U¡¢D¶¼²»Ôٱ仯¡£ºÜÃ÷ÏÔ£¬ÏÖÔÚ¾ÍÒѾ­µ½´ïÁËÕâôһ¸ö²»¶¯µã£¬ËùÒÔ½Ó×ÅɨÃèÏÂÒ»¸öÊäÈëÎļþ£¬¾ÍÊÇlibc.lib¡£

Á´½ÓÆ÷·¢ÏÖlibc.libÀïµÄprintf.objÀﶨÒåÓÐprintf£¬ÓÚÊÇprintf´ÓUÒƵ½D£¬¶øprintf.obj±»¼ÓÈëµ½E£¬Ëü¶¨ÒåµÄËùÓзûºÅ¼ÓÈëµ½D£¬ËüÀïÍ·µÄδ½âÎö·ûºÅ¼ÓÈëµ½U¡£Á´½ÓÆ÷»¹»á°Ñÿ¸ö³ÌÐò¶¼ÒªÓõ½µÄһЩ³õʼ»¯²Ù×÷ËùÔÚµÄÄ¿±êÄ£¿é(±ÈÈçcrt0.objµÈ)¼°ËüÃÇËùÒýÓõÄÄ£¿é (±ÈÈçmalloc.obj¡¢free.objµÈ)×Ô¶¯¼ÓÈëµ½EÖУ¬²¢¸üÐÂUºÍDÒÔ·´Ó¦Õâ¸ö±ä»¯¡£ÊÂʵÉÏ£¬±ê×¼¿â¸÷Ä¿±êÄ£¿éÀïµÄδ½âÎö·ûºÅ¶¼¿ÉÒÔÔÚ¿âÄÚÆäËüÄ£¿éÖÐÕÒµ½¶¨Ò壬Òò´Ëµ±Á´½ÓÆ÷´¦ÀíÍêlibc.libʱ£¬UÒ»¶¨Êǿյġ£×îºó´¦Àílibcd.lib£¬ÒòΪ´ËʱUÒѾ­Îª¿Õ£¬ËùÒÔÁ´½ÓÆ÷»áÅ×ÆúËüÀïÃæµÄËùÓÐÄ¿±êÄ£¿é´Ó¶ø½áÊøɨÃ裬ȻºóºÏ²¢EÖеÄÄ¿±êÄ£¿é²¢Êä³ö¿ÉÖ´ÐÐÎļþ¡£

ÉÏÎÄÃèÊöÁËËäÈ»¸÷Ä¿±êÄ£¿éÖ¸¶¨Á˲»Í¬°æ±¾µÄȱʡ±ê×¼¿âµ«ÈÔÈ»Á´½Ó³É¹¦µÄÀý×Ó£¬½ÓÏÂÀ´Ä㽫Ŀ¶ÃÒòΪÕâÖÖ²»ÑϽ÷¶øµ¼Öµı¯²Òʧ°Ü¡£

ÐÞ¸Ämylib.c³ÉÕâ¸öÑù×Ó:

#include

void foo()
{
// just a test , don't care memory leak
_malloc_dbg( 1, _NORMAL_BLOCK, __FILE__, __LINE__ );
}

ÆäÖÐ_malloc_dbg²»ÊÇANSI CµÄ±ê×¼¿âº¯Êý£¬ËüÊÇVC±ê×¼¿âÌṩµÄmallocµÄµ÷Ê԰棬ÓëÏà¹Øº¯ÊýÅäÌ×ÄÜ°ïÖú¿ª·¢Õß×¥¸÷ÖÖÄÚ´æ´íÎó¡£Ê¹ÓÃËüÒ»¶¨Òª¶¨Òå_DEBUGºê£¬·ñÔòÔ¤´¦ÀíÆ÷»á°ÑËü×Ô¶¯×ªÎªmalloc¡£¼ÌÐøÓÃ

cl /c /MLd mylib.c
lib /OUT:my.lib mylib.obj

±àÒë´ò°ü¡£µ±ÔÙ´ÎÓÃ

link main.obj my.lib

½øÐÐÁ´½Óʱ£¬ÎÒÃÇ¿´µ½ÁËʲô£¿ÌìÄÄ£¬Ò»¶ÑµÄLNK2005¼ÓÉϸö¹óΪ"fatal error"µÄLNK1169µæµ×£¬µ±È»»¹ÉÙ²»ÁËÄǸöLNK4098¡£Á´½ÓÆ÷ÊDz»ÊÇ·èÁË£¿²»£¬ÄãÔ©Í÷¿ÉÁ¯µÄÁ´½ÓÆ÷ÁË£¬ÎÒÅÄÐظ¬±£Ö¤Ëü¿ÉÊÇÒ»Ö±ÔÚ¾¡Ðľ¡ÔðµØÕÕÕ°ìÊ¡£

Ò»¿ªÊ¼E¡¢U¡¢DΪ¿Õ£¬Á´½ÓÆ÷ɨÃèmain.obj£¬°ÑËü¼ÓÈëE£¬°Ñfoo¼ÓÈëU£¬°Ñ main¼ÓÈëD£¬°Ñlibc.lib¼ÓÈëµ½µ±Ç°ÊäÈëÎļþÁбíµÄĩβ¡£½Ó×ÅɨÃèmy.lib£¬foo´ÓUתÒƵ½D£¬_malloc_dbg¼ÓÈëµ½U£¬ libcd.lib¼Óµ½µ±Ç°ÊäÈëÎļþÁбíµÄβ²¿¡£È»ºóɨÃèlibc.lib£¬Õâʱ»á·¢ÏÖlibc.libÀïÈκÎÒ»¸öÄ¿±êÄ£¿é¶¼Ã»Óж¨Òå _malloc_dbg(ËüÖ»ÔÚµ÷ÊÔ°æµÄ±ê×¼¿âÖдæÔÚ)£¬ËùÒÔ²»»áÓÐÈκÎÒ»¸öÄ£¿éÒòΪ_malloc_dbg¶ø¼ÓÈëE£¬µ«ÊÇÿ¸ö³ÌÐò¶¼ÒªÓõ½µÄ³õʼ»¯Ä£¿é (Èçcrt0.objµÈ)¼°ËüÃÇËùÒýÓõÄÄ£¿é(±ÈÈçmalloc.obj¡¢free.objµÈ)»¹ÊÇ»á×Ô¶¯¼ÓÈëµ½EÖУ¬Í¬Ê±UºÍD±»¸üÐÂÒÔ·´Ó¦Õâ¸ö±ä»¯¡£µ±Á´½ÓÆ÷´¦ÀíÍêlibc.libʱ£¬Uֻʣ_malloc_dbgÕâÒ»¸ö·ûºÅ¡£×îºó´¦Àílibcd.lib£¬·¢ÏÖdbgheap.obj¶¨ÒåÁË _malloc_dbg£¬ÓÚÊÇdbgheap.obj¼ÓÈëµ½E£¬ËüÀïÍ·µÄδ½âÎö·ûºÅ¼ÓÈëU£¬Ëü¶¨ÒåµÄËùÓÐÆäËü·ûºÅÒ²¼ÓÈëD£¬ÕâʱÔÖÄѱãÀ´ÁË¡£Ö®Ç° mallocµÈ·ûºÅÒѾ­ÔÚDÖÐ(Ëæ×Ålibc.libÀïµÄmalloc.obj¼ÓÈëE¶ø¼ÓÈëµÄ)£¬¶ødbgheap.objÓÖ¶¨ÒåÁË°üÀ¨mallocÔÚÄÚµÄÐí¶àͬÃû·ûºÅ£¬ÕâÒý·¢ÁËÖض¨Òå³åÍ»£¬Á´½ÓÆ÷Ö»ºÃÖжϹ¤×÷²¢±¨¸æ´íÎó¡£

ÏÖÔÚÎÒÃǸÃÖªµÀ£¬Á´½ÓÆ÷ÍêȫûÓÐÔðÈΣ¬ÔðÈÎÔÚÎÒÃÇ×Ô¼ºµÄÉíÉÏ¡£ÊÇÎÒÃÇ´ÖÐĵذÑȱʡ±ê×¼¿â°æ±¾²»Ò»ÖµÄÄ¿±êÎļþ(main.obj)Óë³ÌÐò¿â (my.lib)Á´½ÓÆðÀ´£¬µ¼ÖÂÁË´óÔÖÄÑ¡£½â¾ö°ì·¨ºÜ¼òµ¥£¬ÒªÃ´ÓÃ/MLdÑ¡ÏîÀ´ÖرàÒëmain.c£»ÒªÃ´ÓÃ/MLÑ¡ÏîÖرàÒëmylib.c¡£

ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃÇÓµÓпâmy.libµÄÔ´´úÂë(mylib.c)£¬ËùÒÔ¿ÉÒÔÓò»Í¬µÄÑ¡ÏîÖØбàÒëÕâЩԴ´úÂë²¢Ôٴδò°ü¡£¿ÉÈç¹ûʹÓõÄÊǵÚÈý·½µÄ¿â£¬Ëü²¢Ã»ÓÐÌṩԴ´úÂ룬ÄÇôÎÒÃǾÍÖ»Óиıä×Ô¼º³ÌÐòµÄ±àÒëÑ¡ÏîÀ´ÊÊÓ¦ÕâЩ¿âÁË¡£µ«ÊÇÈçºÎÖªµÀ¿âÖÐÄ¿±êÄ£¿éÖ¸¶¨µÄĬÈÏ¿âÄØ£¿ÆäʵVCÌṩµÄÒ»¸öС¹¤¾ß±ã¿ÉÒÔÍê³ÉÈÎÎñ£¬Õâ¾ÍÊÇdumpbin.exe¡£ÔËÐÐÏÂÃæÕâ¸öÃüÁî

dumpbin /DIRECTIVES my.lib

¡¡¡¡È»ºóÔÚÊä³öÖÐÕÒÄÇЩ"Linker Directives"Òýµ¼µÄÐÅÏ¢£¬ÄãÒ»¶¨»á·¢ÏÖÿһ´¦ÕâÑùµÄÐÅÏ¢¶¼»á°üº¬Èô¸É¸öÀàËÆ"-defaultlib:XXXX"ÕâÑùµÄ×Ö·û´®£¬ÆäÖÐXXXX±ã´ú±íÄ¿±êÄ£¿éÖ¸¶¨µÄȱʡ¿âÃû¡£

¡¡¡¡ÖªµÀÁ˵ÚÈý·½¿âÖ¸¶¨µÄĬÈϱê×¼¿â£¬ÔÙÓúÏÊʵÄÑ¡Ïî±àÒëÎÒÃǵÄÓ¦ÓóÌÐò£¬¾Í¿ÉÒÔ±ÜÃâLNK2005ºÍLNK1169Á´½Ó´íÎó¡£Ï²»¶IDEµÄÅóÓÑ£¬ÄãÒ»Ñù¿ÉÒÔµ½ "ProjectÊôÐÔ" -> "C/C++" -> "´úÂëÉú³É(code generation)" -> "ÔËÐÐʱ¿â(run-time library)" ÏîÏÂÉèÖÃÓ¦ÓóÌÐòµÄĬÈϱê×¼¿â°æ±¾£¬ÕâÓëÃüÁîÐÐÑ¡ÏîµÄЧ¹ûÊÇÒ»ÑùµÄ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ