2011-03-09 32 views
0

我一直在尋找在C或C++無鎖哈希表的實現,並發現一個:SunriseDD。這是個好消息:)幾天後,我試圖讓這個運行起來,但沒有運氣。我可以使用GCC C編譯器將下載的源代碼編譯爲靜態庫。我得到libSunriseDD.a歸檔。如何鏈接SunriseDD C庫與C++應用程序?

到目前爲止這麼好:D 我創建了一個簡單的C++應用程序來包裝散列表的SunriseDD C實現。下面是main.cpp中:

#include <iostream> 
#include <dd_data_dictionary.h> 

using namespace std; 

struct Node{ 
    int oid; 
    int x, y; 
};  
struct HashTableEntry{ 
    Node *node; 
    int idx; 
}; 
void copyFunction(void * source, void * target){ 
    target = source; 
} 

template<typename T>class HashTableDD{ 
private: 
    dd_dictionary objects; 
public: 
    HashTableDD() { 
     objects = dd_new_dictionary(); // creating dictionary 
     dd_set_object_copy_function_for_dictionary(objects, copyFunction); // setting copy function 
    } 
    ~HashTableDD() { 
     dd_dispose_dictionary(objects); 
    }  
    bool insert(T obj, long key){ 
     return dd_add_object_for_key(objects, (char *)key, (void*)obj); 
    } 
    bool remove(long key){ 
     return dd_remove_object_for_key(objects, (char *)key); 
    } 
    T find(long key) { 
     return (T)dd_object_for_key(objects, (char *)key, false); 
    } 
};  

int main(int argc, char ** argv){ 
    HashTableDD<HashTableEntry*> *ht = new HashTableDD<HashTableEntry*>(); 
    HashTableEntry* hte = new HashTableEntry; 
    hte->idx = 1; 
    hte->node = NULL; 
    ht->insert(hte, 1); 
    HashTableEntry* hte2 = new HashTableEntry; 
    hte2->idx = 2; 
    Node n; 
    n.oid = 10; n.x = 10; n.y = 10; 
    hte2->node = &n; 
    ht->insert(hte2, 2); 

    HashTableEntry* ret = ht->find(1); 
    if(ret != NULL){ 
     cout << "hte. idx: " << ret->idx << " node: " << ret->node << endl; 
    }  
    ht->remove(1); 
    ht->remove(2);  
    delete hte; delete hte2; delete ht; 
    return 0; 
} 

但是連接不開心:

:~/Desktop/HashTable$ make 
Building file: main.cpp 
Invoking: GCC C++ Compiler 
g++ -ISunriseDD/build/../ -O0 -g3 -m64 -c -o"build/main.o" "main.cpp" 
Finished building: main.cpp 

Building target: build/HashTableDD 
Invoking: GCC C++ Linker 
g++ -LSunriseDD/build/ -lSunriseDD -o build/HashTableDD build/main.o 
build/main.o: In function `HashTableDD': 
/home/robertas/Desktop/HashTable/main.cpp:32: undefined reference to `dd_new_dictionary()' 
/home/robertas/Desktop/HashTable/main.cpp:33: undefined reference to `dd_set_object_copy_function_for_dictionary(void*, void (*)(void*, void*))' 
build/main.o: In function `HashTableDD<HashTableEntry*>::insert(HashTableEntry*, long)': 
/home/robertas/Desktop/HashTable/main.cpp:40: undefined reference to `dd_add_object_for_key(void*, char const*, void*)' 
build/main.o: In function `HashTableDD<HashTableEntry*>::find(long)': 
/home/robertas/Desktop/HashTable/main.cpp:47: undefined reference to `dd_object_for_key(void*, char const*, bool)' 
build/main.o: In function `HashTableDD<HashTableEntry*>::remove(long)': 
/home/robertas/Desktop/HashTable/main.cpp:44: undefined reference to `dd_remove_object_for_key(void*, char const*)' 
build/main.o: In function `~HashTableDD': 
/home/robertas/Desktop/HashTable/main.cpp:36: undefined reference to `dd_dispose_dictionary(void*)' 
collect2: ld returned 1 exit status 
make: *** [build/HashTableDD] Error 1 

任何想法,我究竟錯在這裏做什麼?我不正確地鏈接SunriseDD庫嗎?

順便說一句,我有以下的目錄列表,其中我的main.cpp所在:

+HashTable 
|--+build 
| |--main.o 
|---main.cpp 
|--+SunriseDD 
    |--+build 
    | |--libSunriseDD.a 
    | |--other object files 
    |--headers and source files of SunriseDD 

感謝您的幫助!

回答

2

-lSunriseDD最後的連接線。該連接器處理左到右的論點,並搜索庫目前未定義的符號時,它處理靜態庫。

此外,除非庫是C++ - 意識到,帶包括在extern "C"

extern "C" { 
    #include <dd_data_dictionary.h> 
} 
+0

鏈接器輸出相同的錯誤信息 – Robertas 2011-03-09 13:54:25

+0

什麼是您的新鏈接器行? – Erik 2011-03-09 13:55:46

+0

'克++ -LSunriseDD /建造/ -o建立/ HashTableDD構建/ main.o -lSunriseDD' – Robertas 2011-03-09 13:58:11