2011-10-16 27 views
1

我試圖編譯在OS X上的項目是:https://github.com/Ramblurr/PietCreatorC:提領指向不完全類型的錯誤

上午不幸的是無法修復的問題,下面幾行:

width = info_ptr->width; 
height = info_ptr->height; 
ncol = 2 << (info_ptr->bit_depth - 1); 

即產生錯誤:

file.c: In function ‘read_png’: 
file.c:1117: error: dereferencing pointer to incomplete type 
file.c:1118: error: dereferencing pointer to incomplete type 
file.c:1119: error: dereferencing pointer to incomplete type 

低於read_png功能的全碼:

#include <png.h> 
#include <math.h> 

png_byte bit_depth; 

png_structp png_ptr; 
png_infop info_ptr; 
int number_of_passes; 
png_bytep * row_pointers; 


int 
read_png (char *fname) 
{ 
    char header [8]; 

    FILE *in; 
    int i, j, ncol, rc; 

    if (! strcmp (fname, "-")) { 
    /* read from stdin: */ 
    vprintf ("info: not trying to read png from stdin\n"); 
    return -1; 
    } 

    if (! (in = fopen (fname, "rb"))) { 
    fprintf (stderr, "cannot open `%s'; reason: %s\n", fname, 
     strerror (errno)); 
    return -1; 
    } 

    if (! in || (rc = fread (header, 1, 8, in)) != 8 
     || png_sig_cmp ((unsigned char *) header, 0, 8) != 0) { 
    return -1; 
    } 

    if (! (png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0)) 
     || ! (info_ptr = png_create_info_struct (png_ptr))) { 
    return -1; 
    } 

    png_init_io (png_ptr, in); 
    png_set_sig_bytes (png_ptr, 8); 

    png_read_png (png_ptr, info_ptr, 
     PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA 
     | PNG_TRANSFORM_EXPAND, NULL); 
    /**  | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_SHIFT **/ 

    row_pointers = png_get_rows (png_ptr, info_ptr); 

    width = info_ptr->width; 
    height = info_ptr->height; 
    ncol = 2 << (info_ptr->bit_depth - 1); 

    vprintf ("info: got %d x %d pixel with %d cols\n", width, height, ncol); 

    alloc_cells (width, height); 

    for (j = 0; j < height; j++) { 
    png_byte *row = row_pointers [j]; 
    for (i = 0; i < width; i++) { 

     png_byte *ptr = & row [i * 3]; 

     /* ncol always 256 ? */ 
     int r = (ptr [0] * 256)/ncol; 
     int g = (ptr [1] * 256)/ncol; 
     int b = (ptr [2] * 256)/ncol; 

     int col = ((r * 256 + g) * 256) + b; 
     int col_idx = get_color_idx (col); 

     if (col_idx < 0) { 
    if (unknown_color == -1) { 
     fprintf (stderr, "cannot read from `%s'; reason: invalid color found\n", 
      fname); 
     return -1; 
    } else { 
     /* set to black or white: */ 
     col_idx = (unknown_color == 0 ? c_black : c_white); 
    } 
     } 

     set_cell (i, j, col_idx); 
    } 
    } 

    return 0; 
} 
+0

「僅當應用程序直接訪問info_ptr或png_ptr時才存在二進制不兼容性通過png.h和編譯的應用程序的成員裝有不同版本的圖書館」 - 這個從我png.h. libpng 1.2.42。你正在使用哪個版本? – aditya

回答

1

我編譯在p在Mac OS X 10.6.8上成功完成項目。

git clone https://github.com/Ramblurr/PietCreator.git 
cd PietCreator 
mkdir build 
cd build 
cmake ../ 

-- The C compiler identification is GNU 
-- The CXX compiler identification is GNU 
-- Checking whether C compiler has -isysroot 
-- Checking whether C compiler has -isysroot - yes 
-- Checking whether C compiler supports OSX deployment target flag 
-- Checking whether C compiler supports OSX deployment target flag - yes 
-- Check for working C compiler: /usr/bin/gcc 
-- Check for working C compiler: /usr/bin/gcc -- works 
-- Detecting C compiler ABI info 
-- Detecting C compiler ABI info - done 
-- Checking whether CXX compiler has -isysroot 
-- Checking whether CXX compiler has -isysroot - yes 
-- Checking whether CXX compiler supports OSX deployment target flag 
-- Checking whether CXX compiler supports OSX deployment target flag - yes 
-- Check for working CXX compiler: /usr/bin/c++ 
-- Check for working CXX compiler: /usr/bin/c++ -- works 
-- Detecting CXX compiler ABI info 
-- Detecting CXX compiler ABI info - done 
-- Looking for Q_WS_X11 
-- Looking for Q_WS_X11 - not found. 
-- Looking for Q_WS_WIN 
-- Looking for Q_WS_WIN - not found. 
-- Looking for Q_WS_QWS 
-- Looking for Q_WS_QWS - not found. 
-- Looking for Q_WS_MAC 
-- Looking for Q_WS_MAC - found 
-- Looking for QT_MAC_USE_COCOA 
-- Looking for QT_MAC_USE_COCOA - found 
-- Found Qt-Version 4.7.4 (using /usr/local/bin/qmake) 
-- Looking for gdImagePng in /usr/local/lib/libgd.dylib 
-- Looking for gdImagePng in /usr/local/lib/libgd.dylib - found 
-- Found ZLIB: /usr/include (found version "1.2.3") 
-- Found PNG: /usr/X11R6/lib/libpng.dylib 
-- Looking for gdImageJpeg in /usr/local/lib/libgd.dylib 
-- Looking for gdImageJpeg in /usr/local/lib/libgd.dylib - found 
-- Found JPEG: /usr/local/lib/libjpeg.dylib 
-- Looking for gdImageGif in /usr/local/lib/libgd.dylib 
-- Looking for gdImageGif in /usr/local/lib/libgd.dylib - found 
-- Found GD: /usr/local/lib/libgd.dylib 
-- Found GIF: /usr/local/lib/libgif.dylib 
-- Looking for include files HAVE_GD_H 
-- Looking for include files HAVE_GD_H - found 
-- Looking for include files HAVE_PNG_H 
-- Looking for include files HAVE_PNG_H - not found. 
-- Looking for include files HAVE_GIF_LIB_H 
-- Looking for include files HAVE_GIF_LIB_H - found 
-- Configuring done 
-- Generating done 
-- Build files have been written to: /Developer/workspace/png/PietCreator/build 

