zoukankan      html  css  js  c++  java
  • extern “C”浅析

    以下在Linux下测试:

    c函数: void MyFunc(){} ,被编译成函数: MyFunc

    c++函数: void MyFunc(){},被编译成函数: _Z6Myfuncv

    通过这个测试,由于c++中需要支持函数重载,所以c和c++中对同一个函数经过编译后生成的函数名是不相同的,这就导致了一个问题,如果在c++中调用一个使用c语言编写模块中的某个函数,那么c++是根据c++的名称修饰方式来查找并链接这个函数,那么就会发生链接错误,以上例,c++中调用MyFunc函数,在链接阶段会去找Z6Myfuncv,结果是没有找到的,因为这个MyFunc函数是c语言编写的,生成的符号是MyFunc。

    那么如果我想在c++调用c的函数怎么办?

    extern "C"的主要作用就是为了实现c++代码能够调用其他c语言代码。加上extern "C"后,这部分代码编译器按c语言的方式进行编译链接,而不是按c++的方式。

    MyModule.h

    #ifndef MYMODULE_H
    #define MYMODULE_H
    
    #include<stdio.h>
    
    #if __cplusplus
    extern "C"{
    #endif
    
        void func1();
        int func2(int a,int b);
    
    #if __cplusplus
    }
    #endif
    
    #endif

    MyModule.c

    #include"MyModule.h"
    
    void func1(){
        printf("hello world!");
    }
    int func2(int a, int b){
        return a + b;
    }

    TestExternC.cpp

    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    using namespace std;
    
    #if 0
    
        #ifdef __cplusplus
        extern "C" {
            #if 0
                void func1();
                int func2(int a, int b);
            #else
                #include"MyModule.h"
            #endif
        }
    
        #endif
    
    #else
    
        extern "C" void func1();
        extern "C" int func2(int a, int b);
    
    #endif
    
    int main(){
        func1();
        cout << func2(10, 20) << endl;
        return EXIT_SUCCESS;
    }
  • 相关阅读:
    centos6.3 配置防火墙,开启80端口、3306端口
    Unable to run man pages on Centos 6
    1. 信息系统基础知识
    2. 软件工程
    CommonJs
    软考资料
    Node.js资料
    qyqt5(一)
    tf源码中的object_detection_tutorial.ipynb文件
    分类结果的评价指标
  • 原文地址:https://www.cnblogs.com/mmc9527/p/10429743.html
Copyright © 2011-2022 走看看