• <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 攀升 閱讀(3035) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            精品久久人人爽天天玩人人妻| 精品久久久久久亚洲| 亚洲国产天堂久久综合| 亚洲欧美国产日韩综合久久| 国产69精品久久久久9999APGF | 久久亚洲国产成人影院| 久久精品一区二区三区AV| 久久精品国产半推半就| 狠狠色丁香婷婷久久综合五月 | 国产精品久久久久久久久久影院 | 久久免费视频网站| 久久婷婷是五月综合色狠狠| 99久久国产综合精品成人影院| 免费无码国产欧美久久18| 69SEX久久精品国产麻豆| 久久99国产精一区二区三区| 97久久久精品综合88久久| 国产69精品久久久久777| 久久精品综合网| 超级碰久久免费公开视频| 久久无码高潮喷水| 欧美日韩精品久久久久| 99久久精品国产一区二区| 久久天天躁狠狠躁夜夜avapp| 欧美亚洲另类久久综合婷婷| 久久精品国产精品国产精品污| 久久婷婷五月综合色高清 | 狠狠综合久久综合中文88| 日韩乱码人妻无码中文字幕久久| 久久午夜福利电影| 久久久久久无码国产精品中文字幕| 99精品久久精品| 韩国免费A级毛片久久| 97久久国产综合精品女不卡| 伊人久久无码精品中文字幕| 色婷婷狠狠久久综合五月| 欧美一级久久久久久久大| 久久国产香蕉一区精品| 国产精品99久久精品爆乳| 久久97久久97精品免视看秋霞| 精品久久综合1区2区3区激情|