ÈçºÎÔÚC++Öе÷ÓÃCµÄ´úÂë
ÒÔÇ°Ôø¾×ܽá¹ýһƪ(http://www.cppblog.com/franksunny/archive/2007/11/29/37510.html
)£¬¹ØÓÚÔÚ
C
ÖÐÈçºÎµ÷ÓÃ
C++
µÄ´úÂ룬µ±Ê±²¢Î´×öÍêÈ«µÄÕ¹¿ª£¬Ö»ÊǼòµ¥µÄ×öÁËϵ÷ÊÔ£¬×î½ü¿´µ½Ò»¸öÌâÄ¿ÒªÇóʵÏÖ
C
ºÍ
C++
ÖдúÂëµÄ»¥Ïàµ÷Óã¬Æä½á¹ûËäÈ»¶¼ÊÇͨ¹ý
extern “C”
À´ÊµÏÖ
£¬µ«ÊǾßÌ廹ÊÇÓÐЩ²î±ðµÄ¡£
ÏȶÔ
C
Öе÷ÓÃ
C++
´úÂë×÷¸ö¼òµ¥»Ø¹Ë£º
1¡¢
¶ÔÓÚ
C++
ÖзÇÀàµÄ³ÉÔ±º¯Êý£¬¿ÉÒÔ¼òµ¥µÄÔÚº¯ÊýÉùÃ÷Ç°Ãæ¼Ó
extern “C”
£¬Í¨³£º¯ÊýÉùÃ÷λÓÚÍ·ÎļþÖУ¬µ±È»Ò²¿ÉÒÔ½«ÉùÃ÷ºÍº¯Êý¶¨ÒåÒ»Æð·ÅÔÚ
cpp
ÖÐ
£¬ÔÚûÓÐÉùÃ÷µÄÇé¿öÏ£¬Ö±½ÓÔÚ¶¨ÒåÇ°Ìí¼Ó
extern “C”
Ò²¿É
2¡¢
¶ÔÓÚ
C++
ÀàµÄ³ÉÔ±º¯Êý£¬ÔòÐèÒªÁíÍâ×öÒ»¸ö
cpp
Îļþ£¬½«ÐèÒªµ÷Óõĺ¯Êý½øÐаü×°
¡£
ÒÔÉÏÁ½ÏîµÄʵÀý²Î¿´Ç°Ãæ
C
ÖÐÈçºÎµ÷ÓÃ
C++
´úÂëµÄÎÄÕ¡£
ҪʵÏÖ
C++
Öе÷ÓÃ
C
µÄ´úÂ룬¾ßÌå²Ù×÷£º
¶ÔÓÚ
C
Öеĺ¯Êý´úÂ룬Ҫô½«
C
´úÂëµÄÍ·Îļþ½øÐÐÐ޸ģ¬ÔÚÆä±»º¬Èë
C++
´úÂëʱÔÚÉùÃ÷ÖмÓÈë
extern “C”
»òÕßÔÚ
C++
´úÂëÖÐÖØÐÂÉùÃ÷Ò»ÏÂ
C
º¯Êý£¬ÖØÐÂÉùÃ÷ʱÌí¼ÓÉÏ
extern “C”
Í·
¡£
ͨ¹ýÒÔÉϵÄ˵Ã÷£¬ÎÒÃ÷°×Ò»µã£¬ÄǾÍÊǼÓ
extern “C”
Í·Ò»¶¨ÊǼÓÔÚ
C++
µÄ´úÂëÎļþÖвÅÄÜÆð×÷ÓõÄ
¡£
ÏÂÃæ·ÖÎöÒ»ÏÂÕâ¸öÏÖÏóµÄʵÖÊÔÒò£º
C
±àÒëÆ÷±àÒ뺯Êýʱ²»´øº¯ÊýµÄÀàÐÍÐÅÏ¢£¬Ö»°üº¬º¯Êý·ûºÅÃû×Ö£¬Èç
C
±àÒëÆ÷°Ñº¯Êý
int a(float x)
±àÒë³ÉÀàËÆ
_a
ÕâÑùµÄ·ûºÅ£¬
C
Á¬½ÓÆ÷Ö»ÒªÕÒµ½Á˵÷Óú¯ÊýµÄ·ûºÅ£¬¾Í¿ÉÒÔÁ¬½Ó³É¹¦£¬Ëü¼ÙÉè²ÎÊýÀàÐÍÐÅÏ¢ÊÇÕýÈ·µÄ£¬ÕâÊÇ
C
±àÒëÁ¬½ÓÆ÷µÄȱµã¡£¶ø
C++
±àÒëÆ÷ΪÁËʵÏÖº¯ÊýÖØÔØ£¬±àÒëʱ»á´øÉϺ¯ÊýµÄÀàÐÍÐÅÏ¢£¬ÈçËû°ÑÉÏÃæµÄ
a
º¯Êý¿ÉÄܱàÒë³É
_a_float
ÕâÑùµÄ·ûºÅΪÁËʵÏÖÖØÔØ£¬×¢ÒâËü»¹ÊÇûÓдø·µ»ØÖµµÃÐÅÏ¢£¬ÕâÒ²ÊÇΪʲô
C++
²»Ö§³Ö²ÉÓú¯Êý·µ»ØÖµÀ´Çø±ðº¯ÊýÖØÔصÄÔÒòÖ®Ò»£¬µ±È»£¬º¯ÊýµÄʹÓÃÕ߶Ժ¯Êý·µ»ØÖµµÄ´¦Àí·½Ê½£¨ÈçºöÂÔ£©Ò²ÊÇÖØÒªÔÒò¡£
»ùÓÚÒÔÉÏ£¬
C
µ÷ÓÃ
C++
£¬Ê×ÏÈÐèÒªÓ÷â×°º¯Êý°Ñ¶Ô
C++
µÄÀàµÈµÄµ÷Ó÷â×°³É
C
º¯ÊýÒÔ±ã
C
µ÷Óã¬ÓÚÊÇ
extern "C"
µÄ×÷ÓÃÊÇ£ºÈñàÒëÆ÷ÖªµÀÕâ¼þÊ£¬È»ºóÒÔ
C
ÓïÑԵķ½Ê½±àÒëºÍÁ¬½Ó·â×°º¯
Ïà¹ØÎĵµ£º
¡¾ÔÐÍ¡¿
type fun( type arg1, type arg2, ...
);
¡¾ÃèÊö¡¿
Ö÷ÒªÓÃÔÚ²ÎÊý¸öÊý²»È·¶¨µÄº¯ÊýÖУ¬ÀýÈ磺printfº¯Êý¡£
¡¾Ê¹Ó÷½·¨¡¿
²Î¿¼£ºglib/manual/Add.c
#include <stdarg.h>
#include <stdio.h>
int add_em_up (int coun ......
¡¾º¯ÊýÔÐÍ¡¿
int printf(char *format...);
¡¾¹¦ÄÜÃèÊö¡¿
±ê×¼Êä³öº¯Êý¡£
¡¾¾ÙÀý¡¿
#include<stdio.h>
int main ( int argc, char *argv[] )
{
printf("hello world!!\n");
return 0;
}
¡¾ÊµÏÖ·ÖÎ ......
ÕâһϵÁÐÎÄÕÂÊÇÎÒ¹¹½¨C/C++²å¼þ¿ª·¢¿ò¼Ü¿ª·Å¿ò¼ÜµÄ¹ý³Ì¡£ºóÃæ»áÒ»Ö±¸üУ¬Ö±ÖÁ¹¹½¨Íê³É¡£
¡¶¹¹½¨×Ô¼ºµÄC/C++²å¼þ¿ª·¢¿ò¼Ü£¨Ò»£©——³õ²½ÉèÏë¡·
¡¶¹¹½¨×Ô¼ºµÄC/C++²å¼þ¿ª·¢¿ò¼Ü£¨¶þ£©——×ÜÌ幦ÄÜ¡·
¡¶
¹¹½¨×Ô¼ºµÄC/C++²å¼þ¿ª·¢¿ò¼Ü£¨Èý£©——×ÜÌå½á¹¹
¡·
¡¶¹¹½¨×Ô¼ºµÄC/C++²å¼þ¿ª·¢¿ò¼ ......
±¾ÎĵÄÄÚÈÝÊÇ Win32 API£¨ÌرðÊǽø³Ì¡¢Ï̺߳͹²ÏíÄÚ´æ·þÎñ£©µ½ POWER ÉÏ Linux µÄÓ³Éä¡£±¾ÎÄ¿ÉÒÔ°ïÖúÄúÈ·¶¨ÄÄÖÖÓ³Éä·þÎñ×îÊʺÏÄúµÄÐèÒª¡£×÷ÕßÏòÄúÏêϸ½éÉÜÁËËûÔÚÒÆÖ² Win32 C/C++ Ó¦ÓóÌÐòʱÓöµ½µÄ API Ó³Éä¡£
¸ÅÊö
Óкܶ෽ʽ¿ÉÒÔ½« Win32 C/C++ Ó¦ÓóÌÐòÒÆÖ²ºÍǨÒƵ½ pSeries ƽ̨¡£Äú¿ÉÒÔʹÓÃÃâ·ÑÈí¼þ»òÕßµÚÈý·½¹¤¾ßÀ´ ......
VCÖÐÏÂÃ漸¸ö½á¹¹Ìå´óС·Ö±ðÊǶàÉÙÄØ
struct MyStruct
{
double m4;
char m1;
int m3£»
};
struct MyStruct {
char m1;
double m4;
int m3;
};
#pragma pack(push)   ......