0

我目前在 Visual Studio 2008 的 C++ 项目中使用其他人编写的两个 C 文件(实际上,它是作为多边形交集库免费在线提供的)。

我下载的两个文件是 gcp.h 和 gcp.c。所以我将 gcp.h 和 gcp.c 复制到我的项目目录中。链接器给了我这个错误:

未解析的外部符号:void __cdecl gpc_polygon_clip(enum gpc_op,struct gpc_polygon *,struct gpc_polygon *,struct gpc_polygon *)

以下是如何编写使用该库的代码:

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);

为了清楚起见,我使用的两个文件的内容是

#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

在我自己的 C++ 头文件开头的方法之前,我放了#include "gpc.h",我认为它会处理它。但它没有用。

这是我项目的文件结构

4

1 回答 1

3

如果您在 .cpp 文件中使用 C 函数,请包含 C 头文件,如下所示:

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

因为 C 和 C++ 中的函数符号不同。

于 2013-05-04T07:00:22.223 回答