stm32 c和c++混合开发 头文件改变

#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
}

#endif

每个头文件都用这个宏来定义extern "c"

仙人掌笔记:实验平台是keilMDK,处理器是stm32

--------------cppfile.cpp---------------

extern "C"  //声明{}中的函数是在c和c++混合使用的函数

{

extern void cpp_fun(void);//测是时发现,可以加extern 也可以不加extern,此处添加了extern外部声明

extern void c_fun(void );//测是时发现,可以加extern 也可以不加extern,此处添加了extern外部声明

extern  void SPI2_IRQHandler(void) ;//是cpp函数,要在汇编启动代码中调用,测是时发现,可以加extern 也可以不加extern,此处添加了extern外部声明

}

void cpp_fun(void)

{

c_fun();

}

void SPI2_IRQHandler(void) {}


--------------------cfile.c---------------

extern void cpp_fun(void);//像c文件的函数一样声明cpp_fun

void c_fun(void ){}

void c_fun2(void )

{

cpp_fun();

}

总结:属于需要c/c++混合编程的函数,都要在C++的文件*.cpp中用extern "C" {}进行声明,extern "C" {}是c++的语法,不是c的语法,因此在c文件中出现extern "C" {}的字样是错误的,当然如果头文件中含有extern "C" {}字样,然后这个头文件被*.C文件包含(#include "*.h"),编译这个*.c文件也是会出错的.怎么在c和cpp文件中包含同一个.h头文件而都不出错呢?方法很简单,看示例:

------------------c_cpp.h----------

#ifndef c_cpp__H_

#define c_cpp__H_

#ifdef __cplusplus
extern "C" {
#endif

extern void cpp_fun(void);//测是时发现,可以加extern 也可以不加extern,此处添加了extern外部声明

extern void c_fun(void );//测是时发现,可以加extern 也可以不加extern,此处添加了extern外部声明

extern  void SPI2_IRQHandler(void) ;//是cpp函数,要在汇编启动代码中调用,测是时发现,可以加extern 也可以不加extern,此处添加了extern外部声明


#ifdef __cplusplus
}

#endif

#endif

-----------------------------------

然后再cfile.c和cppfile.cpp中都包含这个头文件就行了

--------------cppfile.cpp---------------

#include "c_cpp.h"

void cpp_fun(void)

{

c_fun();

}

void SPI2_IRQHandler(void) {}



--------------------cfile.c---------------

#include "c_cpp.h"

void c_fun(void ){}

void c_fun2(void )

{

cpp_fun();

}





http://www.jb51.net/article/37386.htm

一、extern“C”的作用(最重点)

    1. extern "C"的真实目的是实现类CC++的混合编程extern “C”是由C++提供的一个连接交换指定符号,用于告诉C++这段代码是C函数extern “C”后面的函数不使用的C++的名字修饰,而是用C。这是因为C++编译后库中函数名会变得很长,与C生成的不一致,造成C++不能直接调用C函数。

    2.C++语言支持函数重载,C语言不支持函数重载。函数被C++编译后在库中的名字与C语言的不同。假设某个函数的原型为:void foo(int x, int y);该函数被C编译器编译后在库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字。C++提供了C连接交换指定符号extern“C”来解决名字匹配问题。

    3.extern "C"限定的函数或变量是extern类型的;externC/C++语言中表明函数和全局变量作用范围(可见性)的关键字,该关键字告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。被extern "C"修饰的变量和函数是按照C语言方式编译和连接的。   

     4.与extern对应的关键字是static,被它修饰的全局变量和函数只能在本模块中使用。因此,一个函数或变量只可能被本模块使用时,其不可能被extern “C”修饰。

二、extern“C”__cplusplus

#ifdef __cplusplus
       extern "C" {
       #endif
       #ifdef __cplusplus
       }
       #endif
    CplusplusC plus plus"C++"用于C++文档的头文件中,上面代码的意思是:如果是C++文件(*.cpp)后缀,则使用extern “C”,在C++项目中应用的非常广泛。即使用gcc编译器编译,函数名为C类型如_foo。个人认为,搞懂了这两个关键字,尤其是理解extern "C"(再次强调,不为过,呵呵),接下来的混合编程也就差不多了,哈哈哈。。。。

三、C调用C++函数(接口)

1.设计程序,共四个文件

animal.cpp animal.h  main.c Makefile

1.1 animal.h

[root@localhost CC++]#cat animal.h
#ifndef __ANIMAL_H__  //防止被重复包含
#define __ANIMAL_H__
#ifdef __cplusplus
extern "C" {
#endif

class ANIMAL{
public:
        ANIMAL(char* );
        ~ANIMAL();
        char* getname(void);
private:
        char* name;
};
void print(void);
#ifdef __cplusplus
}
#endif
#endif  // __ANIMAL_H__

1.2 animal.cppC++文件

[root@localhost CC++]#cat animal.cpp
#include "animal.h"
#include <iostream>
using namespace std;
ANIMAL::ANIMAL(char* data)//构造函数
{       name = new char[64];
        strcpy(name, data);
}
ANIMAL::~ANIMAL() //析构函数

{
        if(name)
        {

                delete[] name;
                name = NULL;
        }
}
char* ANIMAL::getname(void)
{        return name;
}
void print(void) //对外接口,而且必须有一个非类中方法,才能被C调用
{
        ANIMAL animal("dog");
        char* animal_name = animal.getname();
        cout << "animal name is :" << animal_name << endl;
}

1.3 main.cC文件

[root@localhost CC++]#cat main.c
int main(void)

{       print();
        return 0;
}

1.4 Makefile

[root@localhost CC++]#cat Makefile
main:main.c animal.o
        gcc -lstdc++ main.c animal.o -o main

animal.o:animal.h
        g++ -c animal.cpp
.PHONY : clean
clean:
        -rm animal.o main

2.测试

2.1生成可执行程序main

[root@localhost CC++]#make
g++ -c animal.cpp
gcc -lstdc++ main.c animal.o -o main

2.2运行可执行程序main

[root@localhost CC++]# ./main
animal name is :dog

四、C++调用C函数

应该这个比较简单,我就不多写了,就直接写代码。
共有三个文件:1.h 1.c  main.cpp
[root@localhost aa]#cat 1.h
#ifndef _1__H_
#define _1__H_
extern void print(char* );
#endif

[root@localhost aa]#cat 1.c
#include <stdio.h>
#include "1.h"
void print(char* data)
{
        printf("%s\n",  data);
}
[root@localhost aa]#cat main.cpp

extern "C"{
#include "1.h"}
int main(void)
{
        print(“hello,world\n”);
        return 0;

}
gcc –c 1.c
g++ main.cpp 1.o

    接着./a.out,又可以出现我们神奇的hello,world了,C++调用C代码很简单,但C调用C++接口可把我给累坏了,苦啊。就是这个gcc后面跟的-lstdc++害的,出现undefined reference to `__gxx_personality_v0'这个错误。是因为你用gcc编译.cpp文件(animal.cpp.按系统默认.cpp文件是c++的文件格式。当然,混搭时,我还遇到了其他的一些问题,都是一些小问题,如果上面解释的还不足以让你解决C\C++混合编程的问题,可以联系我哦.


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值