gpt4 book ai didi

c++ - 为什么我的 C 程序可以在不包含头文件的情况下从 C++ 文件调用函数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:37 25 4
gpt4 key购买 nike

例如,假设我有以下文件:hello.cpp、hello.h 和 main.c

在 hello.cpp 中说我有以下内容:

#include "hello.h"
extern "C" void test_func(int &a, int b){
some stuff
}

在 hello.h 中我有以下内容:

#ifdef __cplusplus
extern "C" {

#endif
void test_func(int& a, int b);

#ifdef __cplusplus
}
#endif

这就是我感到困惑的地方。如果我在 main.c 中有以下内容:

#include "hello.h"
extern void test_func(int*, int);

那么这将无法正确编译。它告诉我我的 hello.h 文件有错误,我假设这是因为 C 不支持通过引用传递?我注意到,如果我将 hello.h 文件更改为“void test_func(int*a, int b)”,那么这将正确编译。

但是,如果我的 main.c 文件中没有#include "hello.h",那么它也可以正确编译。而且,即使不包含 hello.h,我也可以从 main.c 调用 test_func。声明函数原型(prototype)就足够了吗?为什么?如果我想包含 hello.h 是否意味着我必须使其与 C 兼容并且没有任何通过引用传递的函数?

这一切都是新手,所以提前感谢任何人的帮助。

最佳答案

用两个不同的原型(prototype)声明同一个 C 函数是未定义的行为。不要那样做。 (这种特殊情况无论如何都可能“起作用”,因为典型的编译器通过将内存地址放在堆栈上来传递指针类型,并通过将内存地址放在堆栈上来传递引用类型。)

是的,您当前的 hello.h 根本不能用于 C 代码。如果你想让一个函数跨越 C-C++“边界”,它应该有一个类 C 的声明。 (除非它可以位于 C++ 端的命名空间中;此命名空间在 C 端被忽略。)

相反,您可以围绕 C++ 函数创建一个 C 包装器:

// hello.h
#ifndef HELLO_H_GUARD_
#define HELLO_H_GUARD_

#ifdef __cplusplus
void test_func(int &a, int b);

extern "C"
#endif
void test_func_c(int *a, int b);

#endif

// hello.cpp
void test_func(int &a, int b) {
//...
}

extern "C"
void test_func_c(int *a, int b) {
test_func(*a, b);
}

顺便说一下,您提到了“main.c”文件。混合使用 C 和 C++ 时,main 函数应位于 C++ 源文件中。如果你愿意,它可以只是一个调用你的“真正的”C main-like 函数的包装器。

关于c++ - 为什么我的 C 程序可以在不包含头文件的情况下从 C++ 文件调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com