C++调用另一个C/C++的方法

main.cpp

#include "point.c"
int main() {
 getPoint(10);
}

point.h

void getPoint(int a);

point.c

#include <stdio.h>
#include "point.h"
void getPoint(int a) {
    int *pa = &a;
    printf("pa===1===%d", pa);
    *pa = 6;
    printf("pa===2===%d", pa);
}

注意:
1.如果point.c改为cpp文件 将会报错:multiple definition of,正确的写法是抽取出point.h 在main.cpp中#include “point.h”,而在point.c中不引入#include “point.h”,只实现方法即可。

2.注意point.c中当然也可以不引入#include “point.h” ,因为getPoint已经被实现了,main中直接#include “point.c”。

3.main.cpp和point.c中都引入point.h,但是main.cpp不#include “point.c”会报:undefined reference to `getPoint(int)’

4.point.c中 #include “point.h” 引号换成<>将报错“ fatal error: point.h: No such file or directory”

5.值得探讨的是 如果point是以c文件格式引入的话,不需要提取poin.h文件也不会报multiple definition of的错误。

6.与 1 截然不同 当point为c文件的时候,我们应该在main中#include “point.h”和#include “point.c” 然后在point.c实现方法即可。

猜你喜欢

转载自blog.csdn.net/qq_20330595/article/details/82022239