调用C++来自C文件的标准标头(CSTDINT)

发布于 2025-01-27 12:32:27 字数 806 浏览 3 评论 0 原文

我有一个用C ++编写的外部库,例如

external.h

#ifndef OUTPUT_FROM_CPP_H
#define OUTPUT_FROM_CPP_H

#include <cstdint>
extern "C" uint8_t myCppFunction(uint8_t n);

#endif

external.cpp

#include "external.h"
uint8_t myCppFunction(uint8_t n)
{
    return n;
}

当前我别无选择,只能在我当前的C项目中使用此C ++库。但是我的编译器告诉我

No such file or director #include <cstdint>

何时在我的C项目中使用

main.c

#include "external.h"

int main()
{
    int a = myCppFunction(2000);

    return a;
}

我知道这是因为 cstdint 是我试图通过我的C ++标准库C文件。

我的问题是:

  • 有没有一种方法可以在我的C项目中使用此C ++库,而无需修改我的诽谤?
  • 如果没有,我必须在图书馆一侧做什么才能使它成为可能?

I have an external library written in C++ such as

external.h

#ifndef OUTPUT_FROM_CPP_H
#define OUTPUT_FROM_CPP_H

#include <cstdint>
extern "C" uint8_t myCppFunction(uint8_t n);

#endif

external.cpp

#include "external.h"
uint8_t myCppFunction(uint8_t n)
{
    return n;
}

Currently I have no choice but use this C++ library in my current C project. But my compiler is telling me

No such file or director #include <cstdint>

when used in my C project

main.c

#include "external.h"

int main()
{
    int a = myCppFunction(2000);

    return a;
}

I understand that this is because cstdint is a C++ standard library that I'm trying to use through my C file.

My questions are:

  • Is there a way I can manage to use this C++ library in my C project without modifying my libary ?
  • If no, what whould I have to do on the library side to make it possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短叹 2025-02-03 12:32:27

c cstdint 中的前缀是因为它确实是来自C的标头文件。C中的名称是 stdint.h

您需要通过检测 __ cplusplus 宏来有条件地包含正确的标头。您还需要此宏才能使用 extern“ c” 部分,因为这是c ++特定的:

#ifndef OUTPUT_FROM_CPP_H
#define OUTPUT_FROM_CPP_H

#ifdef __cplusplus
// Building with a C++ compiler
# include <cstdint>
extern "C" {
#else
// Building with a C compiler
# include <stdint.h>
#endif

uint8_t myCppFunction(uint8_t n);

#ifdef __cplusplus
}  // Match extern "C"
#endif

#endif

The c prefix in cstdint is because it's really a header file incorporated from C. The name in C is stdint.h.

You need to conditionally include the correct header by detecting the __cplusplus macro. You also need this macro to use the extern "C" part, as that's C++ specific:

#ifndef OUTPUT_FROM_CPP_H
#define OUTPUT_FROM_CPP_H

#ifdef __cplusplus
// Building with a C++ compiler
# include <cstdint>
extern "C" {
#else
// Building with a C compiler
# include <stdint.h>
#endif

uint8_t myCppFunction(uint8_t n);

#ifdef __cplusplus
}  // Match extern "C"
#endif

#endif
丿*梦醉红颜 2025-02-03 12:32:27

您必须修改库。

&lt; cstdint&gt; &lt; stdint.h&gt; 。通常建议使用前者,但只有后者存在于C中。

您还应该在 extern“ C” 上遇到错误。 下方放置以下内容来解决这一点

#ifdef __cplusplus
extern "C" {
#endif

通过在文件末尾的匹配部分

#ifdef __cplusplus
}
#endif

:然后可以从单个功能中删除 extern“ c”

You have to modify the library.

Replace <cstdint> with <stdint.h>. Normally the former is recommended, but only the latter exists in C.

You should also be getting errors on extern "C". That's solved by putting following right below the includes:

#ifdef __cplusplus
extern "C" {
#endif

With a matching section at the end of file:

#ifdef __cplusplus
}
#endif

Then extern "C" can be removed from individual functions.

终陌 2025-02-03 12:32:27

有没有一种方法可以在我的C项目中使用此C ++库,而无需修改我的LIBARY?

创建一个可与C携带的单独的标头,并在使用C编译器编译时使用该标头:

// external_portable_with_c.h
// rewrite by hand or generate from original external.h
// could be just sed 's/cstdint/stdint.h/; s/extern "C"//'
#include <stdint.h>
uint8_t myCppFunction(uint8_t n);
 
// c_source_file.c
#include "external_portable_with_c.h"
void func() {
    myCppFunction(1);
}

如果没有,我在图书馆一侧要做什么才能使它成为可能?

通过其他答案回答。用 #ifdef __cplusplus 保护C ++零件。

请注意,(有些?全部?)编译器需要 main 函数,将使用C ++编译器编译,以供C ++和C正确工作。

Is there a way I can manage to use this C++ library in my C project without modifying my libary ?

Create a separate header that is portable with C and use that header when compiling with C compiler:

// external_portable_with_c.h
// rewrite by hand or generate from original external.h
// could be just sed 's/cstdint/stdint.h/; s/extern "C"//'
#include <stdint.h>
uint8_t myCppFunction(uint8_t n);
 
// c_source_file.c
#include "external_portable_with_c.h"
void func() {
    myCppFunction(1);
}

If no, what whould I have to do on the library side to make it possible ?

Is answered by other answers. Protect the C++ parts with #ifdef __cplusplus.

Note that (some? all?) compilers require the main function to be compiled with C++ compiler for C++ and C to work together properly. https://isocpp.org/wiki/faq/mixing-c-and-cpp#overview-mixing-langs

临走之时 2025-02-03 12:32:27

如果您不想修改库标头,请创建一个新文件,例如 inculte_for_cpp/cstdint with

#include <stdint.h>

content 。之后, #include&lt; cstdint&gt; 应该找到您的文件。

If you don't want to modify the library header, create a new file, for example includes_for_cpp/cstdint with content

#include <stdint.h>

Add the directory includes_for_cpp to the include path of your C project. After that, #include <cstdint> should find your file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文