Portable way to find out INCLUDE,LIBS,CXXFLAGS,LDFLAGS - how to compile my program on foreign systems?

I want to distribute my OpenCV source code to other users (on different OS’s). They need to compile it. Ideally in Unix/Linux there’s pkgconf which tells me what LDFLAGS (-l … -L) what CFLAGS (-I…) to use. But what happens when it is not present (case in point: OpenCV on some M$ windows).

I thought perhaps they will have CMAKE installed and use this script:

cmake_minimum_required(VERSION 3.1)
project(RUBBISH_CMAKE_PROJECT)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( _cmake_opencv_testme test.cpp )
target_link_libraries( _cmake_opencv_testme ${OpenCV_LIBS} )
message(STATUS "RUBBISH_CMAKE_PROJECT::INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "RUBBISH_CMAKE_PROJECT::LIBS: ${OpenCV_LIBS}")
message(STATUS "RUBBISH_CMAKE_PROJECT::LDFLAGS: ${OpenCV_EXTRA_COMPONENTS}")
message(STATUS "RUBBISH_CMAKE_PROJECT::CFLAGS: ${CMAKE_C_FLAGS}")
message(STATUS "RUBBISH_CMAKE_PROJECT::CXXFLAGS: ${CMAKE_CXX_FLAGS}")

Assuming that the recipients of my code do have cmake installed on their system the above does not output anything about library location! This is what I get:

-- RUBBISH_CMAKE_PROJECT::INCLUDE_DIRS: /usr/local/include/opencv4
-- RUBBISH_CMAKE_PROJECT::LIBS: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world;opencv_alphamat;opencv_aruco;opencv_barcode;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_cudaarithm;opencv_cudabgsegm;opencv_cudacodec;opencv_cudafeatures2d;opencv_cudafilters;opencv_cudaimgproc;opencv_cudalegacy;opencv_cudaobjdetect;opencv_cudaoptflow;opencv_cudastereo;opencv_cudawarping;opencv_cudev;opencv_datasets;opencv_dnn_objdetect;opencv_dnn_superres;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_hfs;opencv_img_hash;opencv_intensity_transform;opencv_line_descriptor;opencv_mcc;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_rapid;opencv_reg;opencv_rgbd;opencv_saliency;opencv_sfm;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_viz;opencv_wechat_qrcode;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
-- RUBBISH_CMAKE_PROJECT::LDFLAGS: 
-- RUBBISH_CMAKE_PROJECT::CFLAGS: 
-- RUBBISH_CMAKE_PROJECT::CXXFLAGS: 
-- Configuring done
CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Eigen3::Eigen" but the
  target was not found.  Perhaps a find_package() call is missing for an
  IMPORTED target, or an ALIAS target is missing?


CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Iconv::Iconv" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Eigen3::Eigen" but the
  target was not found.  Perhaps a find_package() call is missing for an
  IMPORTED target, or an ALIAS target is missing?


CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Eigen3::Eigen" but the
  target was not found.  Perhaps a find_package() call is missing for an
  IMPORTED target, or an ALIAS target is missing?


CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Eigen3::Eigen" but the
  target was not found.  Perhaps a find_package() call is missing for an
  IMPORTED target, or an ALIAS target is missing?


CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Eigen3::Eigen" but the
  target was not found.  Perhaps a find_package() call is missing for an
  IMPORTED target, or an ALIAS target is missing?


CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Ceres::ceres" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Eigen3::Eigen" but the
  target was not found.  Perhaps a find_package() call is missing for an
  IMPORTED target, or an ALIAS target is missing?


CMake Error at CMakeLists.txt:5 (add_executable):
  Target "_cmake_opencv_testme" links to target "Ceres::ceres" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?


-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

Additionally the above cmake run on my system, outputs a tonne of libraries but in my system OpenCV programs need only be linked to opencv_world – I have asked this explicitly during making. In fact, I do not think I have lib opencv_highgui because that fails:

c++ test.cpp -lopencv_highgui -I/usr/local/include/opencv4

with this error: /usr/bin/ld: cannot find -lopencv_highgui

And finally the above complains about other external libraries, well what’s their path?

CMake Error at CMakeLists.txt:5 (add_executable):  Target "_cmake_opencv_testme" links to target "Eigen3::Eigen" but the  target was not found.  Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?

So, is there a fool-proof, portable way to find out what are the complete compilation parameters for OpenCV on a foreign system I want to distribute my code assuming that OpenCV dev package is installed?

there is no official opencv dev package
(however, there are prebuilt libs for windows only)

the errors you show look like a broken binary install, built on a machine, where all those libs(eigen,iconv,ceres) were originally present, dont do that.

assuming, your own code does never need those 3rd party deps,

  • distribute your src code
  • let ppl build their own opencv libs, locally
  • use cmake to generate a makefile for your project

(By dev package I mean the OpenCV source code, compiled locally).

Ceres et al are present (at /usr/lib64), it was installed manually by me. Eigen3 was installed by Fedora’s dnf. LD_LIBRARY_PATH includes these paths. OpenCV was installed by downloading sources (v4.5.5 from official location). It discovered all these libraries during compilation. Eigen3Config.cmake resides in /usr/share/cmake CeresConfig.cmake in /usr/lib64/cmake/Ceres/.

cmake was installed using the package manager (dnf). Does OpenCV tries and install its own version of cmake ? (sometimes fedora packages are a version behind the most recent one). If yes, then this is a problem.

But let’s assume for a minute that this is a misconfiguration of my system.

I need to find OpenCV’s include dir (above cmake script provides that) and also library path (it does not seem there is a variable to contain that info in OpenCVConfig.cmake) and libraries to be linked. The reason for this is that I am writing a Perl module which will use OpenCV library calls. Perl has its own Makefile and all I need are the -I... -L... -l ... flags to link to OpenCV libs via this Makefile.

no

well, mine has OpenCV_LIB_PATH
in general, libs go to CMAKE_INSTALL_PREFIX, which is /usr/local/lib by default on linux

In 4.5.5 ./build/unix-install/OpenCVConfig.cmake does not have LIB_PATH neither does ./build/OpenCVConfig.cmake . If yours contains it, then it’s definetely my problem and I hope distributing the code with above CMAKE script will be OK. Thank you for your replies.