gpt4 book ai didi

c++ - 如何将 SunriseDD C 库与 C++ 应用程序链接?

转载 作者:行者123 更新时间:2023-11-28 03:51:41 25 4
gpt4 key购买 nike

我一直在寻找 C 或 C++ 中的无锁哈希表实现,并找到了一个:SunriseDD .这是个好消息 :) 几天来,我一直在努力启动并运行它,但没有成功。我可以使用 GCC C 编译器将下载的源代码编译为静态库。我得到了 libSunriseDD.a 存档。

到目前为止还不错 :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

感谢您的帮助!

最佳答案

-lSunriseDD last 放在链接器行上。链接器从左到右处理参数,并在处理静态库时在库中搜索当前 undefined symbol 。

另外,除非库支持 C++,否则 wrap 包含在 extern "C" 中。

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

关于c++ - 如何将 SunriseDD C 库与 C++ 应用程序链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5246563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com