zoukankan      html  css  js  c++  java
  • C++

    我们拿在Qt项目中加入纯C语言写的代码文件来举例

    问题

    在Qt项目中如果加入纯C语言写的代码文件后,Qt工程就会无法编译。

    解决方法

    在纯C语言写的代码文件的头文件中加入以下内容即可

    #pragma once
    //C++ 运行该文件时,extern C包含的内容用C语言方式连接
    #ifdef __cplusplus
       extern "C"{
    #endif 
     
    //C代码内容所在位置
     
    #ifdef __cplusplus
    }
    #endif 

    实例

    纯C语言写的代码文件为:test.h,test.c

    其中test.c文件内容为:

    #include "test.h"
    #include <stdio.h>
    
    int add(int a,int b)
    {
        return a+b;
    }

    则test.h文件内容应为:

    #ifndef TEST_H
    #define TEST_H
    
    //C++ 运行该文件时,extern C包含的内容用C语言方式连接
    #ifdef __cplusplus
       extern "C"{
    #endif 
     
    #include <stdio.h>
    int add();
     
    #ifdef __cplusplus
    }
    #endif

    #endif
    博客园文作者:Citrusliu 博文地址:https://www.cnblogs.com/citrus
  • 相关阅读:
    PHP引用传值
    PHP快速排序
    PHP冒泡排序法
    Jquery EasyUI datagrid后台数据表格生成及分页详解
    polymer 测试
    imooc movie
    test markdown
    MEAN 27
    MEAN 26
    nodejs 负载均衡
  • 原文地址:https://www.cnblogs.com/citrus/p/13322365.html
Copyright © 2011-2022 走看看