使用 C++ 的简单 C 程序中的链接错误标头

发布于 2024-11-26 15:42:55 字数 1343 浏览 1 评论 0原文

我正在关注介绍教程 http://www.linuxinsight.com /files/alp/alp-ch01-getting-started.pdf

我创建了源文件 main.c、reciprocal.cpp 和 reciprocal.hpp。我已经能够成功编译这些文件。当我去链接它们并收到以下错误消息时,问题就出现了:

main.o: In function `main':
main.c:(.text+0x30): undefined reference to `reciprocal'
collect2: ld returned 1 exit status

我认为这在某种程度上没有正确使用头文件,但是我真的不知道要尝试什么,因为我已经非常彻底地遵循了这些步骤。

如果有人知道为什么会抛出该错误,我将非常感激。

谢谢

**更新 以下是三个源文件的代码:

main.c

#include <stdio.h>
#include "reciprocal.hpp"

int main (int argc, char **argv) {
    int i;
    i = atoi(argv[1]);
    printf("The reciprocal of %d is %g\n", i, reciprocal(i));

    return 0;
}

reciprocal.cpp

#include <cassert>
#include "reciprocal.hpp"

double reciprocal(int i) {
    assert(i != 0);
    return 1.0/i;
}

reciprocal.hpp

#ifdef __cplusplus
extern "C" {
#endif

double reciprocal(int i);

#ifdef __cplusplus
}
#endif

自最初发布以来,我实际上已经修改了 reciprocal.hpp。我所做的唯一更改是从函数签名中删除 extern 关键字。之前的内容是

extern double reciprocal(int i);

“此更改允许我链接该程序,现在我可以运行它”。我认为可能发生的情况是第二个外部人员覆盖了第一个外部人员。如果有人对此有任何见解,我将有兴趣了解。

感谢大家的帮助。

I am following an intro tutorial http://www.linuxinsight.com/files/alp/alp-ch01-getting-started.pdf.

I have created the source files main.c, reciprocal.cpp and reciprocal.hpp. I have been able to successfully compile these files. The problem comes when I go to link them and I receive the following error message:

main.o: In function `main':
main.c:(.text+0x30): undefined reference to `reciprocal'
collect2: ld returned 1 exit status

I gather that this is somehow not using the header files correctly, however I do not really know what to try as I've followed the steps quite thoroughly.

If anyone has an idea why its throwing that error I would appreciate it very much.

Thanks

**UPDATE
Here is the code for the three source files:

main.c

#include <stdio.h>
#include "reciprocal.hpp"

int main (int argc, char **argv) {
    int i;
    i = atoi(argv[1]);
    printf("The reciprocal of %d is %g\n", i, reciprocal(i));

    return 0;
}

reciprocal.cpp

#include <cassert>
#include "reciprocal.hpp"

double reciprocal(int i) {
    assert(i != 0);
    return 1.0/i;
}

reciprocal.hpp

#ifdef __cplusplus
extern "C" {
#endif

double reciprocal(int i);

#ifdef __cplusplus
}
#endif

I have actually modified reciprocal.hpp since originally posting. The only change I've made was to remove the extern keyword from the function signature. It previously read

extern double reciprocal(int i);

This change allowed me to link the program and I can now run it. I think what may have been occurring is that the second extern was overriding the first one. If anyone has any insight on this, I would be interested in knowing.

Thanks for your assistance everyone.

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

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

发布评论

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

评论(3

零度℉ 2024-12-03 15:42:55

当您将 C 和 C++ 代码混合在一起时,可能会遇到麻烦,因为这两种语言具有不同的链接。也就是说,如果您要破解已编译的 C 和 C++ 文件的目标文件,您会发现这些目标文件内部的名称不同。

特别是,C++ 编译器倾向于使用一种称为“名称修饰”的技术,其中函数名称与有关其参数类型的额外信息混合在一起。例如,函数

char foo(int);

具有内部名称

char@foo@int

实际上可能在生成的目标文件中 。问题是 C 代码不会执行此操作,因此如果您编译了一个 C 文件并尝试引用名称 foo,链接器将找不到它,因为函数的名称在生成的 C++ 文件中是 char@foo@int 而不是 foo

为了解决这个问题,C++ 有一个功能,允许您显式告诉链接器不要破坏名称,并使生成的代码看起来像是针对 C 程序的。为此,您可以像这样声明 C++ 函数:

extern "C" char foo(int);

现在,生成的目标文件将包含名称 foo,而无需以与 C 代码所期望的方式兼容的修饰。

要解决您的问题,请尝试将这些 extern "C" 声明之一添加到定义倒数 的 C++ 源文件中。

注意:如果您有一个混合 C 和 C++ 代码的项目,您应该始终main 函数编译为 C++ 代码。 C++ 在其可执行文件中引入了 C 代码中不存在的额外初始化和清理代码。如果将 main 编译为 C 代码,则这些额外的逻辑可能不会添加到程序中,因此您可能会在运行时遇到莫名其妙的崩溃。

希望这有帮助!

When you mix C and C++ code together, you can run into trouble because the two languages have different linkages. That is, if you were to crack open the object files for a compiled C and C++ file, you would find that the names inside of those object files were different.

In particular, C++ compilers tend to use a technique called "name mangling" in which function names are mixed with extra information about the types of their arguments. For example, a function

char foo(int);

might actually have the internal name

char@foo@int

inside of the generated object file. The problem is that C code does not do this, so if you compiled a C file and tried to make a reference to the name foo, the linker wouldn't find it, because the name of the function in the generated C++ file is char@foo@int instead of foo.

To fix this, C++ has a feature that allows you to explicitly tell the linker not to mangle the name and to make the generated code look like it was intended for a C program. To do this, you can declare C++ functions like this:

extern "C" char foo(int);

Now, the generated object file will contain the name foo without adornment in a way that is compatible with what the C code expects.

To fix your problem, try adding one of these extern "C" declarations to your C++ source file that defines reciprocal.

A note: if you have a project that mixes C and C++ code, you should always have the main function compiled as C++ code. C++ introduces extra initialization and cleanup code into its executables that are not present in C code. If you compile main as C code, this extra logic might not get added into the program, and so you may get inexplicable crashes at runtime.

Hope this helps!

帝王念 2024-12-03 15:42:55

将 C++ 文件链接到 C 文件是最困难的。 C++“破坏”了名称,以便可以发生重载。

您可以通过像这样包含相互定义来防止损坏:

extern "C" {
   reciprocal definition...
}

但如果您这样做,您不妨用 C 编写它......

您可以制作一个 main.cpp 文件吗?

Linking a C++ file into a C file is difficult at best. The C++ "mangles" the name so that overloading can occur.

You can prevent the mangling by enclosing your reciprocal definition like this:

extern "C" {
   reciprocal definition...
}

but if you do this, you might as well just write it in C...

Can you make a main.cpp file?

猥︴琐丶欲为 2024-12-03 15:42:55

看起来 reciprocal.hpp 的内容无效,可能是 #ifdef 行中的拼写错误,__cplusplus 拼写正确(应该有两个下划线)?

Looks like the contents of your reciprocal.hpp is invalid, maybe a typo in the #ifdef line, is __cplusplus spelled correctly (there should be two underscores)?

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