我目前正在使用C++文件中的其他人在我的C++项目中编写视觉 Studio 2008(实际上,它是作为多边形交集库在线提供的)。错误:C++无法解析的外部符号
我下载的两个文件是gcp.h和gcp.c.所以我将gcp.h和gcp.c复制到我的项目目录中。链接器给了我这个错误:
unresolved external symbol: 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”,我认为它会照顾它。但它没有奏效。
很可能的解释是你没有编译'库'。您已将gcp.c复制到您的项目目录,但是是否已将其添加到您的项目中?仅仅复制代码文件是不够的,你也必须编译它们。 – john 2013-05-04 06:42:21
我确实在我的项目中包含了gpc.h和gpc.c。正如我刚才所说,我把#include gpc.h. – liudaisuda 2013-05-04 06:48:03
好的,下一次尝试,你把'#include“gcp.h”'放在'extern“C”{'之后立即添加这行''',此后立即'}'。也许问题是这个库的代码是C,你的代码是C++。 – john 2013-05-04 06:49:56