交换 C++ 中常用的函数

发布于 2024-11-06 03:14:58 字数 477 浏览 0 评论 0原文

嘿, 我目前有一个很大的 C++ 代码文件,其中包含许多我经常在程序的不同版本中使用的函数,因此我考虑交换常用的函数:例如,我有:

void doSomething(mxArray *in)
{
    mexPrintf("Hello Matlab");
}

mxArraymexPrintf 是在来自 matlab (mex.h) 的另一个文件中定义的。在我现在的主文件中:

#include "mex.h"
#include "_general.h"

我想知道当 _general.cpp 中不包含 mex.h 时我没有收到任何编译器错误,也是因为文件本身需要它明显地。您最好执行包含操作吗?在这种情况下,在 mex.h 已包含在主文件中之后包含它是否重要?

谢谢!

Hey there,
I have a big code file currently in C++ with many functions which I often use in different versions of my program so I thought about swapping out the generally used functions: E.g. I have:

void doSomething(mxArray *in)
{
    mexPrintf("Hello Matlab");
}

whereas mxArray and mexPrintf are defined in another file which comes from matlab (mex.h). In the mainfile I have now:

#include "mex.h"
#include "_general.h"

and I wonder that I didn't get any compiler errors when not including mex.h in _general.cpp also because the file itself NEEDs it obviously. Would you better do the include or doesn't it matgter in this case it is included AFTER mex.h has already been included in the mainfile?

Thanks!

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

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

发布评论

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

评论(2

﹏半生如梦愿梦如真 2024-11-13 03:14:58

C++(与 C 不同)将拒绝对未看到声明的函数签名做出可疑的假设,并且编译将失败并显示错误消息。如果没有收到错误,则说明某个标头正在获取该函数的声明。如果找不到它,请尝试仅运行预处理器阶段(例如,对于 GCC,您将使用 g++ -E)并检查输出以查看声明...您的编译器可能会留下注释关于哪个文件包含一些有助于理解情况的代码。

例如,如果 _general.cpp 包含 _general.h,而 _general.h 包含 mex.h,那么这是可行的,并且无需直接从 _general.cpp 包含它。但是,如果它可以从 _general.h 中删除,因为它只在“general”的实现中需要,那么那就更好了。

如果其他代码(例如“libraryX”)使用 mex.h 来满足其内部需求,而没有通过其公共 API 公开与 mex 相关的功能,那么最好不要假设它会继续为您包含 mex.h 并自行包含它。

C++ (unlike C) will refuse to make dubious assumptions about the signature of functions for which it hasn't seen a declaration, and compilation will fail with an error message. If you don't get an error, then some header is picking up a declaration for that function. If you can't find it, then try running only the preprocessor stage (e.g. for GCC you'd use g++ -E) and inspecting the output to see the declaration... your compiler may leave comments about which file has included bits of code which can be helpful in understanding the situation.

For example, if _general.cpp includes _general.h which includes mex.h then that's workable, and there's no need to include it directly from _general.cpp. But, if it can be removed from _general.h as it's only needed in the implementation of "general", then that's much better again.

If some other code, say "libraryX" uses mex.h for it's internal needs without exposing mex-related functionality through its public API, then it's better NOT to assume it will continue to include mex.h for you and include it yourself.

她比我温柔 2024-11-13 03:14:58

正如 Tamás 指出的那样,#include 指令实际上将头文件包含到源代码中。您可以使用预处理器(gcc 的一部分)进行检查。

$ cat a.cpp
#include "b.h"
int main() {
    return b();
}
$ cat b.h
#ifndef _B_H
#define _B_H

int b()
{
    return 0;
}

#endif
$ cpp a.cpp
# 1 "a.cpp"
    # 1 "<built-in>"
# 1 "<command-line>"
# 1 "a.cpp"
# 1 "b.h" 1



int b()
{
    return 0;
}
# 2 "a.cpp" 2
int main() {
    return b();
}
$

如您所见,如果两次包含同一个文件(在涉及多个源文件的任何中等复杂的程序中经常出现这种情况),您会遇到以下问题:同一个函数定义了两次。要解决这个问题,标准方法是使用“include Guards”(#ifndef#define#endif in bh)。这样,每当包含 bh 时,就会定义一个常量。当您再次包含 bh 时,预处理器会检查此常量并决定不再包含它。

当然,在选择常量名称时应小心:)

如果遵循此规则,则理想情况下可以在任何位置包含任何头文件。通常最好在绝对必要的地方包含头文件,否则编译将失败。另外,一个好的做法是在自定义头文件之前包含标准头文件。

哈!

As Tamás pointed out, #include directive literally includes the header file into your source code. You can check this using the pre-processor (part of gcc)

$ cat a.cpp
#include "b.h"
int main() {
    return b();
}
$ cat b.h
#ifndef _B_H
#define _B_H

int b()
{
    return 0;
}

#endif
$ cpp a.cpp
# 1 "a.cpp"
    # 1 "<built-in>"
# 1 "<command-line>"
# 1 "a.cpp"
# 1 "b.h" 1



int b()
{
    return 0;
}
# 2 "a.cpp" 2
int main() {
    return b();
}
$

As you can see, if you include the same file twice (which is frequently the case in any moderately complex program involving multiple source files), you run into the problem of having the same function defined twice. To solve this problem, the standard way is to use the 'include guards' (#ifndef, #define and #endif in b.h). That way, whenever you include b.h, a constant is defined. When you include b.h again, pre-processor checks this constant and decides not to include it again.

Of course, care should be taken in choosing the name of the constant :)

If you follow this rule, you can ideally include any header file anywhere. It is generally good idea to include the header file where it is absolutely necessary and without which compilation will fail. Also, a good practice is to include standard header files before custom header files.

HTH!

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