2015-05-04 169 views
1

我正在學習C Windows RPC編程。這裏是我編寫和編譯沒有錯誤的虛擬RPC服務器的源代碼:Windows RPC編程錯誤:LNK 2019無法解析的外部符號RPCServerListen

#include <stdio.h> 
#include "md5_h.h" 
#include "rpc.h" 
#include "rpcndr.h" 

int main() { 
    RPC_STATUS status; 

    status = RpcServerUseProtseqEp(
    (RPC_WSTR)("ncacn_ip_tcp"), 
    RPC_C_PROTSEQ_MAX_REQS_DEFAULT, 
    (RPC_WSTR)("9191"), 
    NULL); 
if (status) { exit(status); } 

status = RpcServerRegisterIf(
    md5_v1_0_c_ifspec, 
    NULL, 
    NULL); 
if (status) { exit(status); } 

status = RpcServerListen(
    1, 
    RPC_C_LISTEN_MAX_CALLS_DEFAULT, 
    FALSE); 
if (status) { exit(status); } 

return 0; 
} 

void __RPC_USER midl_user_free(void* p) { 
    free(p); 
} 

void md5(const unsigned char* szMsg) { 
    printf("voila %s\n", szMsg); 
} 

midl文件也可以編譯而不會出錯。如預期的那樣,MIDL彙編產生md5_s.cmd5_c.c。下面是md5.idl文件如果需要的話:

[ 
uuid(D86FBC01-D6A7-4941-9243-07A4EC65E8CB), 
    version(1.0), 
] 
interface md5 
{ 
    void md5([in, string] const char* szMsg); 
}; 

在聯動階段以下錯誤產生:

LNK2019: unresolved external symbol __imp__RpcServerListen referenced in function main 

我對每一個具體的RPC的功能相同的錯誤,如RpcServerRegisterIfRpcServerUseProtseqEp。我正在使用Microsoft Visual Studio 2013.

我認爲這來自一個缺少包括;但我無法確定哪一個。我試圖包括rpc.h,沒有任何改變。

我必須在我的項目中包含製作的md5_s.c嗎?我已經嘗試過,但沒有解決任何問題。

感謝您的幫助!

回答

3

您需要鏈接到Rpcrt4.lib。 如果您使用visual studio,請將其添加到項目 - >屬性 - >配置屬性 - >鏈接器 - >輸入 - >其他依賴項中。

+0

謝謝,這解決了我遇到的問題;但你到底是怎麼想出來的? – philippe

+1

@philippe這是一個鏈接錯誤。並非所有的.lib文件都包含在默認列表中。所以有時你需要添加額外的.lib文件。例如:winsock,shell API和e.t.c.需要手動添加.lib文件。當鏈接器發出錯誤時,請檢查MSDN中的頁腳註釋,它會列出要使用的庫文件,本例中爲Rpcrt4.lib。 – shebaw