Skip to content

Header guarding macros for precompiled headers should be unique across modules #13836

Closed

Description

@kinchungwong
Title
  • Violations of "headers should have guarding macros" and "header guarding macros should be unique" found in precompiled headers ("precomp.h") across many OpenCV modules
Type of issue
System information (version)
  • OpenCV branch => master (4.x)
    • Issue exists as far back as 2.x
    • Intention to fix this issue in master (no earlier than 4.1.0)
  • Platform => Windows, Visual C++
  • Compiler => Visual Studio Community 2017, Version 15.9.6
  • CMake version => 3.13.4
  • CMake options =>
    • BUILD_SHARED_LIBS=ON
    • ENABLE_PRECOMPILED_HEADERS=ON
Impact (brief)
Impact (details)
  • In the currently preferred implementation strategy for OE-11 "Adding Logging" discussion #11003 (logging improvement), each OpenCV module will define one or several uniquely-named, namespace-scope, static-lifetime variable(s), to represent the scope name and the log filtering threshold level applicable to that module.
  • However, one or several variables will need to be defined in OpenCV core module, and made accessible to all other modules, to act as a fallback, when the logging statement omits certain (log scope) information.
  • To achieve this, these fallback variables need to be exported from core module and imported into all other OpenCV modules.
  • When compiling as DLLs (shared libs) using Visual C++, exporting and importing namespace-level variables between DLLs require precise use of __declspec(dllexport) and __declspec(dllimport) (in which Visual C++ does not tolerate incorrect usage)
  • In order to select the correct macro for the MSVC DLL directive, it is necessary to have the ability to test whether a header (other than a precompiled header) is being included in the compilation of the same module (example: a header in core module being included by a source file in core module, versus included by a source file in some_other modules.)
  • CV_EXPORTS is insufficient for this purpose. (It was sufficient for class and function exports, but not for namespace-scope variables.)
Manifest of findings

Many precompiled headers retain __OPENCV_PRECOMP_H__ from the "big commit" from OpenCV 2.x

In each section below, the first line is the first preprocessor directive found in the "precomp.hpp", the rest of the lines indicate instances of "precomp.hpp" as found in the OpenCV source tree. In other words, any section that contains two or more paths are indicative of multiple files having the same header guard.

However, there are also a few "precomp.hpp" not having any header guard at all, which are also violations of the coding style guide, and will be cleaned up as part of this fix.

------------------------------------------------------------------------
#define __OPENCV_PRECOMP_H__
C:\Users\winuser\source\opencv\modules\calib3d\src\precomp.hpp
C:\Users\winuser\source\opencv\modules\features2d\src\precomp.hpp
C:\Users\winuser\source\opencv\modules\imgproc\src\precomp.hpp
C:\Users\winuser\source\opencv\modules\objdetect\src\precomp.hpp
C:\Users\winuser\source\opencv\modules\photo\src\precomp.hpp
C:\Users\winuser\source\opencv\modules\video\src\precomp.hpp
C:\Users\winuser\source\opencv\modules\world\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\bioinspired\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\ccalib\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudaarithm\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudafeatures2d\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudafilters\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudaimgproc\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudalegacy\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudaobjdetect\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudaoptflow\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudastereo\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudawarping\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\dpm\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\face\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\freetype\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\fuzzy\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\line_descriptor\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\ovis\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\phase_unwrapping\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\plot\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\rgbd\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\saliency\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\shape\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\structured_light\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\superres\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\text\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\tracking\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\xfeatures2d\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_CORE_PRECOMP_H__
C:\Users\winuser\source\opencv\modules\core\src\precomp.hpp

------------------------------------------------------------------------
#define CV_OCL4DNN 0
C:\Users\winuser\source\opencv\modules\dnn\src\precomp.hpp

------------------------------------------------------------------------
#define OPENCV_FLANN_PRECOMP_HPP
C:\Users\winuser\source\opencv\modules\flann\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_GAPI_PRECOMP_HPP__
C:\Users\winuser\source\opencv\modules\gapi\src\precomp.hpp

------------------------------------------------------------------------
#define __HIGHGUI_H_
C:\Users\winuser\source\opencv\modules\highgui\src\precomp.hpp

------------------------------------------------------------------------
#define __IMGCODECS_H_
C:\Users\winuser\source\opencv\modules\imgcodecs\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_ML_PRECOMP_HPP__
C:\Users\winuser\source\opencv\modules\ml\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_STITCHING_PRECOMP_H__
C:\Users\winuser\source\opencv\modules\stitching\src\precomp.hpp

