• <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>

            攀升·Uranus


            Something Different,Something New
            數據加載中……

            (轉)C++程序調用C函數

            轉自:http://blog.csdn.net/ustcgy/archive/2009/12/23/5063082.aspx

            這種需求很多,又因為C++和C是兩種完全不同的編譯鏈接處理方式,所以要稍加處理.總結大致有兩大類實現方法.

            文中給出的是完整的,具體的,但又最基本最簡單的實現,至于理論性的東西在網上很容易搜索的到.

            一.通過處理被調用的C頭文件

            a.h:

            #ifndef __A_H
            #define __A_H

            #ifdef __cplusplus
            extern "C" {
            #endif

            int ThisIsTest(int a, int b);

            #ifdef __cplusplus
            }
            #endif

            #endif

            a.c:

            #include "a.h"

            int ThisIsTest(int a, int b) {
              return (a + b);
            }

            aa.h:

            class AA {
              public:
                  int bar(int a, int b);
            };

            aa.cpp:

            #include "a.h"
            #include "aa.h"
            #include "stdio.h"

            int AA::bar(int a, int b){
                printf("result=%d\n", ThisIsTest(a, b));
                return 0;
            }

            main.cpp:

            #include "aa.h"

            int main(int argc, char **argv){
              int a = 1;
              int b = 2;
              AA* aa = new AA();
              aa->bar(a, b);
              delete(aa);
              return(0);
            }

            Makefile:

            all:
                    gcc -Wall -c a.c -o a.o
                    gcc -Wall -c aa.cpp -o aa.o
                    gcc -Wall -c main.cpp -o main.o
                    g++ -o test *.o

            二. 通過處理調用的C++文件

            恢復a.h文件為一般性C頭文件,在aa.cpp文件中extern包含a.h頭文件或函數.

            a.h:

            #ifndef __A_H
            #define __A_H
            int ThisIsTest(int a, int b);
            #endif

            aa.cpp:

            extern "C"
            {
                #include "a.h"
            }
            #include "aa.h"
            #include "stdio.h"

            int AA::bar(int a, int b){
                printf("result=%d\n", ThisIsTest(a, b));
                return 0;
            }

            or

            aa.cpp:

            #include "aa.h"
            #include "stdio.h"

            extern "C"
            {
                int ThisIsTest(int a, int b);
            }

            int AA::bar(int a, int b){
                printf("result=%d\n", ThisIsTest(a, b));
                return 0;
            }

            posted on 2010-03-08 15:23 攀升 閱讀(3027) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            久久国产精品99久久久久久老狼 | 97久久精品人人澡人人爽| 久久99国产精品尤物| 伊人热人久久中文字幕| 久久丝袜精品中文字幕| 久久久久久久亚洲Av无码| 狠狠综合久久综合中文88| 亚洲精品乱码久久久久久蜜桃图片| 狠狠色丁香久久婷婷综合五月 | 日产精品99久久久久久| 青草影院天堂男人久久| 国产aⅴ激情无码久久| 久久高清一级毛片| 日韩AV无码久久一区二区| 久久精品免费网站网| 久久se精品一区二区| 浪潮AV色综合久久天堂| 亚洲午夜无码AV毛片久久| 国产成人香蕉久久久久| 久久久久亚洲AV无码永不| 亚洲精品无码专区久久久| 武侠古典久久婷婷狼人伊人| 国产精品久久久久9999| 无码人妻久久一区二区三区| 日本WV一本一道久久香蕉| 久久九色综合九色99伊人| 91亚洲国产成人久久精品网址| 国内精品久久久久久99| 久久精品欧美日韩精品| 久久精品国产亚洲av日韩| 久久久久亚洲精品天堂| 久久亚洲AV成人无码电影| 国产美女亚洲精品久久久综合| 久久夜色精品国产亚洲| A级毛片无码久久精品免费| 欧美精品乱码99久久蜜桃| 久久久久免费精品国产| 亚洲精品国产字幕久久不卡| 色婷婷综合久久久久中文一区二区| 久久精品人妻中文系列| 久久久久人妻一区二区三区vr|