需要创建一个C lib - 使用C ++类 - 是否可能 [英] Need to create a C lib - using C++ classes - is it possible

查看:87
本文介绍了需要创建一个C lib - 使用C ++类 - 是否可能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


我们有很多C ++代码。我们现在需要创建一个库,可以从C和C ++中使用
。鉴于我们有很多C ++代码

使用类我们怎样才能隐藏它是来自C

编译器的C ++这个事实?


我们可以有一个C头文件,它使用C ++

文件的功能并将其编译成lib文件吗?


任何人都可以给我一些关于如何入门的建议吗?


A

Hello

We have a lot of C++ code. And we need to now create a library which
can be used from C and C++. Given that we have a lot of C++ code
using classes how can we ''hide'' the fact that it is C++ from C
compilers?

Can we have a C header file which uses the functionality of the C++
files and compile this into a lib file?

Can anyone give me some pointers as to how to get started?

A

推荐答案

只需附上C链接命名空间中的用户代码,例如:


externC {

// C代码

}
Just enclose the user code in the C linkage namespace, eg:

extern "C" {
// C code
}


Angus写道:
Angus wrote:

你好


我们有很多C ++代码。我们现在需要创建一个库,可以从C和C ++中使用
。鉴于我们有很多C ++代码

使用类我们怎样才能隐藏它是来自C

编译器的C ++这个事实?
Hello

We have a lot of C++ code. And we need to now create a library which
can be used from C and C++. Given that we have a lot of C++ code
using classes how can we ''hide'' the fact that it is C++ from C
compilers?



您必须声明库的C接口。

You will have to declare a C interface to the the library.


我们是否可以使用C头文件C ++

文件的功能并将其编译成lib文件?
Can we have a C header file which uses the functionality of the C++
files and compile this into a lib file?



只有那些合法C的代码子集你会有类似的东西:


#ifdef __cplusplus

extern" C" {

#endif


/ *您的POD类型和功能声明在这里* /


#ifdef __cplusplus

}

#endif


函数定义将编译为C ++,作为您的
$ b的一部分$ b C ++库。


-

Ian Collins。

Only that subset of the code that''s legal C. You would have something like:

#ifdef __cplusplus
extern "C" {
#endif

/* your POD types and function declarations go here */

#ifdef __cplusplus
}
#endif

The function definitions would be compiled as C++ as part of with your
C++ library.

--
Ian Collins.


sebastian写道:
sebastian wrote:

只需将用户代码包含在C链接命名空间中,例如:


externC {

// C代码

}
Just enclose the user code in the C linkage namespace, eg:

extern "C" {
// C code
}



如果代码是C并编译,则没有多大用处用C编译器...


-

Ian Collins。

Not a lot of use if the code is C and compiled with a C compiler...

--
Ian Collins.


这篇关于需要创建一个C lib - 使用C ++类 - 是否可能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