------------------------------------------------------------------------
#ifdef GTEST_LINKED_AS_SHARED_LIBRARY
C:\Users\winuser\source\opencv\modules\ts\src\precomp.hpp

------------------------------------------------------------------------
#define __VIDEOIO_H_
C:\Users\winuser\source\opencv\modules\videoio\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_CCALIB_PRECOMP__
C:\Users\winuser\source\opencv_contrib\modules\aruco\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_BGSEGM_PRECOMP_HPP__
C:\Users\winuser\source\opencv_contrib\modules\bgsegm\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_CNN_3DOBJ_PRECOMP_HPP__
C:\Users\winuser\source\opencv_contrib\modules\cnn_3dobj\src\precomp.hpp

------------------------------------------------------------------------
#define OPENCV_PRECOMP_H
C:\Users\winuser\source\opencv_contrib\modules\cudabgsegm\src\precomp.hpp
C:\Users\winuser\source\opencv_contrib\modules\cudacodec\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_DNN_OBJDETECT_PRECOMP_HPP__
C:\Users\winuser\source\opencv_contrib\modules\dnn_objdetect\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_HDF_PRECOMP_H__
C:\Users\winuser\source\opencv_contrib\modules\hdf\src\precomp.hpp

------------------------------------------------------------------------
#define _OPENCV_PRECOMP_HPP_
C:\Users\winuser\source\opencv_contrib\modules\hfs\src\precomp.hpp

------------------------------------------------------------------------
#define OPENCV_IMG_HASH_PRECOMP_H
C:\Users\winuser\source\opencv_contrib\modules\img_hash\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_OPTFLOW_PRECOMP_HPP__
C:\Users\winuser\source\opencv_contrib\modules\optflow\src\precomp.hpp

------------------------------------------------------------------------
#define OPENCV_REG_PRECOMP_H__
C:\Users\winuser\source\opencv_contrib\modules\reg\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_SFM_PRECOMP__
C:\Users\winuser\source\opencv_contrib\modules\sfm\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_STEREO_PRECOMP_H__
C:\Users\winuser\source\opencv_contrib\modules\stereo\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_SURFACE_MATCHING_PRECOMP_HPP__
C:\Users\winuser\source\opencv_contrib\modules\surface_matching\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_PRECOMP_HPP__
C:\Users\winuser\source\opencv_contrib\modules\videostab\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_VIZ_PRECOMP_HPP__
C:\Users\winuser\source\opencv_contrib\modules\viz\src\precomp.hpp

------------------------------------------------------------------------
#define _OPENCV_XIMGPROC_PRECOMP_HPP_
C:\Users\winuser\source\opencv_contrib\modules\ximgproc\src\precomp.hpp

------------------------------------------------------------------------
#define __OPENCV_XOBJDETECT_PRECOMP_HPP__
C:\Users\winuser\source\opencv_contrib\modules\xobjdetect\src\precomp.hpp

Activity

  1. alalek commented on Feb 15, 2019

    @alalek
    Member

    precomp.hpp

    Special type of headers (like test_precomp.hpp, perf_precomp.hpp).
    They are not expected to be included twice (must be avoided). Must be included from .cpp files (or dispatched .simd.hpp) in the first include directive (should be included via relative path - or opencv_world would break (precompiled headers doesn't work well with opencv_world)).
    These headers are not covered by coding style guide.

    __declspec(dllimport)

    Not used in OpenCV (for "CV_EXPORTS" at least).

    In order to select the correct macro for the MSVC DLL directive

    We use __declspec(dllexport) only during OpenCV binaries build.
    There is corresponding bug - single definition of CV_EXPORTS for all modules headers.
    But for example, for imgproc module compilation:

    • imgproc headers should be processed with __declspec(dllexport)
    • core headers - with __declspec(dllimport) (or at least without "export"). But it is dllexport too, so core symbols are declared as exported symbols of imgproc module.
      Unfortunately, there is no small patch for this.
      Current impact is bigger size of opencv_imgproc.dll (it re-exports inlined functions from opencv_core.dll too) - but there is no such problem with opencv_world.dll (all-in-one).

    to act as a fallback

    I will try to prepare demo patch for that on weekend.

  2. kinchungwong commented on Feb 15, 2019

    @kinchungwong
    ContributorAuthor

    I agree that CV_EXPORTS is the real issue here.

    Closing this issue for now since there is no urgent need to fix precompiled header guards. If there is future need, this issue can be reopened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions