2016-10-10 26 views
1

v9.6,win server 2012,vs 2015 成功編譯並鏈接爲x64。卡住取得Windows版本的示例C功能

創建函數失敗,說在dll中沒有'add_one'函數。

postgres=# create function add_one(integer) returns integer as 
'win32project1',' add_one' language c strict; 

ERROR: could not find function "add_one" in file "C:/Program Files/PostgreSQL/9.6/lib/win32project1.dll" 

的功能似乎是有不過,DUMPBIN說

 1 0 000112CB Pg_magic_func = @ILT+710(Pg_magic_func) 
     2 1 00011087 pg_finfo_add_one = @ILT+130(pg_finfo_add_one) 
     3 2 00011190 pg_finfo_add_one_float8 = @ILT+395(pg_finfo_add_one_float8) 
     4 3 000110F5 pg_finfo_concat_text = @ILT+240(pg_finfo_concat_text) 
     5 4 000112C1 pg_finfo_copytext = @ILT+700(pg_finfo_copytext) 
     6 5 0001107D pg_finfo_makepoint = @ILT+120(pg_finfo_makepoint) 

回答

1

行,有需要導出爲每個功能的元數據功能pg_finfo_xxx加上實際的功能2個功能本身xxx

用於pg函數編譯的標準頭文件用PGDLLEXPORT標記了元數據函數,但實際函數的前向聲明沒有用這種方式標記。我沒有看到這是如何工作的。

#define PG_FUNCTION_INFO_V1(funcname) \ 
Datum funcname(PG_FUNCTION_ARGS); \ 
extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \ 
const Pg_finfo_record * \ 
CppConcat(pg_finfo_,funcname) (void) \ 
{ \ 
    static const Pg_finfo_record my_finfo = { 1 }; \ 
    return &my_finfo; \ 
} \ 
extern int no_such_variable 

但我把它做

#define PG_FUNCTION_INFO_V1(funcname) \ 
PGDLLEXPORT Datum funcname(PG_FUNCTION_ARGS); \ 
extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \ 
const Pg_finfo_record * \ 
CppConcat(pg_finfo_,funcname) (void) \ 
{ \ 
    static const Pg_finfo_record my_finfo = { 1 }; \ 
    return &my_finfo; \ 
} \ 
extern int no_such_variable 
0

你沒有顯示源文件的工作,但它應該包含這樣的:

extern PGDLLEXPORT Datum add_one(PG_FUNCTION_ARGS); 

PG_FUNCTION_INFO_V1(add_one); 

然後它可能會工作。

我已經開始discussion on the hackes mailing list實施PostgreSQL中your answer,但似乎與PostgreSQL的使用(生成和使用導出定義文件),這將導致至少警告,所以我們已經放棄了它的構建過程。

你擁有的另一個選擇是創建和使用像PostgreSQL這樣的導出定義文件,那麼你可以完全不使用PGDLLEXPORT裝飾。

+0

我直接從9.6文檔頁面複製源文件https://www.postgresql.org/docs/9.6/static/xfunc-c.html#XFUNC-C-DYNLOAD – pm100

+0

建議確實有效,但如果PG_FUNCTION_INFO中有PGDLLEXPORT,那麼這個額外的行就不需要了 – pm100

+0

文檔中的示例似乎沒有針對Windows。這是一個錯誤。我會提出一個補丁。 –