如何在 C++ 中使用 C 标头程序?

发布于 2024-11-18 22:32:09 字数 1260 浏览 8 评论 0原文

我正在 Visual Studio 2010 中开发一个项目,该项目将生成一个 win 32 dll 文件。我的例子是 C 文件,编译并运行良好。我想合并我编写的 C++ 函数中的一些功能,但遇到了一些困难。

如果我尝试将 C++ 函数链接到 C 程序,它对字符串等一无所知,并且根本不起作用(当然)。

所以我只能将示例更改为 C++ 程序,然后我就可以不受惩罚地使用其他文件了。当我尝试执行此操作时,我收到一个我不理解的链接错误,并且不确定如何解决。

这些示例使用供应商提供的标头,其中包括诸如以下语句

 typedef void ( __cdecl *BINDING_PROC_BEVNT)(WORD_T choice, INT_T * pStatus, 
            I_EVNT_T  * pIn, O_EVNT_T  * pOut);

在主代码正文中,遵循示例:

extern BINDING_PROC_BEVNT       b_evnt;

然后允许您编写

b_evnt(choice, &status, &inpEvent, &outpEvent);

在供应商提供的 C 文件中,这些再次引用为:

BINDING_PROC_BEVNT      b_evnt; 
b_evnt = (BINDING_PROC_BEVNT)GetProcAddress(hCNCMod, "bevnt");

我看到的链接器错误是:

错误 LNK2001:无法解析的外部符号“void (__cdecl* b_evnt)(unsigned Short,short *,union I_EVNT_T *,union O_EVNT_T *)”(?b_evnt@@3P6AXGPAFPATI_EVNT_T@@PATO_EVNT_T@@@ZA)

如果我重命名我的主文件并且重新编译为 C 程序,并省略我的 C++ 函数,一切完美编译。即使主文件被处理为 C++ 文件,Intellisense 似乎也能识别定义(将鼠标悬停在上方会显示正确的定义)。

此外,我尝试将 extern "C" 添加到几个不同的位置,但它似乎对 C++ 文件没有影响,并且在 C 文件中生成了编译错误(关于不了解字符串)。

任何见解都会受到赞赏,我今天可能只是盯着这个问题太久而没有发现一些明显的东西,或者它可能是我完全不知道的东西。

感谢您的帮助!

I am working on a project in Visual Studio 2010 which is to produce a win 32 dll file. The examples I have are C files and the compile and work well. I would like to incorporate some functionality from C++ function I've written but I have hit a bit of a wall.

If I attempt to link the C++ functions to the C program, it knows nothing about strings, etc and just doesn't work at all (of course).

So I am left with changing the example into a C++ program, then I can use my other files with impunity. When I attempt to do this I get a link error that I don't understand and am uncertain about how to resolve.

The examples use vendor provided headers, which include statements such as

 typedef void ( __cdecl *BINDING_PROC_BEVNT)(WORD_T choice, INT_T * pStatus, 
            I_EVNT_T  * pIn, O_EVNT_T  * pOut);

In the body of the main code, following the examples:

extern BINDING_PROC_BEVNT       b_evnt;

Which then allows you to write

b_evnt(choice, &status, &inpEvent, &outpEvent);

In a vendor provided C file, these are again referenced as:

BINDING_PROC_BEVNT      b_evnt; 
b_evnt = (BINDING_PROC_BEVNT)GetProcAddress(hCNCMod, "bevnt");

The linker error I am seeing is:

error LNK2001: unresolved external symbol "void (__cdecl* b_evnt)(unsigned short,short *,union I_EVNT_T *,union O_EVNT_T *)" (?b_evnt@@3P6AXGPAFPATI_EVNT_T@@PATO_EVNT_T@@@ZA)

If I rename my main file and recompile as a C program, and omit my C++ functions, everything compiles perfectly. Even when the main file is processed as a C++ file, Intellisense seems to recognize the definitions (hovering over shows the correct definitions).

Additionally I attempted to add extern "C" to a few different locations but it didn't seem to make a difference in the C++ files, and generated a compile error in the C files (about not knowing about strings).

Any insight would be appreciated, I may have simply stared at this too long today to be picking up on something obvious, or it may be something I'm completely unaware of.

Thanks for the help!

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

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

发布评论

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

