2011-08-11 92 views
0

我真的很討厭不得不在這裏提問。但我一直在尋找some of the other posts,像這樣的解決方案似乎並不奏效。它可能是我對語法的誤解。Dlsym:投射到不同大小的整數的指針

我正在改進我的一些舊代碼。問題中的函數循環遍歷一些加載的模塊並運行一個函數。當我在x86上時,這段代碼工作得非常好,但跳到64位正在搞砸了一切。

int FindCmd(ArgS *Args) 
{ 
    /* We need to check our loaded modules for the appropriate command. */ 
    int found = 0; 

    ModS *Current; 

    for(Current = Modules; Current != NULL; Current = Current->Next)  
    { /* Cycle through the modules. */ 

     int (*OnConsoleCmd)(RootS *IRC, ArgS *Args, McapiS *Mcapi); 

     /* The below statement is the problem. */ 
     OnConsoleCmd = (int (*)(RootS *, ArgS *, McapiS *))dlsym(Current->Handle, "OnConsoleCmd"); 
     /* The above statement is the problem. */ 

     if(OnConsoleCmd != NULL) 
     { 
      if(OnConsoleCmd(IRC, Args, Mcapi) != 0)  /* Run command. */ 
       found++; 
     } 
    } 

    return found; 
} 

我得到以下警告:

exec/src/input.c:98:18: warning: cast to pointer from integer of different size 

,當然還有我的程序段錯誤。我知道這只是一個鑄造問題,但我不知道一個簡單便攜的解決方案。如果您需要更多信息,請告訴我。謝謝。

+1

你不應該討厭在這裏提問,這在很大程度上是本網站的目的。 – dreamlax

+0

@dreamlax:憎恨不得不去討厭。恨不得不意味着你對沒有經驗的建設性不滿。討厭意味着你不願意尋求你實際需要改進的幫助。 –

回答

3

這很可能是因爲您的範圍內沒有dlsym()的原型,因此它被隱式聲明爲int dlsym(),這是錯誤的。

如果您將#include <dlfcn.h>添加到使用dlsym()的文件中,您將得到正確的聲明並且它應該可以工作。

+0

也可能想用'-Werror = prototypes'調用gcc。 –

+0

是的,就是這樣,我忘了。哇,多麼尷尬!不管怎樣,謝謝! –