I am trying to read an image using OpenCV in C++ and the OpenCV is built and linked using vcpkg in manifesto mode.
Here is the code for main.cpp:
#include <opencv2/opencv.hpp>
#include <filesystem>
int main()
{
printf("Hello World with OpenCV\n");
std::string filename="/home/mans/MyData/BitBucket/test/vcpkg/dog.jpeg";
if (std::filesystem::exists(filename)) {
std::cout << "The file " << filename << " exists." << std::endl;
}
cv::Mat image=cv::imread(filename.c_str());
if (image.empty()) {
std::cout << "The image is empty." << std::endl;
}
else {
std::cout << "The image is not empty." << std::endl;
}
}
and this is the code for CmakeLists.txt:
cmake_minimum_required (VERSION 3.20)
if (DEFINED ENV{VCPKG_ROOT})
File(TO_CMAKE_PATH $ENV{VCPKG_ROOT} VCPKG_ROOT)
message(STATUS ${VCPKG_ROOT})
if(WIN32)
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
set(VCPKG_TARGET_TRIPLET "x64-windows-static" CACHE STRING "")
else(WIN32)
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "" FORCE)
set(VCPKG_TARGET_TRIPLET "x64-linux" CACHE STRING "")
endif(WIN32)
set(VCPKG_AVALIABLE TRUE)
else (DEFINED ENV{VCPKG_ROOT})
message(WARNING "VCPKG_ROOT is not defined, OpenCV will not be used to convert images.")
set(VCPKG_AVALIABLE FALSE)
endif()
project (test)
if (MSVC)
add_definitions (-D_USE_MATH_DEFINES)
add_definitions(/MP)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ot /openmp" )
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(CXX_STANDARD 20)
endif()
set(OpenCV_STATIC, ON)
FIND_PACKAGE(OpenCV REQUIRED)
if (OpenCV_FOUND)
add_definitions (-D_USE_OPENCV)
endif(OpenCV_FOUND)
add_executable(test main.cpp)
target_include_directories(test PUBLIC ${OpenCV_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(test ${OpenCV_LIBS})
and the vcpkg manifesto file is:
{
"dependencies":
[
{
"name": "opencv4",
"default-features": false
}
]
}
When I ran the application and try to read a jpeg file, I am getting this output:
Hello World with OpenCV
The file /home/mans/MyData/BitBucket/test/vcpkg/dog.jpeg exists.
The image is empty.
I think there is a problem with my manifesto file that doesn't include the jpeg library. I can not remove the line "default-features": false as then the system tries to build highgui for the opencv and it can not build it on my system.
How can instruct the vcpkg not to build vcpkg with highgui or ask it to build with jpeg and other default libraries?