如何混合编译C语言和C++
2017-05-24 15:56
141 查看
如何混合编译C语言和C++
实际开发过程中,C++中会调用C与语言编写的代码,我在网络上面找到一篇写得很好的文章
http://blog.csdn.net/keensword/article/details/401114
就着上面的例子,我使用C语言采用两种方法重写了一下。
方法一、全局函数和变量在devVar.c文件中实现,在extern.cpp文件中使用extern关键字声明在devVar.c文件中定义的函数和变量。
devVar.c文件的代码如下所示:
#include <stdio.h>
int i = 1; void func() { printf("%d",i++); }
extern.cpp文件中代码如下所示:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
//#include "devVar.h"
//extern int i;
//extern void func();
extern "C"
{
extern int i;
extern void func();
//#include "devVar.h"
}
int main(void)
{
for (int x = 0;x < 10; x++)
{
func();
}
} 所以在C++文件中编译C文件需要使用extern "C"关键字,声明语法如下所示
extern "C"
{
采用C语言实现的内容
}
方法二、
在devVar.h文件中实现C代码(即devVar.h作为C语言头文件),在.cpp文件中包含C语言头文件。
devVar.h头文件内容为:
extern.cpp文件内容如下所示
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
//#include "devVar.h"
//extern int i;
//extern void func();
extern "C"
{
//extern int i;
//extern void func();
#include "devVar.h"
}
int main(void)
{
for (int x = 0;x < 10; x++)
{
func();
}
} 其中,包含C语言头文件的方式为:
extern "C"
{
//extern int i;
//extern void func();
#include "devVar.h"
}写到这里,楼主又产生了一个疑问,上面的例子讲的是C++调用C实现的代码,那如果是C调用C++编写的代码呢?
楼主作了如下改动:
devVar.cpp代码为:
单独编译每个文件都通过,链接声称可执行文件的时候报错:
1>extern.obj : error LNK2019: unresolved external symbol _func referenced in function _main,说明.c文件中extern void func(),按照C编译的规则,得到函数_func,而devVar.cpp文件采用C++编译方式,得到的函数为XX·!_func(具体楼主也不知道哈),这样链接的时候函数自然找不到,那怎么解决呢?
需要在devVar.cpp中,明确调用extern "C"关键字,声明cpp文件中有关代码,需要按照C的方式来生成,修改devVar.cpp文件如下所示:
此时,除了需要使用extern "C"声明编译的时候采用C方式编译外,.cpp文件中的代码可以按照C++方式编写,例如
devVar.cpp按照下面方式写,也是正确的。
原文地址:http://blog.csdn.net/sruru/article/details/7951019
实际开发过程中,C++中会调用C与语言编写的代码,我在网络上面找到一篇写得很好的文章
http://blog.csdn.net/keensword/article/details/401114
就着上面的例子,我使用C语言采用两种方法重写了一下。
方法一、全局函数和变量在devVar.c文件中实现,在extern.cpp文件中使用extern关键字声明在devVar.c文件中定义的函数和变量。
devVar.c文件的代码如下所示:
#include <stdio.h>
int i = 1; void func() { printf("%d",i++); }
extern.cpp文件中代码如下所示:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
//#include "devVar.h"
//extern int i;
//extern void func();
extern "C"
{
extern int i;
extern void func();
//#include "devVar.h"
}
int main(void)
{
for (int x = 0;x < 10; x++)
{
func();
}
} 所以在C++文件中编译C文件需要使用extern "C"关键字,声明语法如下所示
extern "C"
{
采用C语言实现的内容
}
方法二、
在devVar.h文件中实现C代码(即devVar.h作为C语言头文件),在.cpp文件中包含C语言头文件。
devVar.h头文件内容为:
#include <stdio.h> int i = 1; void func() { printf("%d",i++); }
extern.cpp文件内容如下所示
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
//#include "devVar.h"
//extern int i;
//extern void func();
extern "C"
{
//extern int i;
//extern void func();
#include "devVar.h"
}
int main(void)
{
for (int x = 0;x < 10; x++)
{
func();
}
} 其中,包含C语言头文件的方式为:
extern "C"
{
//extern int i;
//extern void func();
#include "devVar.h"
}写到这里,楼主又产生了一个疑问,上面的例子讲的是C++调用C实现的代码,那如果是C调用C++编写的代码呢?
楼主作了如下改动:
devVar.cpp代码为:
int i = 1; void func() { printf("%d",i++); }extern.c文件代码为
#include <stdio.h> extern int i; extern void func(); int main(void) { int x = 0; for (;x < 10; x++) { func(); } }
单独编译每个文件都通过,链接声称可执行文件的时候报错:
1>extern.obj : error LNK2019: unresolved external symbol _func referenced in function _main,说明.c文件中extern void func(),按照C编译的规则,得到函数_func,而devVar.cpp文件采用C++编译方式,得到的函数为XX·!_func(具体楼主也不知道哈),这样链接的时候函数自然找不到,那怎么解决呢?
需要在devVar.cpp中,明确调用extern "C"关键字,声明cpp文件中有关代码,需要按照C的方式来生成,修改devVar.cpp文件如下所示:
#include <stdio.h> int i = 1; extern "C" void func() { printf("%d",i++); }
此时,除了需要使用extern "C"声明编译的时候采用C方式编译外,.cpp文件中的代码可以按照C++方式编写,例如
devVar.cpp按照下面方式写,也是正确的。
#include "stdafx.h" #include <iostream> using namespace std; int i = 1; extern "C" void func() { cout << "i = " << i++ << endl; }
原文地址:http://blog.csdn.net/sruru/article/details/7951019
相关文章推荐
- C++下混合编译c语言方法总结
- linux下c程序c++程序混合编译,c程序中调用c++程序,c语言项目与c++语言项目的合并
- 如何在 Visual Studio 中混合编译C# 和 C++代码,生成单一的DLL?
- ubuntu使用技巧——如何编译C++和C语言的使用程序
- 如何在 Visual C++.NET 2003 下编译eMule
- c语言编译流程(c语言是如何变成可执行文件的)
- 如何混合使用C和C++(上)
- 如何混合使用C和C++(下)
- [C++] 如何在 C++ Builder 3.0 下编译含有 MFC 的程序?
- 如何用vs编译c语言,compiler error C1853
- 如何在编译时刻判断两个类型是否可以自动转换?——《Modern C++ Design》读书笔记(1)
- 如何混合使用C和C++ (转)
- C++中在多个编译单元中如何定义常量
- 如何判断程序是由C编译程序编译的,还是由C++编译程序编译的.
- 如何用c语言调用c++做成的动态链接库
- 如何解决"ANSI C++ forbids data member `ip_opts' with same name as enclosing class"的编译错误
- C++中频繁引用的类如何避免不必要的重编译
- C/C++混合编译Makefile终于完全Debug了
- 也谈如何在C语言中巧用正则表达式,并且如何将其转化成C++的形式