PDCurses TUI C++ Win32 console app - Access violat

2019-07-23 09:55发布

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.

标签: c++ c pdcurses
2条回答
乱世女痞
2楼-- · 2019-07-23 10:22

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 :)

查看更多
3楼-- · 2019-07-23 10:41

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!

查看更多
登录 后发表回答