C++ÓëC½øÐÐÏ໥º¯Êýµ÷Óã¬extern¹Ø¼ü´ÊµÄÃîÓÃ
extern³ýÁËÄܹ»ÓÃÓÚÉùÃ÷±äÁ¿£¬ÒѾÔÚÍⲿ±»¶¨Ò壬²»ÓÃÖØж¨ÒåºÍ·ÖÅäÄÚ´æ¿Õ¼ä£¬»¹¿ÉÒÔÔÚC++ÓëC½øÐÐÏ໥º¯Êýµ÷Ó÷½Ãæ»áÓкܴóµÄÓô¦¡£
Ê×ÏÈ£¬ÎÒÃÇÀ´¿´Ò»Ï£¬ÎªÊ²Ã´C++ºÍCÓïÑÔ²»ÄÜÏ໥½øÐк¯Êýµ÷Óãº
±àдÈçϵÄÔ´Îļþ£º
int func(int i)
{
return 1;
}
·Ö±ð´æΪtest.cc,test.c£¬·Ö±ð½øÐбàÒëÉú³ÉobjectÎļþ£º
g++ -c test.cc -o testcc
gcc -c test.c -o testc
ʹÓÃnmÃüÁî²é¿´Éú³ÉµÄobjectÎļþµÄ²»Í¬ÔÚÄĸöµØ·½£º
nm testcc
00000000 T _Z4funci
U __gxx_personality_v0
nm testc
00000000 T func
¿´³öÀ´ÁË°É£¬C++±àÒëÆ÷°Ñ²ÎÊýÐÅÏ¢±àÒëºó·Åµ½Á˺¯ÊýµÄ¶ÔÏóÃû×ÖÀïÃ棨ΪÁËʵÏÖ¶à̬£©£¬ËùÒÔ²»ÄÜ»¥ÏàÖ±½Óµ÷Óá£
ÖªµÀÁËÔÒò£¬½â¾öµÄ°ì·¨×ÔÈ»¾ÍÓÐÁË£¬Ö»Òª¸æËßÁ´½Ó³ÌÐò£¬ÄãÁ´½ÓµÄobjectµÄÃüÃû·½Ê½£¬Ëü¾Í»á°ïÄã×ö²»Ò»ÑùµÄ²éÕÒ£¬´Ó¶øÕÒµ½C»òÕßC++ÀïÃæµÄº¯ÊýÁË¡£
±ÈÈ磺
ÒªÈÃC++Äܹ»µ÷ÓÃCµÄº¯Êý£º
main.cc
extern "C"
{
int func(int i);
}
int main(void)
{
func(1);
}
½øÐбàÒëÁ´½Ó£º
g++ main.cc test.c
ÒªÈÃCÄܹ»µ÷ÓÃC++µÄº¯Êý£¬ÉÔ΢ÓÐЩ²»Í¬£º
main.c:
extern int func(int i);
int main(void)
{
func(1);
}
ÐèÒªÐÞ¸Ätest.ccµÄÔ´Îļþ£º
extern "C"
{
int func(int i);
}
int func(int i)
{
return 1;
}
¾Í¿ÉÒÔÁË
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃ棬ºóÃæ ......
CºÍÖ¸Õëѧϰ±Ê¼Ç £¨1£©
µÚ¶þÕ »ù±¾ÖªÊ¶
»·¾³£º·ÖΪ·ÒëºÍÖ´ÐÐ2¸ö¹ý³Ì¡£ÆäÖзÒë¿ÉÒÔÀí½âΪÊÇÒ»¸ö´Ó³ÌÐò—Ä¿±ê´úÂë—Á´½ÓÆ÷µÄ¹ý³Ì¡£±àÒë¹ý³ÌÓУº
1Ô¤´¦ÀíÆ÷£º´¦Àí#define #includeÕâÒ»Àà´úÂëµÄ¹ý³Ì£»
2Ô´´úÂë½âÎö£º²úÉú´íÎóºÍ¾¯¸æ£»
3ÓÅ»¯Æ÷£ºÓÐÀûÓÚÓÅ»¯´úÂ룬Ìá¸ßЧ ......
Ò»¡¢±à¼JavaÔ´Îļþ
=============================================
Hello.java
=============================================
package test;
public class Hello
{
static
{
try
{
//´Ë´¦¼´Îª±¾µØ·½·¨ËùÔÚÁ´½Ó¿âÃû
&n ......
µÚһƪÎÄÕÂÖÕÓÚÂä±ÊÁË£¬±¾ÎĽ«´Ó˼¿¼µÄ½Ç¶ÈÈ¥ÖØÐÂÈÏʶһÏÂHelloworld¡£ÔÚÀý×ÓÖ®ºó»áÌá³ö²»Í¬µÄÎÊÌ⣬Òýµ¼´ó¼Òȥ˼¿¼Ã¿Ò»¾äÄËÖÁÿ¸öϸ½Ú¡£Í¬Ê±Ï£ÍûÄܹ»Èôó¼ÒÒÔºóÄܹ»ÒÔ²»¶Ï˼¿¼²»¶ÏÌáÎʵķ½Ê½È¥¿´´ý×Ô¼ºµÄ³ÌÐò¡£ÎÒÏàÐÅ´ó¼Ò»á·¢ÏֺܶàеĶ«Î÷£¬ºÃÁËÇÐÈëÕýÌâ¡£
ÎÒÃÇÿѧһÖÖеÄÓïÑÔ»ù±¾¶¼»á´Óhelloworld¿ªÊ¼£¬»ù±¾Ã¿ÖÖÓïÑÔ ......
ÔÚC++ÖУ¬¿âµÄµØλÊǷdz£¸ßµÄ¡£C++Ö®¸¸ Bjarne StroustrupÏÈÉú¶à´Î±íʾÁËÉè¼Æ¿âÀ´À©³ä¹¦ÄÜÒªºÃ¹ýÉè¼Æ¸ü¶àµÄÓï·¨µÄÑÔÂÛ¡£ÏÖʵÖУ¬C++µÄ¿âÃÅÀà·±¶à£¬½â¾öµÄÎÊÌâÒ²ÊǼ«Æä¹ã·º£¬¿â´ÓÇáÁ¿¼¶µ½ÖØÁ¿¼¶µÄ¶¼ÓС£²»ÉÙ¶¼ÊÇÈÃÈËÑÛ½ç´ó¿ª£¬Òà»òÊÇÍû¶øÉú̾µÄ˼ά½Ü×÷¡£ÓÉÓÚ¿âµÄÊýÁ¿·Ç³£ÅӴ󣬶øÇÒÏÞÓÚ±ÊÕßˮƽ£¬ÆäÖкܶಢ²»Á˽⡣ËùÒÔÎÄÖ ......