Le Hoang Long
Le Hoang Long

Reputation: 500

Why doesn't library source file include its header

I am currently trying to explore about OpenCV.

When I looked for Mat class, i can see that it is defined in mat.hpp, which is located in an include folder. However, when I attempted to search for its corresponding mat.cpp, I found that the class definition is actually located in matrix.cpp. I suppose the difference in file name is not a problem, as long as it includes mat.hpp. However, the header mat.hpp wasn't included either. I know the compiler would probably still able to compile, but as far as I know, this is a bad programming practice.

So, my question is, is it true that it is a bad practice not to include the header file in the source file? Is there any reason why it should not be included?

I understand that I do not need to explore too much about the detailed implementation of the library, but this question is just out of curiosity.

Here is the #include part of the matrix.cpp

#include "precomp.hpp"
#include "opencl_kernels_core.hpp"
#include "bufferpool.impl.hpp"

And here is the #include part of the mat.hpp

#include "opencv2/core/matx.hpp"
#include "opencv2/core/types.hpp"
#include "opencv2/core/bufferpool.hpp"

Upvotes: 2

Views: 375

Answers (2)

AnT stands with Russia
AnT stands with Russia

Reputation: 320531

In C++, when you define a class in a header file and then define class members (methods and such) in a .cpp file, the class definition is supposed to be visible in that .cpp file. This is usually achieved by including the header file with the class definition into the .cpp file(s) that defines the members.

In OpenCV mat.hpp is included into matrix.cpp indirectly. matrix.cpp includes precomp.hpp, which in turn includes core.hpp, which in turn includes mat.hpp.

So, mat.hpp is actually included into matrix.cpp. Nothing unusual here.

P.S. File names play no role whatsoever. There's nothing unusual in mat.hpp being named "differently" from matrix.cpp.

Upvotes: 3

kmdreko
kmdreko

Reputation: 60062

The short answer is: That's how the project evolved.

The long answer is: OpenCV did (and still does) declare many of its functions and classes in core.hpp while the many .cpp files hold the implementations. In this case, matrix-specific functions (and implementations for Mat) would be in matrix.cpp and definitions for classes, like Mat, would be in core.hpp.

As the project grew, things seem to get split up (probably just to be easier to manage). You can see in this commit, the class Mat was moved out of core.hpp into its own mat.hpp (named as such probably to match the class name). The source file matrix.cpp does include mat.hpp, though indirectly (through precomp.hpp and core.hpp).

I don't think this confusion is intentional, just incidental. Is this bad practice? Some would say so, but thats up to you to decide.

Disclaimer: I have not worked on the OpenCV source, but did use it extensively for a time.

Upvotes: 3

Related Questions