1

I encountered a strange issue in C++ and OpenCV2. The following code does not print "I ran!":

#include <iostream>

#include <opencv2/opencv.hpp>

// Opens image as grayscale and saves it to save_dir
int grayscale_file(const cv::String &file_dir, const std::string &save_dir){
    cv::Mat fi = cv::imread(file_dir, cv::ImreadModes::IMREAD_GRAYSCALE);// Loads image as grayscale

    return cv::imwrite(save_dir, fi);
}

int main(int argc, char* argv[]){
    std::cout << "I ran!" << std::endl;
    return 0;
}

However when I remove the code inside grayscale_file, it prints "I ran!":

#include <iostream>

#include <opencv2/opencv.hpp>

// Opens image as grayscale and saves it to save_dir
int grayscale_file(const cv::String &file_dir, const std::string &save_dir){
    return 0;
}

int main(int argc, char* argv[]){
    std::cout << "I ran!" << std::endl;
    return 0;
}

Why does the first piece of code prevent "I ran!" from being printed to the terminal, whereas the second piece of code doesnt?

Edit: Commenting some things out lead me to the issue of the cv::imread function. Removing the line that uses this lets the program run. I found a post here that explains it pretty well. I'll find a debug library instead of the release that I think I was using.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 4
    Are you sure you did not get build errors with the first code? I don't see any reason for a run time error or UB especially since the function was never called. – drescherjm Oct 08 '22 at 02:34
  • 1
    What if you put in keyboard smash instead? I bet you have a build error. – tadman Oct 08 '22 at 02:40
  • @drescherjm It was able to compile just fine. It looks like whenever I just include OpenCV it breaks. Am I passing my arguments wrong? –  Oct 08 '22 at 02:42
  • Either it compiles, in which case the `main()` function runs, or it doesn't. Set a breakpoint in `main()` and run with tracing. – tadman Oct 08 '22 at 02:47
  • @JaMiT So sorry. Whenever I try to use it inside of the function it doesn't allow `main()` to be ran. I'll try and find out if there were any possible errors. –  Oct 08 '22 at 02:52
  • 4
    "it doesn't allow main() to be ran" is not a thing that happens. Run in a debugger. See where it actually goes. Maybe your library bombs out before `main()` can engage, sure, but you need to know what's up. – tadman Oct 08 '22 at 02:59
  • If there's a static variable somewhere it will be initialized before `main` starts. If that hangs then main might never start. If the static variable is part of the cv library it will only be included if there's a call that forces the library to be linked. – Mark Ransom Oct 08 '22 at 03:02
  • 2
    What you might be missing: many IDEs, when they are told to build and run, will *run the last version of the program that compiled, even if there was an error that prevented building the current source code*. – Karl Knechtel Oct 08 '22 at 03:10
  • @TomTheCat Ah, you probably intended "include" in a more colloquial sense than `#include`. Good idea to stick to phrasing this as "use". ;) – JaMiT Oct 08 '22 at 03:45
  • @JaMiT My bad. Whenever I use `cv::imread` it doesn't exactly run `main()`. My issue was that I was using the release libraries instead of the debug libraries. Since I'm on Windows, it's going to be a rough time trying to get it to work. –  Oct 08 '22 at 03:49
  • MSVC builds in RelWithDebug mode even though Release is selected. Try adding `CV_IGNORE_DEBUG_BUILD_GUARD` to compile options. [related](https://stackoverflow.com/questions/47166710/can-i-use-a-release-config-of-opencv-with-debug-config-of-my-app) – Burak Oct 08 '22 at 08:06
  • Make sure you use the same variant of the Microsoft VC runtime library during opencv build snd your build. You can see a lot of undefined behaviour from heap corruptions otherwise. – Micka Oct 08 '22 at 08:24

1 Answers1

0

After weeks of head-scratching and research, I eventually figured out that my debugger was throwing an error at me (as pointed out in the comments). I was able to find info on fixing it from this question, which lead me to believe that my libraries were still not properly linked.

I am not 100% sure on why or how the error came to be, or how I fixed it, but I essentially used MSYS2's library installer to install OpenCV via this page. Information on the pacman command and installing the libraries was originally found on this question.

My final code and *.json files are as follows:

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:/msys64/mingw64/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-fdiagnostics-color=always",
                "-I\"C:\\msys64\\mingw64\\include\\opencv4\"",
                "-L\"C:\\msys64\\mingw64\\bin\"",
                "-lopencv_imgcodecs460",
                "-lopencv_highgui460",
                "-lopencv_core460",
                "-o",
                "${fileDirname}/corrscii.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

c_pp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/Program Files/Cpp_Libs/OpenCV-MinGW-Build-OpenCV-4.5.5-x64/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu17",
            "intelliSenseMode": "windows-gcc-x64",
            "cppStandard": "c++23"
        }
    ],
    "version": 4
}

main.cpp

#include <iostream>

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>

int main(int argc, char* argv[]){
    cv::String img_path = "C:\\path\\to\\a_photo.png";
    cv::Mat img = cv::imread(img_path);

    if (img.empty()){
        std::cout << "Error: inputted file cannot be read" << std::endl;
        return 1;
    };

    return 0;
}

I hope this helps anybody who may come across it.