C和C++混编2
来源:互联网 发布:科赫兄弟 知乎 编辑:程序博客网 时间:2024/04/30 12:59
例如1:
//test.c
int fun(int a, int b)
{
returna+b;
}
//main.cpp
#include <stdio.h>
#include "test.c"
int main()
{
printf("%d\n", fun(10, 11));
return0;
}
命令:g++ -o main main.cpp 即可得到可执行文件
例如2:
//test.c
int fun(int a, int b)
{
returna+b;
}
//test.h
int fun(int, int);
//main.cpp
#include <stdio.h>
#include "test.h"
int main()
{
printf("%d\n", fun(10, 11));
return0;
}
编译方法:
g++ -c test.c //生成test.o
g++ -c main.cp //生成main.o
g++ -o maintest.o main.o //链接,生成main可执行文件
例如3:
//test.c
int fun(int a, int b)
{
returna+b;
}
//test.h
#ifdef __cplusplus
extern "C"{
#endif
int fun(int, int);
#ifdef __cplusplus
}
#endif
//main.cpp
#include <stdio.h>
#include "test.h"
int main()
{
printf("%d\n", fun(10, 11));
return0;
}
编译方法:
gcc -c test.c //生成test.o
g++ -c main.cpp //生成main.o
g++ -o main test.o main.o //链接,生成main可执行文件
例如4:
//test.c
int fun(int a, int b)
{
return a+b;
}
//test.h
int fun(int, int);
//main.cpp
#include <stdio.h>
extern "C"
{
#include "test.h"
}
int main()
{
printf("%d\n", fun(10, 11));
return0;
}
编译方法:
gcc -c test.c //生成test.o
g++ -c main.cpp //生成main.o
g++ -o main test.o main.o //链接,生成main可执行文件
例如5:
//test.c
int fun(int a, int b)
{
returna+b;
}
//test.h
int fun(int, int);
//main.cpp
#include <stdio.h>
extern "C" int fun(int, int)
int main()
{
printf("%d\n", fun(10, 11));
return0;
}
编译方法:
gcc -c test.c //生成test.o
g++ -c main.cpp //生成main.o
g++ -o main test.o main.o //链接,生成main可执行文件
- C和C++混编2
- OC和C混编
- C++和Objective-C混编
- matlab和C如何混编
- C++和Objective-C混编
- Objective-C和C++混编
- C++和Objective-C混编
- C++和Objective-C混编
- C++和Objective-C混编
- C和C++混编1
- Objective-C和C++混编
- C++和Objective-C混编
- Objective-C和swift混编
- C++和C语言混编
- matlab和C如何混编
- Swift和Objective-C混编
- object-c和swift 混编
- C和C++混编,c调用C++,c++调用C
- 女王大人的水题
- web压力测试--Jmeter与Badboy的结合
- java实现判断一个数是否为素数
- step into uml
- js实践篇:例外处理Try{}catch(e){}
- C和C++混编2
- Newtonsoft.Json
- 尼克的工作安排
- php的截取字符串函数msubstr使用
- 使用htmlhelper直接在View中实现时间做差
- 编程之美:饮料供货
- primefaces4 翻页时更新分页footer,totalRecords的更新
- hdu 1242 Rescue
- 区间调度