<small id='1W33r'></small><noframes id='1W33r'>

        <legend id='1W33r'><style id='1W33r'><dir id='1W33r'><q id='1W33r'></q></dir></style></legend>

      1. <tfoot id='1W33r'></tfoot>

      2. <i id='1W33r'><tr id='1W33r'><dt id='1W33r'><q id='1W33r'><span id='1W33r'><b id='1W33r'><form id='1W33r'><ins id='1W33r'></ins><ul id='1W33r'></ul><sub id='1W33r'></sub></form><legend id='1W33r'></legend><bdo id='1W33r'><pre id='1W33r'><center id='1W33r'></center></pre></bdo></b><th id='1W33r'></th></span></q></dt></tr></i><div id='1W33r'><tfoot id='1W33r'></tfoot><dl id='1W33r'><fieldset id='1W33r'></fieldset></dl></div>
          <bdo id='1W33r'></bdo><ul id='1W33r'></ul>

        在 C 代码中使用 C++ 库

        时间:2023-09-19

        <small id='MwfzK'></small><noframes id='MwfzK'>

            <bdo id='MwfzK'></bdo><ul id='MwfzK'></ul>
            1. <tfoot id='MwfzK'></tfoot>
              <legend id='MwfzK'><style id='MwfzK'><dir id='MwfzK'><q id='MwfzK'></q></dir></style></legend>
                <tbody id='MwfzK'></tbody>

                • <i id='MwfzK'><tr id='MwfzK'><dt id='MwfzK'><q id='MwfzK'><span id='MwfzK'><b id='MwfzK'><form id='MwfzK'><ins id='MwfzK'></ins><ul id='MwfzK'></ul><sub id='MwfzK'></sub></form><legend id='MwfzK'></legend><bdo id='MwfzK'><pre id='MwfzK'><center id='MwfzK'></center></pre></bdo></b><th id='MwfzK'></th></span></q></dt></tr></i><div id='MwfzK'><tfoot id='MwfzK'></tfoot><dl id='MwfzK'><fieldset id='MwfzK'></fieldset></dl></div>
                  本文介绍了在 C 代码中使用 C++ 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 C++ 库,它提供了各种用于管理数据的类.我有库的源代码.

                  我想扩展 C++ API 以支持 C 函数调用,以便库可以同时与 C 代码和 C++ 代码一起使用.

                  我使用的是 GNU 工具链(gcc、glibc 等),因此语言和架构支持不是问题.

                  是否有任何理由说明这在技术上是不可能的?

                  是否有任何问题需要我注意?

                  是否有与此相关的资源、示例代码和/或文档?

                  <小时>

                  我发现的其他一些事情:

                  1. 使用以下内容来包装 C 代码需要使用的 C++ 标头.

                  #ifdef __cplusplus外部C"{#万一////代码在这里...//#ifdef __cplusplus}//外部 "C"#万一

                  1. 将真正的"C++ 接口保存在 C 不包含的单独头文件中.想想 PIMPL 原则这里.使用 #ifndef __cplusplus #error 的东西有助于在这里检测任何疯狂.
                  2. 小心将 C++ 标识符作为 C 代码中的名称
                  3. 枚举在 C 和 C++ 编译器之间的大小不同.如果您使用的是 GNU 工具链,则可能不是问题,但仍然要小心.
                  4. For structs 遵循以下形式,以免 C 混淆.

                    typedef struct X { ... } X

                  5. 然后使用指针来传递 C++ 对象,它们只需要在 C 中声明为 struct X,其中 X 是 C++ 对象.

                  所有这些都是由一位 C++ 高手朋友提供的.

                  解决方案

                  是的,这当然是可能的.您将需要用 C++ 编写一个接口层,用 extern "C" 声明函数:

                  extern "C" int foo(char *bar){return realFoo(std::string(bar));}

                  然后,您将从 C 模块调用 foo(),该模块会将调用传递给在 C++ 中实现的 realFoo() 函数.>

                  如果您需要公开具有数据成员和方法的完整 C++ 类,那么您可能需要做比这个简单的函数示例更多的工作.

                  I have a C++ library that provides various classes for managing data. I have the source code for the library.

                  I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

                  I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

                  Are there any reasons why this is technically not possible?

                  Are there any gotcha's that I need to watch out for?

                  Are there resources, example code and/or documentation available regarding this?


                  Some other things that I have found out:

                  1. Use the following to wrap your C++ headers that need to be used by C code.

                  #ifdef __cplusplus
                  extern "C" {  
                  #endif  
                  //  
                  // Code goes here ...  
                  //  
                  #ifdef __cplusplus  
                  } // extern "C"  
                  #endif
                  

                  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.
                  2. Careful of C++ identifiers as names in C code
                  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.
                  4. For structs follow the following form so that C does not get confused.

                    typedef struct X { ... } X
                    

                  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

                  All of this is courtesy of a friend who's a wizard at C++.

                  解决方案

                  Yes, this is certainly possible. You will need to write an interface layer in C++ that declares functions with extern "C":

                  extern "C" int foo(char *bar)
                  {
                      return realFoo(std::string(bar));
                  }
                  

                  Then, you will call foo() from your C module, which will pass the call on to the realFoo() function which is implemented in C++.

                  If you need to expose a full C++ class with data members and methods, then you may need to do more work than this simple function example.

                  这篇关于在 C 代码中使用 C++ 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:对“std::cout"的未定义引用 下一篇:链接上的未定义符号 ___gxx_personality_v0

                  相关文章

                  最新文章

                  <small id='rYVFo'></small><noframes id='rYVFo'>

                    <bdo id='rYVFo'></bdo><ul id='rYVFo'></ul>
                  <legend id='rYVFo'><style id='rYVFo'><dir id='rYVFo'><q id='rYVFo'></q></dir></style></legend>

                  <i id='rYVFo'><tr id='rYVFo'><dt id='rYVFo'><q id='rYVFo'><span id='rYVFo'><b id='rYVFo'><form id='rYVFo'><ins id='rYVFo'></ins><ul id='rYVFo'></ul><sub id='rYVFo'></sub></form><legend id='rYVFo'></legend><bdo id='rYVFo'><pre id='rYVFo'><center id='rYVFo'></center></pre></bdo></b><th id='rYVFo'></th></span></q></dt></tr></i><div id='rYVFo'><tfoot id='rYVFo'></tfoot><dl id='rYVFo'><fieldset id='rYVFo'></fieldset></dl></div>

                    <tfoot id='rYVFo'></tfoot>