评论(4

灯下孤影 2024-11-25 22:32:09

如果您正在针对具有 C 语言绑定的库进行编译,则必须明确告诉 C++ 该库的头文件引用 C 对象,而不是 C++ 对象,否则 C++ 名称修改将阻止正确链接。通常您可以这样做:

extern "C" {
#include "vendor.h"
}

这将告诉 C++ 编译器大括号之间的符号是 C 符号,并且不应应用名称修饰。

If you are compiling against a library that has C-language bindings, you have to tell C++ explicitly that the header files for the library reference C-objects, not C++ objects, or C++ name mangling will prevent correct linking. Often you can do this like so:

extern "C" {
#include "vendor.h"
}

This will tell the C++ compiler that the symbols between the braces are C symbols, and should not have name mangling applied.

后来的我们 2024-11-25 22:32:09

要包含 C++ 中的 C 头文件,请执行以下操作:

test.cpp

extern "C" {

#include "c_header_file.h"

}

听起来您可能需要执行上述操作才能将供应商头文件包含在 C++ 代码中。

相关地,要使头文件自动适用于 C 和 C++:

c_header_file.h

#ifdef __cplusplus
extern "C" {
#endif

void f(int);
// all declarations go here

#ifdef __cplusplus
}
#endif

并非所有供应商提供的头文件都包含上述 __cplusplus 检测,因此您必须将它们手动包装在 extern "C" 如第一个示例所示。

To include a C header file from C++, do something like this:

test.cpp

extern "C" {

#include "c_header_file.h"

}

It sounds like the above is what you might need to do to include the vendor header file in your C++ code.

Relatedly, to make a header file automatically work for both C and C++:

c_header_file.h

#ifdef __cplusplus
extern "C" {
#endif

void f(int);
// all declarations go here

#ifdef __cplusplus
}
#endif

Not all vendor-provided header files will contain the above __cplusplus detection, so you will have to wrap them manually in extern "C" as in the first example.

顾冷 2024-11-25 22:32:09
error LNK2001: unresolved external symbol "void (__cdecl* b_evnt) 
(unsigned short,short *,union I_EVNT_T *,union O_EVNT_T *)" 
(?b_evnt@@3P6AXGPAFPATI_EVNT_T@@PATO_EVNT_T@@@ZA)

这意味着无法找到 C++ 损坏的变量 b_evnt。这是真的,因为它应该被 C 破坏(只是一个 _ 前缀)。要解决这个问题,请在编译 C++ 时在标头中告诉编译器:

#ifdef __cplusplus
extern "C" BINDING_PROC_BEVNT       b_evnt;
#else
extern BINDING_PROC_BEVNT       b_evnt;
#endif

如果仅此而已,那么您就完成了。如果您需要更多符号,您可能想使用 Greg 的解决方案 - 但请注意,这也不是解决所有问题的方法。

error LNK2001: unresolved external symbol "void (__cdecl* b_evnt) 
(unsigned short,short *,union I_EVNT_T *,union O_EVNT_T *)" 
(?b_evnt@@3P6AXGPAFPATI_EVNT_T@@PATO_EVNT_T@@@ZA)

That means, the C++ mangled variable b_evnt can't be found. That's true, because it should've been C mangled (just an _ prefix). To fix that, tell it to the compiler in the header when compiling for C++:

#ifdef __cplusplus
extern "C" BINDING_PROC_BEVNT       b_evnt;
#else
extern BINDING_PROC_BEVNT       b_evnt;
#endif

If that's all, you're done. If there are more symbols you need, you might want to use Greg's solution instead - but be aware that that is also not a fixall.

初见你 2024-11-25 22:32:09

您的供应商提供的标头包含在 C++ 编译单元中,但它们没有为 C++ 做好准备。因此,函数的声明正在使用名称修改进行编译,C++ 编译器需要支持重载。

标头需要包装在 extern "C" {} 块中,以便让 C++ 编译器知道这些声明使用 C 链接。

可能最简单的方法是使用您自己的包装器标头,执行以下操作:

#ifndef FOO_WRAPPER_H
#define FOO_WRAPPER_H

#if __cplusplus
extern "C" {
#endif

#include "foo.h"

#if __cplusplus
}
#endif

#endif

并包含 yiour 包装器标头而不是供应商的标头 - 包装器将适用于 C 或 C++ 编译。

另外,请联系您的库供应商并让他们知道他们应该进行这些更改 - 库的用户不必执行此解决方法即可使用 C++ 中的库。

Your vendor provided headers are being included in a C++ compilation unit, but they aren't prepared for C++. So the declarations of the functions are being compiled with name mangling the C++ compielr requires to support overloading.

The headers need to be wrapped in an extern "C" {} block to let the C++ compiler know that these declarations use a C linkage.

Probably the easiest way to do this it to use your own wrapper headers that do something like:

#ifndef FOO_WRAPPER_H
#define FOO_WRAPPER_H

#if __cplusplus
extern "C" {
#endif

#include "foo.h"

#if __cplusplus
}
#endif

#endif

And include yiour wrapper header instead of the vendor's header - the wrapper will work for wither C or C++ compiles.

Also, contact your library vendor and let them know they should make these changes - users of the library shouldn't have to do this workaround to use the library from C++.

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