4

我从教程中获得了以下 directFB 示例代码:

#include <stdio.h>
#include <unistd.h>

#include <directfb/directfb.h>

static IDirectFB *dfb = NULL;
static IDirectFBSurface *primary = NULL;
static int screen_width = 0;
static int screen_height = 0;
#define DFBCHECK(x...)                                         \
  {                                                            \
    DFBResult err = x;                                         \
                                                               \
    if (err != DFB_OK)                                         \
      {                                                        \
        fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
        DirectFBErrorFatal( #x, err );                         \
      }                                                        \
  }
int main(int argc, char **argv) {

    DFBSurfaceDescription dsc;
    DFBCHECK(DirectFBInit (&argc, &argv));
    DFBCHECK(DirectFBCreate (&dfb));
    DFBCHECK(dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
    dsc.flags = DSDESC_CAPS;
    dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
    DFBCHECK(dfb->CreateSurface( dfb, &dsc, &primary ));
    DFBCHECK(primary->GetSize (primary, &screen_width, &screen_height));
    DFBCHECK(primary->FillRectangle (primary, 0, 0, screen_width, screen_height));
    DFBCHECK(primary->SetColor (primary, 0x80, 0x80, 0xff, 0xff));
    DFBCHECK(primary->DrawLine (primary, 0, screen_height / 2, screen_width - 1, screen_height / 2));
    DFBCHECK(primary->Flip (primary, NULL, 0));
    sleep(5);
    primary->Release(primary);
    dfb->Release(dfb);
    return 23;
}

我正在尝试编译并得到错误(来自 gnome 终端):

$ g++ Test01HorizontalLine.cpp -I /usr/local/include/directfb/
Test01HorizontalLine.cpp: In function ‘int main(int, char**)’:
Test01HorizontalLine.cpp:27: error: invalid conversion from ‘int’ to ‘DFBSurfaceCapabilities’
Test01HorizontalLine.cpp:33: error: invalid conversion from ‘int’ to ‘DFBSurfaceFlipFlags’

我正在使用 DFB 1.4.11(我无法更新我当前的项目)。我怎样才能使这个示例运行?(新手)

- 更新

更新行后(如@jclin所述):

dsc.caps = DFBSurfaceCapabilities(DSCAPS_PRIMARY | DSCAPS_FLIPPING);  
...  
primary->Flip(primary, NULL, DFBSurfaceFlipFlags(0));  

结果是:

$ g++ Test01HorizontalLine.cpp -I /usr/local/include/directfb/
/tmp/cc6iUS1W.o: In function `main':
Test01HorizontalLine.cpp:(.text+0x1c): undefined reference to `DirectFBInit'
Test01HorizontalLine.cpp:(.text+0x61): undefined reference to `DirectFBErrorFatal'
Test01HorizontalLine.cpp:(.text+0x6d): undefined reference to `DirectFBCreate'
Test01HorizontalLine.cpp:(.text+0xb2): undefined reference to `DirectFBErrorFatal'
Test01HorizontalLine.cpp:(.text+0x111): undefined reference to `DirectFBErrorFatal'
Test01HorizontalLine.cpp:(.text+0x188): undefined reference to `DirectFBErrorFatal'
Test01HorizontalLine.cpp:(.text+0x1ef): undefined reference to `DirectFBErrorFatal'
Test01HorizontalLine.cpp:(.text+0x26d): undefined reference to `DirectFBErrorFatal'
/tmp/cc6iUS1W.o:Test01HorizontalLine.cpp:(.text+0x2e4): more undefined references to `DirectFBErrorFatal' follow
collect2: ld returned 1 exit status
4

3 回答 3

4

下面的代码适用于我的测试 ubuntu12,我可能与你有不同的 directfb,但应该是相似的:

#include <stdio.h>
#include <unistd.h>

#include <directfb/directfb.h>

static IDirectFB *dfb = NULL;
static IDirectFBSurface *primary = NULL;
static int screen_width = 0;
static int screen_height = 0;
#define DFBCHECK(x...)                                         \
  {                                                            \
    DFBResult err = x;                                         \
                                                               \
    if (err != DFB_OK)                                         \
      {                                                        \
        fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
        DirectFBErrorFatal( #x, err );                         \
      }                                                        \
  }
int main(int argc, char **argv) {

    DFBSurfaceDescription dsc;
    DFBCHECK(DirectFBInit (&argc, &argv));
    DFBCHECK(DirectFBCreate (&dfb));
    DFBCHECK(dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
    dsc.flags = DSDESC_CAPS;
    dsc.caps = DFBSurfaceCapabilities(DSCAPS_PRIMARY | DSCAPS_FLIPPING);
    DFBCHECK(dfb->CreateSurface( dfb, &dsc, &primary ));
    DFBCHECK(primary->GetSize (primary, &screen_width, &screen_height));
    DFBCHECK(primary->FillRectangle (primary, 0, 0, screen_width, screen_height));
    DFBCHECK(primary->SetColor (primary, 0x80, 0x80, 0xff, 0xff));
    DFBCHECK(primary->DrawLine (primary, 0, screen_height / 2, screen_width - 1, screen_height / 2));
    primary->Flip(primary, NULL, DFBSurfaceFlipFlags(0));
    sleep(5);
    primary->Release(primary);
    dfb->Release(dfb);
    return 23;
}

使用以下命令行构建:

 g++ -o testHorizonLine  Test01HorizontalLine.cpp -I/usr/include \
 -I/usr/include/directfb -L/usr/lib/i386-linux-gnu -ldirectfb

要查找 libdirectfb.so 的位置,您可以使用 linuxfind命令,然后将-L选项添加到 g++ 命令以包含库路径并添加-ldirectfb到链接 libdirectfb.so 文件。

$ find /usr/lib -name libdirectfb.so   

运行 testhorizo​​nLine 输出:

   ~~~~~~~~~~~~~~~~~~~~~~~~~~| DirectFB 1.2.10 |~~~~~~~~~~~~~~~~~~~~~~~~~~
        (c) 2001-2008  The world wide DirectFB Open Source Community
        (c) 2000-2004  Convergence (integrated media) GmbH
      ----------------------------------------------------------------

(*) DirectFB/Core: Single Application Core. (2012-05-21 06:43)
(!) Direct/Util: opening '/dev/fb0' failed
    --> Permission denied
(!) DirectFB/FBDev: Error opening framebuffer device!
(!) DirectFB/FBDev: Use 'fbdev' option or set FRAMEBUFFER environment variable.
(!) DirectFB/Core: Could not initialize 'system_core' core!
    --> Initialization error!
Test01HorizontalLine.cpp <24>:
        (#) DirectFBError [DirectFBCreate (&dfb)]: Initialization error!
于 2013-01-24T02:59:00.093 回答
3

你能试试这个吗,因为我现在没有这样的版本。

dsc.caps = DFBSurfaceCapabilities(DSCAPS_PRIMARY | DSCAPS_FLIPPING);  
...  
primary->Flip(primary, NULL, DFBSurfaceFlipFlags(0));  
于 2013-01-17T09:46:03.557 回答
3

如果您在项目中使用库,包括标题只是工作的一半。您还必须显示实际库(在您的情况下为libdirectfb.so)的位置,以便它可以链接到您的代码。通过传递-L-l选项来做到这一点(另请注意,前缀 lib- 和 externsion .so 都不是必需的):

g++ your_source_file.cpp -I /path/to/include -L/path/to/lib -ldirectfb

关于如何在项目中使用库有很多主题,即您可能想从这里这里开始

PS 使用-Wall选项来查看正在发生的事情的更多信息总是一个好主意。

于 2013-01-25T12:19:31.830 回答