Question

I'm trying to use libreDWG to open and understand some dwg files. I have installed it and at least got some of the test programs to run (even if they seg fault later on). Anyway, I have included a small header file in my project very similar to the simple example found here https://github.com/h4ck3rm1k3/libredwg/blob/master/examples/load_dwg.c There seems to be a general problem with data types (at least in the way I'm compiling it) meaning I've added a few casts of form (char*) to number of variables which previously trying to automatically convert (void*) and (unsigned char*) to type (char*) and got rid of those compiler complaints. But even still when I compile it like so

g++ xxx.c++ -L/opt/local/lib/ -lredwg -o program_name

I get the following error:

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

I'm not sure what to do, I've fixed any problems in the source the compiler complains about and am linking to the relevant libraries with -lredwg (right? I haven't missed any?). My header file is just to test the functionality and looks like:

#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;
}

What am I doing wrong? I believe libredwg is written in c. Is this the problem?

Was it helpful?

Solution

It seems that you are trying to link against a 32 bit library when you're on a 64 bit platform, like in this answer. Solution is to download (or build yourself from source) a 64 bit version of libredwg. Or alternatively add the "-m32" flag to your g++ command line - to build your whole app as a 32 bit executable.

EDIT : as you have found out, the problem is actually caused by trying to link C++ code with a C library without the following at the top / bottom of your code :

#ifdef __cplusplus 
extern "C" { 
#endif

// ... source code here

#ifdef __cplusplus
} 
#endif 

Basically this tells the compiler not to do C++ name-mangling - switching name mangling off allows linking between C and C++

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top