【问题标题】:Error: C++ unresolved external symbol [duplicate]错误:C++ 无法解析的外部符号 [重复]
【发布时间】:2013-05-04 06:39:58
【问题描述】:

我目前在 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",我认为它会处理它。但它没有用。

【问题讨论】:

  • 最可能的解释是您没有编译“库”。您已将 gcp.c 复制到您的项目目录,但您是否已将其添加到您的项目中?仅仅复制代码文件是不够的,你还要编译它们。
  • 我确实将 gpc.h 和 gpc.c 都包含到了我的项目中。正如我刚才所说,我输入了#include gpc.h。
  • 好的,下一次尝试,在您放置#include "gcp.h" 的位置添加这一行,紧接在extern "C" { 之前和紧接在} 之后的这一行。也许问题是这个库代码是 C 而你的代码是 C++。
  • 你是说试试 extern "C" {gcp.h} 吗?这就是我的预处理器命令的样子。 #include &lt;CGAL/Polygon_2_algorithms.h&gt;#include &lt;QtOpenGL&gt;#include &lt;assert.h&gt;extern "C" { #include "gpc.h" }
  • 并且编译器给出了错误“错误 C2014 预处理器命令必须作为第一个非空白开始”

标签: c++ linker symbols


【解决方案1】:

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

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

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

【讨论】:

  • 成功了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 2011-08-09
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
  • 2020-07-09
相关资源
最近更新 更多