Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: support Ceres 2.0.0 #2732

Merged
merged 1 commit into from
Nov 7, 2020
Merged

Conversation

alalek
Copy link
Member

@alalek alalek commented Oct 29, 2020

Merge with PR: opencv/opencv#18699

https://github.com/ceres-solver/ceres-solver/releases/tag/2.0.0 (Oct 26, 2020)

resolves #2578

Validated with:

  • Linux: Ceres 2.0.0 (built from sources)
  • Linux: Ceres 1.13.0 (from Fedora 31), Ceres 1.14.0 (from Fedora 32)
  • Linux: Ceres 1.13.0 from sources
  • Windows platform
  • Mac OSX platform
opencv=

@alalek
Copy link
Member Author

alalek commented Oct 30, 2020

/cc @LaurentBerger It would be nice if you can check this on Windows platform (if you still have configured build environment)

@NikolausDemmel
Copy link

I guess with ceres 2.0 it only compiles if you compile the whole of opencv with c++14 mode, right? By default it's C++11 I think (and ceres required 14).

@alalek
Copy link
Member Author

alalek commented Oct 31, 2020

@NikolausDemmel

Modern CMake supports propagation of language requirements from used components (through target_link_libraries()).

For example, CeresTargets.cmake from Ceres package has this:

set_target_properties(Ceres::ceres PROPERTIES
...
  INTERFACE_COMPILE_FEATURES "cxx_std_14"
...
)

