【问题标题】:calling .c file inside .cpp在 .cpp 中调用 .c 文件
【发布时间】:2018-08-16 09:50:30
【问题描述】:

我正在尝试从 C++ 文件 (main.cpp) 调用 C 文件 (dispmanx.c)。

dispmanx.c:

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>

#include "bcm_host.h"

#define WIDTH   200
#define HEIGHT  200

#ifndef ALIGN_UP
#define ALIGN_UP(x,y)  ((x + (y)-1) & ~((y)-1))
#endif

int run(unsigned char* fileData)
{
    typedef struct
    {
        DISPMANX_DISPLAY_HANDLE_T   display;
        DISPMANX_MODEINFO_T         info;
        void                       *image;
        DISPMANX_UPDATE_HANDLE_T    update;
        DISPMANX_RESOURCE_HANDLE_T  resource;
        DISPMANX_ELEMENT_HANDLE_T   element;
        uint32_t                    vc_image_ptr;

    } RECT_VARS_T;

    static RECT_VARS_T  gRectVars;


    RECT_VARS_T    *vars;
    uint32_t        screen = 1;
    int             ret;
    VC_RECT_T       src_rect;
    VC_RECT_T       dst_rect;
    VC_IMAGE_TYPE_T type = VC_IMAGE_RGB565;
    int width = WIDTH, height = HEIGHT;
    int pitch = ALIGN_UP(width*2, 32);
    int aligned_height = ALIGN_UP(height, 16);
    VC_DISPMANX_ALPHA_T alpha = { DISPMANX_FLAGS_ALPHA_FROM_SOURCE | DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS, 
                             120, /*alpha 0->255*/
                             0 };

    vars = &gRectVars;

    bcm_host_init();

    printf("Open display[%i]...\n", screen );
    vars->display = vc_dispmanx_display_open( screen );

    ret = vc_dispmanx_display_get_info( vars->display, &vars->info);
    assert(ret == 0);
    printf( "Display is %d x %d\n", vars->info.width, vars->info.height );

    vars->image = fileData;

//  vars->image = calloc(1, pitch * height);
    assert(vars->image);

    vars->resource = vc_dispmanx_resource_create( type,
                                                  width,
                                                  height,
                                                  &vars->vc_image_ptr );
    assert( vars->resource );
    vc_dispmanx_rect_set( &dst_rect, 0, 0, width, height);
    ret = vc_dispmanx_resource_write_data(  vars->resource,
                                            type,
                                            pitch,
                                            vars->image,
                                            &dst_rect );


    assert( ret == 0 );
    vars->update = vc_dispmanx_update_start( 10 );
    assert( vars->update );

    vc_dispmanx_rect_set( &src_rect, 0, 0, width << 16, height << 16 );

    vc_dispmanx_rect_set( &dst_rect, ( vars->info.width - width ) / 2,
                                     ( vars->info.height - height ) / 2,
                                     width,
                                     height );

    vars->element = vc_dispmanx_element_add(    vars->update,
                                                vars->display,
                                                2000,               // layer
                                                &dst_rect,
                                                vars->resource,
                                                &src_rect,
                                                DISPMANX_PROTECTION_NONE,
                                                &alpha,
                                                NULL,             // clamp
                                                VC_IMAGE_ROT0 );

    ret = vc_dispmanx_update_submit_sync( vars->update );
    assert( ret == 0 );

    printf( "Sleeping for 10 seconds...\n" );
    sleep( 10 );

    vars->update = vc_dispmanx_update_start( 10 );
    assert( vars->update );
    ret = vc_dispmanx_element_remove( vars->update, vars->element );
    assert( ret == 0 );
    ret = vc_dispmanx_update_submit_sync( vars->update );
    assert( ret == 0 );
    ret = vc_dispmanx_resource_delete( vars->resource );
    assert( ret == 0 );
    ret = vc_dispmanx_display_close( vars->display );
    assert( ret == 0 );

    return 0;
}

#ifdef __cplusplus
}
#endif

main.cpp:

#include "dispmanx.c"


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

using namespace cv;
using namespace std;
int main()
{
    Mat image;
    image = imread("t.png", IMREAD_COLOR);   // Read the file
    run(image.data);
}

代码应该获取一个 png 文件的地址,使用 OpenCV 库获取它的数据,并将其传递给在屏幕上显示图片的 C 程序。

我可以编译 dispmanx.c:

pi@raspberrypi:~/openCVtest.1 $ gcc -I/opt/vc/include/
    -I/opt/vc/include/interface/vcos/pthreads
    -I/opt/vc/include/interface/vmcs_host/linux
    -I/opt/vc/src/hello_pi/libs/ilclient
    -I/opt/vc/src/hello_pi/libs/vgfont
    -L/opt/vc/lib/ -L/opt/vc/src/hello_pi/libs/ilclient
    -L/opt/vc/src/hello_pi/libs/vgfont
    -c dispmanx.c
    -o dispmanx.o
    -lbrcmGLESv2 -lbrcmEGL -lopenmaxil -lbcm_host -lvcos -lvchiq_arm -lpthread -lrt -lm

但是当我尝试编译 main.cpp 时:

