我正在尝试使用 libreDWG 打开和理解一些 dwg 文件。我已经安装了它并且至少可以运行一些测试程序(即使它们稍后会出现故障)。无论如何,我在我的项目中包含了一个小头文件,与此处找到的简单示例非常相似 https://github.com/h4ck3rm1k3/libredwg/blob/master/examples/load_dwg.c数据类型似乎存在一个普遍问题(至少在我编译它的方式上)意味着我已经添加了一些形式(char *)到以前试图自动转换(void *)的变量数量和 (unsigned char*) 键入 (char*) 并摆脱那些编译器投诉。但是即使我这样编译它也是如此
g++ xxx.c++ -L/opt/local/lib/ -lredwg -o program_name
我收到以下错误:
Undefined symbols for architecture x86_64:
"dwg_read_file(char*, _dwg_struct*)", referenced from:
load_dwg(char*)in ccN6HUqz.o
"dwg_free(_dwg_struct*)", referenced from:
load_dwg(char*)in ccN6HUqz.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
我不确定该怎么做,我已经修复了编译器提示的源代码中的任何问题,并使用 -lredwg 链接到相关库(对吗?我没有遗漏任何问题?)。我的头文件只是为了测试功能,看起来像:
#include "suffix.c"
#include <dwg.h>
plan floor_plan;//temporary data structure defined elsewhere for now
void
add_line(double x1, double y1, double x2, double y2)
{
line_in temp;
temp.start.x=x1;
temp.start.y=y1;
temp.end.x=x2;
temp.end.y=y2;
floor_plan.lines.push_back(temp);
std::cout<<"LINE: :"<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<std::endl;
}
void
add_circle(double x, double y, double R)
{
// Yet to do
}
void
add_text(double x, double y, char *txt)
{
// Make something with that
}
int
load_dwg(char *filename)
{
unsigned int i;
int success;
Dwg_Data dwg;
dwg.num_objects = 0;
success = dwg_read_file(filename, &dwg);
for (i = 0; i < dwg.num_objects; i++)
{
Dwg_Entity_LINE *line;
Dwg_Entity_CIRCLE *circle;
Dwg_Entity_TEXT *text;
switch (dwg.object[i].type)
{
case DWG_TYPE_LINE:
line = dwg.object[i].tio.entity->tio.LINE;
add_line(line->start.x, line->end.x, line->start.y, line->end.y);
break;
case DWG_TYPE_CIRCLE:
circle = dwg.object[i].tio.entity->tio.CIRCLE;
add_circle(circle->center.x, circle->center.y, circle->radius);
break;
case DWG_TYPE_TEXT:
text = dwg.object[i].tio.entity->tio.TEXT;
add_text(text->insertion_pt.x, text->insertion_pt.y, (char*) text->text_value);
break;
}
}
dwg_free(&dwg);
return success;
}
我做错了什么?我相信 libredwg 是用 c 语言编写的。这是问题所在吗?
最佳答案
当您在 64 位平台上时,您似乎正在尝试链接到 32 位库,例如 answer .解决方案是下载(或自己从源代码构建)64 位版本的 libredwg。或者将“-m32”标志添加到您的 g++ 命令行 - 将您的整个应用构建为 32 位可执行文件。
编辑:正如您所发现的,问题实际上是由于尝试将 C++ 代码与 C 库链接而在代码的顶部/底部没有以下内容引起的:
#ifdef __cplusplus
extern "C" {
#endif
//...这里是源代码
#ifdef __cplusplus
}
#endif
基本上这会告诉编译器 not to do C++ name-mangling - 关闭名称重整允许在 C 和 C++ 之间进行链接
关于c++ - 在 C++ 中链接到 libredwg 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22611907/