tim
tim

Reputation: 10176

Swap out generally used functions in C++

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!

Upvotes: 1

Views: 321

Answers (2)

Anupam Srivastava
Anupam Srivastava

Reputation: 859

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!

Upvotes: 3

Tony Delroy
Tony Delroy

Reputation: 106096

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.

Upvotes: 4

Related Questions