• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            martin

            thinking

            常用鏈接

            統(tǒng)計(jì)

            software

            最新評(píng)論

            C/C++混合編程

            前段時(shí)間,碰到了C,C++混合編程的需求,經(jīng)過(guò)努力,順利解決問(wèn)題.現(xiàn)把這方面的知識(shí)做一下簡(jiǎn)單的總結(jié):
             
            1.當(dāng)C++文件要用到C語(yǔ)言中的函數(shù)代碼時(shí),采用下屬方法即可:
            在C++中的.h文件或.cpp文件中加入下列代碼,
            #define LINT_ARGS 1
            extern "C" {
            #include "system.h"
            }
             
            然后在代碼中直接調(diào)用這些函數(shù)即可.

            注解:
            1.1 LINT_ARGS 1表示在檢查C語(yǔ)言中的函數(shù)原型時(shí),要對(duì)函數(shù)原型的參數(shù)進(jìn)行檢查. 
            1.2. "{ }" 中包含的頭文件為C語(yǔ)言中的頭文件.
            1.3.extern "C" 告訴鏈接器,這些頭文件中的函數(shù)被當(dāng)做C語(yǔ)言中的函數(shù)來(lái)處理.
             
            下面以一個(gè)實(shí)例加以說(shuō)明:
            下面為一個(gè)C語(yǔ)言的頭文件(sysgct.h):
            #ifdef LINT_ARGS
              int  GCT_send(unsigned int task_id, HDR *h);
              ......
            #else
              int  GCT_send();
              ......
            #endif
            ~
            in file MapBaseReq.cpp 文件中
            #include ....
            extern "C"
            {
            #include "sysgct.h"
            }
            void
            MapBaseReq::sendPdu(const BasePdu_var& thePduP)
            {
                ...
                if (GCT_send(m->hdr.dst, (HDR *)m) != 0)
                    {
                            relm((HDR *)m);
                            SETERR1(errSWErrorMsg, "sendPdu(): GCT_send() Failed");
                    }
               ...
            }
             
            2.當(dāng)C文件要用到C++語(yǔ)言某個(gè)類(lèi)中的方法時(shí),可以采用下列方法:
            2.1 在cpp文件中用下列方式定義函數(shù):
            extern "C" returnType FunName(parameters list).
             
            2.2 然后在相應(yīng)的頭文件中進(jìn)行聲明:
            extern returnType FunName(parameters list);
             
            2.3 在相應(yīng)的.c文件中包含該頭文件,然后直接利用相應(yīng)的函數(shù)即可.
             
            下面給出實(shí)例.
             
            2.4 cpp文件

            #include <iostream>
            #include <iomanip>
            #include "TTDebug.h"
            using namespace std;

            extern "C"
            {
            #include "Utility.h"
            }

            static int display_hex_buffer(unsigned char* buffer, unsigned char* ptr,int len);

            extern "C" void tDebug_traceFunc(int level, char* trace)
            {
                    TDEBUG_TRACEFUNC(level,trace);
            }

            extern "C" void nDebug(int level, unsigned char* args, int iLen, int cid)
            {
                    unsigned char buf[512];
                    if(0 != TTDebug::instance() && TTDebug::instance()->isTTDebugOn(level))
                    {
                            /* Check whether the current thread already holds the mutex lock */
                            LockGuard<MutexWrapper> guard(TTDebug::instance()->mutex());
                            TTDebug::instance()->traceStart(level, __FILE__, __LINE__);
                            memset(buf,0,512);
                            display_hex_buffer(buf,args,iLen);
                            TTDebug::instance()->outStream() << "Send Msg(0-0x" << setw(4) << setfill('0') << hex << cid <<"):0x" << buf;
                            TTDebug::instance()->traceEnd();
                    }
            }

            2.5 .h 文件

            #ifndef __UTILITY_H
            #define __UTILITY_H

            extern void tDebug_traceFunc(int level, char* trace);
            extern void nDebug(int level, unsigned char* args,int iLen, int cid);

            #endif
             
            2.6 cpp文件中定義的函數(shù)在c文件中調(diào)用實(shí)例
            在test.c文件中:

            ...

            int ctu_ent(msg,pInt)
              MSG* msg;
              int *pInt;
            {

             tDebug_traceFunc(10,"ctu ctu_ent");

              HDR *h;
              MSG *m;

               ...

            }

            ...


            posted on 2009-03-03 16:25 martin_yahoo 閱讀(4815) 評(píng)論(3)  編輯 收藏 引用

            評(píng)論

            # re: C/C++混合編程 2009-03-03 20:44 夢(mèng)在天涯

            不錯(cuò)!很有用
            在《C++編程思想》中有提到
            extern "C"
            {
            #include "Utility.h"
            }!  回復(fù)  更多評(píng)論   

            # re: C/C++混合編程 2009-03-05 22:46 cdy20


            c+上c頭文件名,也可以
            比如#include<cstdio>........  回復(fù)  更多評(píng)論   

            # re: C/C++混合編程[未登錄](méi) 2009-03-05 23:20 martin_yahoo

            @cdy20
            只不過(guò)是在c頭文件中已經(jīng)加上了如下類(lèi)似的語(yǔ)句:
            #ifdef _c_plus_plus
            extern "C"{
            #endif
            .....
            #ifdef _c_plus_plus
            }
            #endif

            如果在c語(yǔ)言的頭文件中加上述語(yǔ)句, 就應(yīng)象采用隨筆中提到的做法.
              回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            99久久精品国产麻豆| 国内高清久久久久久| 国产精品成人99久久久久91gav| 久久91精品国产91久久麻豆| 大香网伊人久久综合网2020| 欧美亚洲日本久久精品| 人妻少妇久久中文字幕一区二区| 国产精品天天影视久久综合网| 久久九色综合九色99伊人| 久久无码中文字幕东京热| 亚洲国产天堂久久综合网站| 久久99这里只有精品国产| 久久精品国产91久久综合麻豆自制| 日韩中文久久| 狠狠色丁香久久婷婷综| 中文字幕无码久久精品青草 | 久久精品亚洲男人的天堂| 久久久久亚洲av成人网人人软件| 精品久久久久久亚洲精品| 久久精品国产男包| 青青草原1769久久免费播放| 国产成人精品久久| 久久免费视频6| 亚洲国产精品人久久| 久久精品亚洲一区二区三区浴池 | 久久青青国产| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 亚洲国产精品久久| 久久精品一本到99热免费| 久久亚洲sm情趣捆绑调教| 久久久人妻精品无码一区| 亚洲国产精品热久久| 久久99毛片免费观看不卡 | 精品综合久久久久久97| 久久精品国产亚洲Aⅴ蜜臀色欲| 狠狠狠色丁香婷婷综合久久五月| 久久婷婷激情综合色综合俺也去| 综合久久精品色| 合区精品久久久中文字幕一区| 久久久久国产精品嫩草影院| 精品无码久久久久久国产|