How to link a SunriseDD C library with a C++ app?
I was looking for a lock-free hash table implementation in C or C++ and found one: SunriseDD. That was the good news :) For a few days I am trying to get this up and running but with no luck. I can compile downloaded sources as a static library using GCC C compiler. And I get libSunriseDD.a archive.
So far so good :D I have created a simple C++ application to wrap the SunriseDD C implementation of the hash table. Here is the 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;
}
But the linker is not happy about that:
:~/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/roberta开发者_运维问答s/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
Any ideas what am I doing wrong here? Do I link the SunriseDD library incorrectly?
BTW I have the following directory listing where my main.cpp resides:
+HashTable
|--+build
| |--main.o
|---main.cpp
|--+SunriseDD
|--+build
| |--libSunriseDD.a
| |--other object files
|--headers and source files of SunriseDD
Thanks for the help!
Put -lSunriseDD
last on the linker line. The linker processes arguments left to right, and searches libraries for currently undefined symbols when it processes a static library.
Also, unless the library is C++-aware, wrap includes in extern "C"
.
extern "C" {
#include <dd_data_dictionary.h>
}
精彩评论