0

I am currently using two C files written by others in my C++ project in Visual Studio 2008 (actually, it is provided free online as a polygon intersection library).

The two files I downloaded are gcp.h and gcp.c. So I copied gcp.h and gcp.c to my project directory. The linker gave me this error:

unresolved external symbol: void __cdecl gpc_polygon_clip(enum gpc_op,struct gpc_polygon *,struct gpc_polygon *,struct gpc_polygon *)

Below is how wrote my code which uses the library:

static int poly_intersection(Convex_Polygon& poly1,Convex_Polygon& poly2,Convex_Polygon& rp)
{
    if(!poly1.is_convex() || !poly2.is_convex())
        return 1;

    // Construct 1st convex polygon
    gpc_vertex *gvp1 = new gpc_vertex[poly1.size()];
    gpc_vertex v1;
    for (unsigned int n = 0; n < poly1.size(); n++)
    {
        v1.x = poly1.vertex(n).x();
        v1.y = poly1.vertex(n).y();
        gvp1[n] = v1;
    }

    gpc_vertex_list gvl1;
    gvl1.num_vertices = poly1.size();
    gvl1.vertex = gvp1;

    gpc_vertex_list *gvlp1 = new gpc_vertex_list[1];
    gvlp1[0] = gvl1;

    gpc_polygon gp1;
    gp1.num_contours = 1;
    gp1.hole = 0;
    gp1.contour = gvlp1;

    // Construct 2st convex polygon
    gpc_vertex *gvp2 = new gpc_vertex[poly2.size()];
    gpc_vertex v2;
    for (unsigned int n = 0; n < poly2.size(); n++)
    {
        v2.x = poly2.vertex(n).x();
        v2.y = poly2.vertex(n).y();
        gvp2[n] = v2;
    }

    gpc_vertex_list gvl2;
    gvl2.num_vertices = poly2.size();
    gvl2.vertex = gvp2;

    gpc_vertex_list *gvlp2 = new gpc_vertex_list[1];
    gvlp2[0] = gvl2;

    gpc_polygon gp2;
    gp2.num_contours = 1;
    gp2.hole = 0;
    gp2.contour = gvlp2;

    // Do convex polygon intersection
    gpc_polygon result;
    gpc_polygon_clip(GPC_INT, &gp1, &gp2, &result);

And for clarity, the content of the two files I used are

#gcp.h

#define GPC_EPSILON (DBL_EPSILON)
#define GPC_VERSION "2.32"

/*
===========================================================================
                           Public Data Types
===========================================================================
*/

typedef enum                        /* Set operation type                */
{
  GPC_DIFF,                         /* Difference                        */
  GPC_INT,                          /* Intersection                      */
  GPC_XOR,                          /* Exclusive or                      */
  GPC_UNION                         /* Union                             */
} gpc_op;

typedef struct                      /* Polygon vertex structure          */
{
  double              x;            /* Vertex x component                */
  double              y;            /* vertex y component                */
} gpc_vertex;

typedef struct                      /* Vertex list structure             */
{
  int                 num_vertices; /* Number of vertices in list        */
  gpc_vertex         *vertex;       /* Vertex array pointer              */
} gpc_vertex_list;

typedef struct                      /* Polygon set structure             */
{
  int                 num_contours; /* Number of contours in polygon     */
  int                *hole;         /* Hole / external contour flags     */
  gpc_vertex_list    *contour;      /* Contour array pointer             */
} gpc_polygon;

typedef struct                      /* Tristrip set structure            */
{
  int                 num_strips;   /* Number of tristrips               */
  gpc_vertex_list    *strip;        /* Tristrip array pointer            */
} gpc_tristrip;


/*
===========================================================================
                       Public Function Prototypes
===========================================================================
*/

void gpc_read_polygon        (FILE            *infile_ptr,
                              int              read_hole_flags,
                              gpc_polygon     *polygon);

void gpc_write_polygon       (FILE            *outfile_ptr,
                              int              write_hole_flags,
                              gpc_polygon     *polygon);

void gpc_add_contour         (gpc_polygon     *polygon,
                              gpc_vertex_list *contour,
                              int              hole);

void gpc_polygon_clip        (gpc_op           set_operation,
                              gpc_polygon     *subject_polygon,
                              gpc_polygon     *clip_polygon,
                              gpc_polygon     *result_polygon);

void gpc_tristrip_clip       (gpc_op           set_operation,
                              gpc_polygon     *subject_polygon,
                              gpc_polygon     *clip_polygon,
                              gpc_tristrip    *result_tristrip);

void gpc_polygon_to_tristrip (gpc_polygon     *polygon,
                              gpc_tristrip    *tristrip);

void gpc_free_polygon        (gpc_polygon     *polygon);

void gpc_free_tristrip       (gpc_tristrip    *tristrip);

#endif

Before my own method at the beginning of my C++ header file, I put #include "gpc.h", which I thought would take care of it. But it didn't work.

This is the file structure of my project

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
liudaisuda
  • 125
  • 1
  • 8
  • Mostly likely explanation is that you are not compiling the 'library'. You've copied gcp.c to your project directory but have you added it to your project? It not enough to simply copy code files, you have to compile them too. – john May 04 '13 at 06:42
  • I did included both gpc.h and gpc.c into my project. And as I just said, I put #include gpc.h. – liudaisuda May 04 '13 at 06:48
  • OK, next try, where you have put `#include "gcp.h"` add this line immediately before `extern "C" {` and this line immediately afterwards `}`. Maybe the problem is that this library code is C and your code is C++. – john May 04 '13 at 06:49
  • Are you saying try extern "C" {gcp.h} ? This is what my preprocessor commands looks like. `#include ` `#include ` `#include ` `extern "C" { #include "gpc.h" }` – liudaisuda May 04 '13 at 07:02
  • and the compiler gave an error "Error C2014 preprocessor command must start as first nonwhite space" – liudaisuda May 04 '13 at 07:05
  • I did try a different style like chncwang suggested below. It worked. Didn't know why, I simply put the command in 3 lines. – liudaisuda May 04 '13 at 07:11
  • @liudaisuda that's what I asked you do to, but it's easier to see it laid out as chncwang showed you – john May 04 '13 at 07:15

1 Answers1

3

If you use a C function in a .cpp file, include the C header file like below:

extern "C" {
    #include "gpc.h"
}

Because the function symbol in C and C++ is different.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chncwang
  • 148
  • 1
  • 6