如何把C工程转成C++工程
PHPz
PHPz 2017-04-17 13:40:28
0
2
491

我有一个包含50+ .c 和 20+ .h 的c工程,需要转成c++类以便在单个exe中产生多个实例

是否有通用的指导方法

我google了一下,想到下面几点

  1. 把.c 改为 .cpp 解决所有隐式转换的错误

  2. 删除static修饰,处理全局名字冲突

  3. 创建一个全局.h 文件,新建一个FOO类,把所有的全局函数,变量放入这个类

  4. 原先保存宏和常数的头文件以 extern "C"方式 include

  5. 所有的函数重命名为FOO::函数

PHPz
PHPz

学习是最好的投资!

reply all(2)
Peter_Zhu

Write a C++ class file to encapsulate the C code

Peter_Zhu

I have seen many projects where C++ calls C库. They all
compile C库 using C编译器 into library,
and then C++ code using the extern "C" methodincludeInterface,
then write a class to encapsulate the interface you use,
and finally link to library.

This will be easier and the correctness is guaranteed. If you change C to C++ source code, the workload will be too much and you will have to retest

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template