pi@raspberrypi:~/openCVtest.1 $ g++ -I/opt/vc/include/     -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/src/hello_pi/libs/ilclient -I/opt/vc/src/hello_pi/libs/vgfont -L/opt/vc/lib/ -L/opt/vc/src/hello_pi/libs/ilclient -L/opt/vc/src/hello_pi/libs/vgfont -c main.cpp dispmanx.o  -o main.o -lbrcmGLESv2 -lbrcmEGL -lopenmaxil -lbcm_host -lvcos -lvchiq_arm -lpthread -lrt -lm               In file included from main.cpp:2:0:

dispmanx.c:在函数“int run(unsigned char*)”中: dispmanx.c:52:68:错误:从“int”到“DISPMANX_FLAGS_ALPHA_T”的无效转换 [-fpermissive] VC_DISPMANX_ALPHA_T alpha = { DISPMANX_FLAGS_ALPHA_FROM_SOURCE | DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~

dispmanx.c:111:63: error: cannot convert ‘VC_IMAGE_TRANSFORM_T’ to ‘DISPMANX_TRANSFORM_T’ for argument ‘10’ to ‘DISPMANX_ELEMENT_HANDLE_T vc_dispmanx_element_add(DISPMANX_UPDATE_HANDLE_T, DISPMANX_DISPLAY_HANDLE_T, int32_t, const VC_RECT_T*, DISPMANX_RESOURCE_HANDLE_T, const VC_RECT_T*, DISPMANX_PROTECTION_T, VC_DISPMANX_ALPHA_T*, DISPMANX_CLAMP_T*, DISPMANX_TRANSFORM_T)’
                                             VC_IMAGE_ROT0 );

我已经分别测试了这两个程序和功能,但是当我尝试混合它们时,我得到了我无法理解的错误。

我是一名学生,具有初级知识水平。我做错了什么?

【问题讨论】:

  • 您可能希望显示dispmanx.c 中的第111 行的位置。数到 111 并找到它是可能的,但最好通过标记 // The error is here 或类似的东西让读者更容易阅读。你可以edit你的问题去做。

标签: c++ c raspbian


【解决方案1】:

鉴于dispmanx.c的这些内容:

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>

main.cpp

开头
#include "dispmanx.c"

你会遇到严重的问题。

您已经用extern "C" 包装了整个标准头文件(例如unistd.h),然后#include 将它们放入C++ 文件中。

它们不应该在 C++ 代码中以这种方式使用。

#include'ing 源文件(.c.cpp 等)首先是一个根本性的坏主意。跨不同语言(如 C 和 C++)执行此操作,然后使用 extern "C" 不正确地包装系统标头更糟糕。您不能安全地将 C 代码编译为 C++ 代码,当然也不能使用 extern "C" 包装系统头文件以用于 C++ 代码。

extern "C" 不能保证使用 C++ 编译器编译 C 代码是安全的。它们是不同的语言,有细微的区别。

【讨论】:

  • 公平地说,这些都是 C 标头,并且可能有自己的 #ifdef/extern "C" 组合
  • 感谢您提供非常有用的信息,如果使用 extern "c" 进行包装不是一个好方法,您对解决我的问题有何建议?如果我为 dispmanx.c 创建一个主要方法并为我的 cpp 代码(包含 OpenCV 库)尝试一个包装器,这会是一个更好的选择吗?
  • @LightnessRacesinOrbit 但是Linux glibc stdio.h headerDefine ISO C stdio on top of C++ iostreams. 评论开头文件。
  • @AndrewHenle 我不知道那条评论在那里做了什么,但是 glibc I/O 是 C++ 标准库的 IOstreams 的包装器是难以置信的。
  • @Ben 您使用 C 编译器编译 C 代码,使用 C++ 编译器编译 C++ 代码,而不是将它们链接在一起。有关示例,请参见 stackoverflow.com/questions/31903005/…
【解决方案2】:

问题出在这里:

#include "dispmanx.c"

*.cpp 文件中包含*.c 文件不是一个好主意。 C 和 C++ 是相似的语言,所以它可能一开始看起来可以工作(即编译器报告的错误不是在第 1 行而是在第 111 行),但实际上并没有。

从 C++ 代码调用 C 代码的一种正确方法是提供如下声明:

// C++ code
#include <iostream>

extern "C" {
    int run(unsigned char* fileData); // declaration for the C code
}

...
int main()
{
    ...
    run(image.data); // calling the C code
}

C 代码的声明可以在*.cpp 文件中,或者更常规地,在单独的专用*.h 文件中。如果您的 C 代码在 dispmanx.c 中,那么常规名称是 dispmanx.h

// dispmanx.h
extern "C" {
    int run(unsigned char* fileData); // declaration for the C code
}
// main.cpp
#include <iostream>
#include "dispmanx.h"
...
int main()
{
    ...
    run(image.data); // calling the C code
}

另外,另一个约定:如果你想让你的新 dispmanx.h 文件在 C 和 C++ 中都可以编译,你应该对 C 编译器隐藏 extern "C" 部分,如 this question and answer 中所述。

【讨论】:

    猜你喜欢
    • 2017-09-12
    • 2011-11-12
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多