PDCurses TUI C++ Win32 console app - Access violation reading location

微笑、不失礼 提交于 2019-12-11 09:56:01

问题


I have downloaded pdcurses source and was able to successfully include curses.h in my project, linked the pre-compiled library and all good.

After few hours of trying out the library, I saw the tuidemo.c in the demos folder, compiled it into an executable and brilliant! exactly what I needed for my project.

Now the problem is that it's a C code, and I am working on a C++ project in VS c++ 2008.

The files I need are tui.c and tui.h How can I include that C file in my C++ code? I saw few suggestions here

but the compiler was not too happy with 100's of warnings and errors.

How can I go on including/using that TUI pdcurses includes!?

Thanks

EDIT:

I added extern "C" statement, so my test looks like this now, but I'm getting some other type of error

#include <stdio.h>
#include <stdlib.h>
using namespace std;

extern "C" {
   #include <tui.h>
}

void sub0()
{
    //do nothing
}

void sub1()
{
    //do nothing
}


int main (int argc, char * const argv[]) {

    menu MainMenu[] =
    {
        { "Asub", sub0, "Go inside first submenu" },
        { "Bsub", sub1, "Go inside second submenu" },
        { "", (FUNC)0, "" }   /* always add this as the last item! */
    };
    startmenu(MainMenu, "TUI - 'textual user interface' demonstration program");

    return 0;
}

Although it is compiling successfully, it is throwing an Error at runtime, which suggests a bad pointer:

0xC0000005: Access violation reading location 0x021c52f9

at line

startmenu(MainMenu, "TUI - 'textual user interface' demonstration program");

Not sure where to go from here. thanks again.


回答1:


If I'm not mistaken (and I could easily be), it's due to the difference in calling conventions for C/C++. Try making the callbacks extern "C", and make them call a C++ function. Call it a trampoline :)




回答2:


Finally got it working. The solution was in the steps below:

First I renamed tui.c to tui.cpp

For the header tui.h, I followed the exact same step of wrapping the code as described here.

then in my project i just included the header without any extern "C" block

#include "tui.h"

Compiled and it worked!



来源:https://stackoverflow.com/questions/3044603/pdcurses-tui-c-win32-console-app-access-violation-reading-location

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!