So, OpenCV modules which uses Ceres will be automatically compiled with C++14.
It is enough, from OpenCV build scripts just do not break that in some way (don't force C++11).

Forced C++11 mode causes compilation errors with error messages near AddResidualBlock (there is difference in processing of variadic template parameters between C++11/C++14).
You can investigate that through make VERBOSE=1 / ninja -v and check for -std=c++11 / -std=gnu++11 compiler flags.


BTW, Possible workaround for non-patched OpenCV is forcing -DCMAKE_CXX_STANDARD=14 through OpenCV CMake command line.

@LaurentBerger
Copy link
Contributor

/cc @LaurentBerger It would be nice if you can check this on Windows platform (if you still have configured build environment)

I updated my ceres version and builded and installed it.
When I ran c_api_test.exe it gives me

Solver Summary (v 2.0.0-eigen-(3.3.90)-no_lapack-eigensparse-no_openmp)

cd opencv_contrib
git branch ceres
git clone -b support_ceres_2.0.0 https://github.com/alalek/opencv_contrib

and I tried to build opencv :

and there is a problem

-- Failed to find Ceres - Missing required Ceres dependency: Eigen version 3.3.90, please set Eigen3_DIR.
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Found installed version of gflags: G:/lib/install/gflags/lib/cmake/gflags
-- Detected gflags version: 2.2.1
-- Failed to find Ceres - Missing required Ceres dependency: Eigen version 3.3.90, please set Eigen3_DIR.
-- Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h
-- Module opencv_sfm disabled because the following dependencies are not found: Glog/Gflags

I used flags -DEigen3_DIR:PATH="$myRepo"/Eigen \ to build opencv

full cmakeoutput : ceres2.txt

@alalek
Copy link
Member Author

alalek commented Nov 2, 2020

@LaurentBerger Thank you for checking this!

Failed to find Ceres - Missing required Ceres dependency: Eigen version 3.3.90, please set Eigen3_DIR.
Eigen3_DIR

Please try PR about Eigen from the main repository: opencv/opencv#18699
(Ceres scripts are too strict)

Failed to find glog

Do you have glog installed? Try to pass Glog_DIR CMake variable.

@NikolausDemmel
Copy link

Thanks for the explanation.

So, OpenCV modules which uses Ceres will be automatically compiled with C++14.

Yes that makes sense. I didn't realize ceres sets these properties. I'm not sure, but hopefully there are no (binary) incompatibilities if you mix the libraries compiled with C++11 and C++14.

For mixing C++99 and C++14 it may be different (thinking about a possible backport to opencv3, to also make that compatible with ceres 2.0.0.

Forced C++11 mode causes compilation errors with error messages near AddResidualBlock (there is difference in processing of variadic template parameters between C++11/C++14).

Exactly. I've been investigating over at Homebrew/homebrew-core#63596. Maybe you can also give your opinion there what would be the best solution, if you can. This patch, once released, would solve the issue with opencv 4 at least, I believe. 👍

BTW, Possible workaround for non-patched OpenCV is forcing -DCMAKE_CXX_STANDARD=14 through OpenCV CMake command line.

Yes, exactly. I've added those to test with the homebrew formulas opencv and opencv@3. I haven't checked if all are required, but it works if you pass

      -DOPENCV_SKIP_CMAKE_CXX_STANDARD=ON
      -DCMAKE_CXX_STANDARD=14
      -DCMAKE_CXX_EXTENSIONS=OFF

for opencv 4 and

      -DCMAKE_CXX_STANDARD=14
      -DHAVE_CXX11=ON
      -DCMAKE_CXX_EXTENSIONS=OFF

for opencv 3. But for homebrew that is not really an option I think, as I'm not sure if it causes some incompatibilities with users of opencv that use C++99 or C++11.

@@ -57,23 +90,11 @@ set(LIBMV_LIGHT_LIBS
if(Ceres_FOUND)
add_definitions("-DCERES_FOUND=1")
list(APPEND LIBMV_LIGHT_LIBS simple_pipeline)
list(APPEND LIBMV_LIGHT_INCLUDES "${CERES_INCLUDE_DIR}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might this cause a build failure with ceres 1.14, if that is not in a default include path (assuming retaining compatibility with 1.14 is a goal)? It looks to me like ceres 2.0.0 sets the right target properties w.r.t. the include paths, but ceres 1.14 doesn't seem to do so. Otoh with ceres 2.0.0 CERES_INCLUDE_DIRS is not set at all anymore, so you would need to have a conditional to avoid adding an empty include path. In any case, it should be CERES_INCLUDE_DIRS not ..._DIR, I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (in sfm in rgbd modules).

@LaurentBerger
Copy link
Contributor

I update my cmake to 3.18.4
in my cmake script I think my eigen3_dir path was wrong now

and results now is :

-- Caffe:   NO
-- Protobuf:   NO
-- Glog:   NO
-- freetype2:   NO
-- harfbuzz:    NO
-- Julia not found. Not compiling Julia Bindings.
-- Module opencv_ovis disabled because OGRE3D was not found
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Found installed version of gflags: G:/lib/install/gflags/lib/cmake/gflags
-- Detected gflags version: 2.2.1
CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:273 (message):
  The package name passed to `find_package_handle_standard_args` (Gflags)
  does not match the name of the calling package (gflags).  This can lead to
  problems in calling code that expects `find_package` result variables
  (e.g., `_FOUND`) to follow a certain pattern.
Call Stack (most recent call first):
  G:/Lib/install/ceres-solver/CMake/FindGflags.cmake:581 (find_package_handle_standard_args)
  G:/Lib/install/ceres-solver/CMake/CeresConfig.cmake:256 (find_package)
  G:/Lib/opencv_contrib/modules/rgbd/CMakeLists.txt:3 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Found Gflags: G:/Lib/install/gflags/include
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Found installed version of gflags: G:/lib/install/gflags/lib/cmake/gflags
-- Detected gflags version: 2.2.1
-- Checking Ceres
-- Found installed version of gflags: G:/lib/install/gflags/lib/cmake/gflags
-- Detected gflags version: 2.2.1
CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:273 (message):
  The package name passed to `find_package_handle_standard_args` (Gflags)
  does not match the name of the calling package (gflags).  This can lead to
  problems in calling code that expects `find_package` result variables
  (e.g., `_FOUND`) to follow a certain pattern.
Call Stack (most recent call first):
  G:/Lib/install/ceres-solver/CMake/FindGflags.cmake:581 (find_package_handle_standard_args)
  G:/Lib/install/ceres-solver/CMake/CeresConfig.cmake:256 (find_package)
  G:/Lib/opencv_contrib/modules/sfm/CMakeLists.txt:9 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Checking SFM deps... FALSE

full output
new.txt

@alalek
Copy link
Member Author

alalek commented Nov 2, 2020

Checking SFM deps... FALSE

@LaurentBerger Please attach logs of check_glog_gflags.cpp compilation from CMakeFiles/CMakeError.log

@LaurentBerger
Copy link
Contributor

@alalek where are logs of check_glog_gflags.cpp? I cannot found something in opencv build repo

opencvbuild/CMakeFiles/CMakeError.log
CMakeError.log

endif()
set(SFM_GLOG_GFLAGS_TEST "${SFM_GLOG_GFLAGS_TEST}" CACHE INTERNAL "")
set(SFM_GLOG_GFLAGS_TEST_CACHE_KEY "${__cache_key}" CACHE INTERNAL "")
message(STATUS "Checking SFM glog/gflags deps... ${SFM_GLOG_GFLAGS_TEST}")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LaurentBerger

new.txt
-- Checking SFM deps... FALSE

Logs don't correspond to the content of this patch.
Please re-check that patch is applied properly.

Copy link
Contributor

@LaurentBerger LaurentBerger Nov 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed compilation check: G:/Lib/opencv_contrib/modules/sfm/cmake/checks/check_glog_gflags.cpp
Change Dir: G:/Lib/build/opencv/CMakeFiles/CMakeTmp

Run Build Command(s):C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/Bin/MSBuild.exe cmTC_32546.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=15.0 /v:m && Microsoft (R) Build Engine version 15.9.21+g9802d43bc3 pour .NET Framework

Copyright (C) Microsoft Corporation. Tous droits réservés.



  Compilateur d'optimisation Microsoft (R) C/C++ version 19.16.27041 pour x64

  Copyright (C) Microsoft Corporation. Tous droits réservés.

  

  cl /c /IG:\Lib\install\gflags\include /Zi /W4 /WX- /diagnostics:classic /MP /Od /Ob0 /Oi /D WIN32 /D _WINDOWS /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /D GFLAGS_IS_A_DLL=1 /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /EHa /RTC1 /MDd /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"cmTC_32546.dir\Debug\\" /Fd"cmTC_32546.dir\Debug\vc141.pdb" /Gd /TP /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /errorReport:queue  /bigobj G:\Lib\opencv_contrib\modules\sfm\cmake\checks\check_glog_gflags.cpp

  check_glog_gflags.cpp

  

G:\Lib\opencv_contrib\modules\sfm\cmake\checks\check_glog_gflags.cpp(1): fatal error C1083: Impossible d'ouvrir le fichier include : 'glog/logging.h' : No such file or directory [G:\Lib\build\opencv\CMakeFiles\CMakeTmp\cmTC_32546.vcxproj]

Is it good message ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see /IG:\Lib\install\gflags\include but don't see include directory for glog from this log.

Log above requests to specify GLOG_INCLUDE_DIR:

-- Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h

Could you dump used CMake options?

Copy link
Contributor

@LaurentBerger LaurentBerger Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I added :

-DEigen3_DIR:PATH=${installRepo}/Eigen3/share/eigen3/cmake \
-Dglog_DIR:PATH=${installRepo}/glog/lib/cmake/glog -Dgflags_DIR:PATH=${installRepo}/gflags/lib/cmake/gflags \
-DGLOG_INCLUDE_DIR=${installRepo}/glog/include \

and sfm is true :

new.txt

But now I have got an error building sfm :

1>------ Build started: Project: opencv_sfm, Configuration: Release x64 ------
1>simple_pipeline.cpp
1>G:\lib\install\glog\include\glog/log_severity.h(51): fatal error C1189: #error:  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
1>Done building project "opencv_sfm.vcxproj" -- FAILED.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fatal error C1189: #error: ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.

Added to handle that (not really related to Ceres 2.0.0):

add_definitions(/DGLOG_NO_ABBREVIATED_SEVERITIES)

@NikolausDemmel
Copy link

I tried this patched version of the contribs with the opencv Homebrew formula, and it seems to work fine on macOS: https://github.com/Homebrew/homebrew-core/runs/1349352444?check_suite_focus=true (the build failure is in another formula depending on ceres, rawtoaces).

Would it be reasonable to backport this to opencv 3? It looks like it still gets regular releases.

@alalek
Copy link
Member Author

alalek commented Nov 7, 2020

👍

@opencv-pushbot opencv-pushbot merged commit a35c1e3 into opencv:master Nov 7, 2020
@NikolausDemmel
Copy link

Thanks for working on this!

Opencv 3 / 4 seems to be on a ~ 3 month release schedule, so these patches should land some time in January 2021 I think?

@shr-project
Copy link

Hello, can you please check #2923 ?

Since this PR was merged the GLOG_LIBRARIES variable is empty whenever Ceres is detected and only Glog_LIBS is defined (and contains what used to be in GLOG_LIBRARIES), to fix build with gold, I had to add Glog_LIBS into LIBMV_LIGHT_LIBS to link with glog again without unresolved symbols in lib/libopencv_sfm.so.4.5.2.

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

Successfully merging this pull request may close these issues.

Ceres 2.0.0 upgrade to C++14 breaks sfm module
5 participants