未从 main.cpp C++ 调用 C 函数

Posted

技术标签:

【中文标题】未从 main.cpp C++ 调用 C 函数【英文标题】:C function not called from main.cpp C++ 【发布时间】:2017-08-17 13:26:33 【问题描述】:
extern "C" 
#include "CLAHE.h"

#include "opencv2/opencv.hpp"       // opencv general include file
#include "opencv2/highgui/highgui.hpp"  // opencv GUI include file
using namespace cv;

#include <vector>

#include "stdlib.h"

#include <QDebug>

#define WIDTH 704
#define HEIGHT 576

int main(int argc, int * argv[])

    int width = 0, height = 0;
    unsigned char * cbuf, *obuf;

    Mat gray;
    Mat img = imread("path/to/image", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
    cvtColor(img, gray, CV_BGR2GRAY);

    width = gray.cols; height = gray.rows;

    qDebug() << "Width = " << width << ", Height = " << height;

    CLAHE_OBJ *obj = new CLAHE_OBJ;

    init_clahe(obj, width, height, 64, 64, 256, 0, 255);

    return 0;

CLAHE.h 具有 CLAHE_OBJ 结构:

typedef struct CLAHE_OBJ

    int *m_blocks_histogram;
    int *m_blocks_he_map;
    int m_image_width;
    int m_image_height;
    int m_image_pixels;
    int m_window_size_wd;
    int m_window_size_ht;
    int m_window_pixels;
    int m_width_blocks_num;
    int m_height_blocks_num;
    int m_blocks_num;

    //optional
    int m_grayscale_levels; //Default: 256, Max: 256
    int *m_map_to_histogram_bins;
    int m_input_min_pixel_value;
    int m_input_max_pixel_value;

CLAHE_OBJ;

int init_clahe(CLAHE_OBJ *clahe_obj, int frame_width, int frame_height,
               int window_size_width, int window_size_height,
               int grayscale_levels, int min_pixel_value, int max_pixel_value)

    printf("Break point 1\n");

    if(frame_width <= 0 || frame_height <= 0)
    
        printf("Frame width and height must be greater that 0.");
        return -1;
    

    printf("Break point 2\n");

    if(grayscale_levels == 0)
    
        printf("Number of grayscale values must be greater than 0.");
        return -1;
    

    if(frame_width % window_size_width > 0 ||
            frame_height % window_size_height > 0)
    
        printf("Set window size (width, height) such that the frame can be divided"
               " into equal sized blocks,i.e., "
               "frame_width % window_size_width == 0;frame_height % window_size_height > 0;");
        return -1;
    

    printf("Break point 3\n");

    clahe_obj->m_image_width = frame_width;
    clahe_obj->m_image_height = frame_height;
    clahe_obj->m_image_pixels = frame_width * frame_height;
    clahe_obj->m_window_size_wd = window_size_width;
    clahe_obj->m_window_size_ht = window_size_height;
    clahe_obj->m_window_pixels = window_size_height * window_size_width;

    if((clahe_obj->m_width_blocks_num = frame_width / window_size_width) < 2 ||
    (clahe_obj->m_height_blocks_num = frame_height / window_size_height) < 2)
    
        printf("Number of blocks that the frame can be divide into must be greater than 4, i.e.,"
               "frame_width / window_size_width >= 2;frame_height / window_size_height >= 2;");
        return -1;
    

    printf("Break point 4\n");

    clahe_obj->m_blocks_num = clahe_obj->m_width_blocks_num * clahe_obj->m_height_blocks_num;

    if(clahe_obj->m_blocks_num > 256)
    
        printf("Number of blocks that the frame can be divide into must be lesser than 256.");
        return -1;
    

    clahe_obj->m_grayscale_levels = (grayscale_levels >= 256) ? 256 : grayscale_levels;
    map_to_histogram_bins(clahe_obj, min_pixel_value, max_pixel_value);

    clahe_obj->m_blocks_histogram = (int*)malloc(clahe_obj->m_blocks_num *
                                                 clahe_obj->m_grayscale_levels *
                                                 sizeof(int));
    if(!clahe_obj->m_blocks_histogram)
    
        printf("Unable to allocate memory for histograms for each block");
        return -1;
    
    memset(clahe_obj->m_blocks_histogram, 0, clahe_obj->m_blocks_num *
           clahe_obj->m_grayscale_levels * sizeof(int));

    printf("Break point 5\n");

    clahe_obj->m_blocks_he_map = (int*)malloc(clahe_obj->m_blocks_num *
                                              clahe_obj->m_grayscale_levels *
                                              sizeof(int));
    if(!clahe_obj->m_blocks_he_map)
    
        printf("Unable to allocate memory for equalized histograms for each block");
        return -1;
    

    printf("Break point 6\n");

    return 0;

使用“imread”读取输入图像后程序立即崩溃。 'init_clahe' 永远不会被调用,即 printf("Break Point ..") 永远不会被执行。我无法弄清楚出了什么问题。我什至尝试过使用 malloc 而不是 new,但问题仍然存在。

请帮忙。

编辑#1: qDebug() 语句被执行。我确实得到了正确的宽度和高度值。

【问题讨论】:

'程序在读取输入图像后立即崩溃' - 它读取了吗? 'imread()' 如何标记错误?它是否引发异常,('因为我看不到任何捕获处理程序?) 您不能使用调试器来指示崩溃的位置(以及原因)吗? int main(int argc, int * argv[]) 导致未定义的行为(假设是托管环境)。 我应该将 int main() 改为 void main() 吗? @Scheff 你打错字了,假设你的意思是char **argvchar *argv[] 【参考方案1】:

您似乎正在将您的应用程序与第三方库链接。只需确保您对它们都使用相同的运行时库,而不是混合发布和调试运行时库。

【讨论】:

以上是关于未从 main.cpp C++ 调用 C 函数的主要内容,如果未能解决你的问题,请参考以下文章

C++ 变量值未从标头传递到主

在 C++ 程序中使用多个 .cpp 文件?

当我尝试在 C++ 中编译和运行我的“main.cpp”文件时,导致调用中止方法的原因是啥?

C++ STM32用户定义类构造函数问题

c语言静态函数调用问题

c++中关于私有静态变量的问题