在C ++程序中使用C函数 [英] Using C functions in a C++ program

查看:62
本文介绍了在C ++程序中使用C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一位同事是C开发人员在C

中编写了几个函数,我现在需要在我的C ++应用程序中调用它们。这通常不是问题,除了他写的头文件还包含几个结构和#defines,我需要在我的C ++中使用

应用程序。


在不破坏编译器/链接器的情况下,使用我的代码中的C函数,结构和定义的最佳方法是什么?


对于C函数,我知道我需要使用externC。 {...}。但是如何将b / b
包含在结构和定义中呢?如果我在我的C ++代码中包含

头文件,则链接器将失败。


非常感谢任何帮助。

谢谢。

A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?

For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.

Any help is greatly appreciated.

Thanks.

推荐答案



ruffiano写道:

ruffiano wrote:

我的一位同事是C开发人员在C

中编写了几个函数,我现在需要在我的C ++应用程序中调用它们。这通常不是问题,除了他写的头文件还包含几个结构和#defines,我需要在我的C ++中使用

应用程序。


在不破坏编译器/链接器的情况下,使用我的代码中的C函数,结构和定义的最佳方法是什么?


对于C函数,我知道我需要使用externC。 {...}。但是如何将b / b
包含在结构和定义中呢?如果我在我的C ++代码中包含

头文件,则链接器将失败。
A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?

For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.



您可以在命名空间中嵌入C函数和结构:


命名空间CFunction {

#include" CHeader.h"


};


现在你将它们称为CFunctions :: function( )等等,它不会用你已经拥有的名字来破坏你的全局命名空间。


你也可以使用预处理器来取消定义并重新定义你的

宏需要的地方。我不知道push / pop和undef是否标准

但它们相当常见。

You can embed the C functions and structures in a namespace:

namespace CFunctions {

#include "CHeader.h"

};

Now you refer to them as CFunctions::function() and such and it won''t
clobber your global namespace with names you already have.

You can also use preprocessor stuff to undefine and redefine your
macros where needed. I don''t know if push/pop and undef are standard
but they are rather common.


ruffiano发布:
ruffiano posted:

我的一位同事是C开发人员在C

中编写了几个函数,我现在需要在我的C ++应用程序中调用它们。这通常不是问题,除了他写的头文件还包含几个结构和#defines,我需要在我的C ++中使用

申请。
A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.



您将拥有以下文件:


apple.hpp

table。 hpp


apple.cpp

table.cpp

disk.cpp


您同事的源文件的扩展名应为.c。 - 大多数

编译器认为它应该被编译成C.确保你通过编译器选项确定

,特别注意

是否编译为C89或C99。


You''ll have your files like:

apple.hpp
table.hpp

apple.cpp
table.cpp
disk.cpp

Your colleague''s source files should have the extension ".c" -- most
compilers take this to mean that it should be compiled as C. Make sure though
that you go through the compiler options, paying specific attention to
whether it''s compiled as C89 or as C99.


使用C函数,结构和c的最佳方法是什么在没有扰乱编译器/链接器的情况下定义了我的代码?

对于C函数我知道我需要使用externC {...}。但是如何将b / b
包含在结构和定义中呢?如果我在我的C ++代码中包含

头文件,则链接器将失败。
What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?
For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.



为什么链接器会失败?


-


Frederick Gotham


Why does the linker fail?

--

Frederick Gotham


Noah Roberts写道:
Noah Roberts wrote:

ruffiano写道:
ruffiano wrote:

>>我的一位同事是C开发人员在C
中编写了几个函数,我现在需要在我的C ++应用程序中调用它们。这通常不是问题,除了他写的头文件还包含几个结构和#defines,我需要在我的C ++
应用程序中使用它。

对于C函数,我知道我需要使用extern ; C" {...}。但是我如何包含结构和定义呢?如果我在我的C ++代码中包含
头文件,那么链接器将失败。
>>A colleague of mine who is a C developer wrote several functions in C
which I now need to invoke in my C++ application. This is normally not
a problem except that the header file that he wrote contains also
several structures and #defines, which I need to use in my C++
application.

What is the best way to use the C functions, structures and defines in
my code without upsetting the compiler / linker?

For the C functions I know I need to use extern "C" { ...}. But how do
I get to include the structures and the defines? If I include the
header file in my C++ code, then the linker will fail.




您可以在命名空间中嵌入C函数和结构:


namespace CFunctions {


#include" CHeader.h"


};


现在你将它们称为CFunctions :: function()等等,它不会用你已经拥有的名字来破坏你的全局命名空间。



You can embed the C functions and structures in a namespace:

namespace CFunctions {

#include "CHeader.h"

};

Now you refer to them as CFunctions::function() and such and it won''t
clobber your global namespace with names you already have.



然后他们将无法链接,假设C代码是使用

C编译器编译的,所有符号将是externC,而不是C ++损坏的名称。


即使C代码是用C ++编译器编译的,你也必须有条件地包含C ++编译的命名空间声明。


更好的关闭把externC放在一起包括标题,或者

最好还是有条件地将标题包含在标题中。


-

Ian Collins 。

But then they will fail to link, assuming the C code is compiled with a
C compiler, all the symbols will be extern "C", not C++ mangled names.

Even if the C code is compiled with the C++ compiler, you will have to
conditionally include the namespace declaration for C++ compilation.

Better off just putting extern "C" round the inclusion of the header, or
better still conditionally included the wrapper in the header.

--
Ian Collins.


这篇关于在C ++程序中使用C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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