運行make後應用程序已成功編譯:

Linking CXX executable pietcreator 
[ 95%] Built target pietcreator 
[ 97%] Generating NPietTest.moc 
Scanning dependencies of target npiettest 
[100%] Building CXX object npiet/CMakeFiles/npiettest.dir/test/NPietTest.cpp.o 
Linking CXX executable npiettest 
[100%] Built target npiettest 

唯一的問題,我跑進執行cmake ../時都與缺少的依賴關係。我不得不下載/編譯/安裝的Qt 4.7.4Qt的流動性1.2.0。在那之後,我還需要的libgdgiflib但後來我用brew作業。

我建議你嘗試另一種git clone並嘗試從頭編譯。

如果你想知道,釀造安裝GD 2.0.36RC1giflib 4.1.6。當你有一個結構爲下

+0

你實際上也許能夠給我送編譯後的文件?我將嘗試重新編譯自己並匹配您使用的依賴關係的內部版本號。 – James

+0

我可能,但請記住,這可能無法正常工作。通過電子郵件地址向我發送Twitter消息。 – karlphillip

+1

原來,我的系統上有一個過時的libjpeg庫,導致了其他依賴性問題。運行已編譯的應用程序向我顯示了這一點,並在更新時完美運行謝謝。 – James

2

您需要查看png.h(或其文檔),找出png_infop類型的指針,然後找出應如何訪問其字段。假設這個指針真的是正確的,那麼要麼需要包含該類型的定義(以便編譯器知道其數據成員width等),否則就會有getter函數你應該打電話給png_infop參數並返回你之後的信息。

[編輯:看來,如果你應該使用png_get_IHDR,或png_get_image_width等]

+0

你可以擴展你的編輯? – James

0

當你在頭一個向前聲明通常你得到這個錯誤,你不包括文件源文件中對應的類:

//header.h 
class B; 
class A{ 
A(); 
B* b; 
} 

//source.cpp 
#include "header.h" 
//include "B.h" //include header where B is defined to prevent error 
A::A() 
{ 
    b->foo(); //error, B is only a forward declaration 
} 

因此,您需要包含相應的頭,前向聲明是不夠的。

4

我認爲這是由png.h模塊的創建者設計的。

應該是png_infop被聲明爲指向"png.h"中的struct的指針。實際的結構聲明和定義應該在"png.c"中。

作者不想公開struct的內部,因此在"png.c"中定義了struct

這意味着你不能訪問結構(即任何成員:info_ptr->widthinfo_ptr->heightinfo_ptr->bit_depth

struct成員並非直接提供給用戶訪問

我打賭有功能的訪問。如果筆者認爲,你將需要widthheight,或bit_depth信息的成員(即:getWidth(info_ptr)getHeight(info_ptr),...)。

+0

我將更多地查看源文件以查看是否屬於這種情況。我還編輯了我的問題,以包含我正在編譯的Github回購。 – James

0

不完全引用錯誤,通常會出現:

struct foo; 

int bar(void) { 
    struct foo *p; 
    p->a = 0; 
} 

這裏的FOO結構宣告但其實際內容是不知道,而且會產生反引用不完全類型的錯誤。

這背後的理念是強制正式使用API​​操縱數據結構。這可以確保未來的API可以更輕鬆地更改結構,而不會影響傳統程序。

所以一般的API頭會做這樣的事情:

/* 
* foo.h part of foo API 
*/ 

struct foo; 

extern void foo_set_a(struct foo *p, int value); 
extern int foo_get_a(struct foo *p); 

它會在內部實現富API函數...例如:

/* 
* foo.c ... implements foo API 
*/ 

struct foo { 
    int a; 
}; 

void foo_set_a(struct foo *p, int value) { 
    p->a = value; 
} 

int foo_get_a(struct foo *p) { 
    return p->a; 
} 

,然後FOO API的用戶可以:

use_foo() { 
    struct foo *my_foo; 
    foo_set_a(my_foo, 1); 
} 
相關問題