0

I'm writing a C++ program in Visual Studio 2017 that is trying to make use of this C program: https://www.cs.cmu.edu/~quake/triangle.html

I have extern "C" {#include triangle.h} at the beginning of my program but I'm still getting an error:

LNK2019 unresolved external symbol _triangulate referenced in function "void __cdecl createTriangulation(void)" (?createTriangulation@@YAXXZ)   GLTests C:\Users\Connor McDermott\source\repos\GLTests\GLTests\main.obj 1   

Here's the function where I call triangulate from:

void createTriangulation() {
triangulateio in, out;

/* Define input points. */

in.numberofpoints = pts.size();
in.numberofpointattributes = 0;
in.pointlist = (REAL *)malloc(in.numberofpoints * 2 * sizeof(REAL));
for (int i = 0; i < pts.size(); i++) {
    in.pointlist[2 * i] = pts[i].x;
    in.pointlist[2 * i + 1] = pts[i].y;
}
in.pointmarkerlist = (int *)NULL;

in.numberofsegments = pts.size();
in.segmentlist = (int *)malloc(in.numberofsegments * 2 * sizeof(int));
in.segmentlist[0] = pts.size() - 1;
in.segmentlist[1] = 0;
for (int i = 1; i < pts.size(); i++) {
    in.segmentlist[2 * i] = i - 1;
    in.segmentlist[2 * i + 1] = i;
}

in.numberofholes = 0;
in.numberofregions = 0;

out.pointlist = (REAL *)NULL;            /* Not needed if -N switch used. */
/* Not needed if -N switch used or number of point attributes is zero: */
out.pointattributelist = (REAL *)NULL;
out.pointmarkerlist = (int *)NULL; /* Not needed if -N or -B switch used. */
out.trianglelist = (int *)NULL;          /* Not needed if -E switch used. */
/* Not needed if -E switch used or number of triangle attributes is zero: */
out.triangleattributelist = (REAL *)NULL;
out.neighborlist = (int *)NULL;         /* Needed only if -n switch used. */
/* Needed only if segments are output (-p or -c) and -P not used: */
out.segmentlist = (int *)NULL;
/* Needed only if segments are output (-p or -c) and -P and -B not used: */
out.segmentmarkerlist = (int *)NULL;
out.edgelist = (int *)NULL;             /* Needed only if -e switch used. */
out.edgemarkerlist = (int *)NULL;   /* Needed if -e used and -B not used. */

char switches[6] = { 'p', 'z', 'B', 'P', 'N' };
triangulate(switches, &in, &out, (struct triangulateio *) NULL);

for (int i = 0; i < out.numberofsegments; i++) {
    edge e;
    e.p0 = pts[out.segmentlist[2 * i]];
    e.p1 = pts[out.segmentlist[2 * i + 1]];
}

}

I've tried looking at the questions about this error, but there's so many possible causes that I can't find which one applies to this situation. The most common problem seems to be not using extern "C" when including the header of a c program, but I've done that.

So what am I doing wrong?

Thanks to anyone who can help.

  • what's `_triangulate`? – Joseph D. May 01 '18 at 03:45
  • You need to add `triangle.c` and `tricall.c` to your VS2017 project and compile it together. – FBergo May 01 '18 at 03:47
  • *but I've done that* -- Seeing is believing -- this is your claim, but where is the proof? Maybe you did something wrong. – PaulMcKenzie May 01 '18 at 04:01
  • Take a look at ANSI_DECLARATORS pre-processor definition in triangle.c files. This file must be added and compiled with ANSI_DECLARATORS defined, otherwise it will generate the triangulate function without any parameters. – EylM May 01 '18 at 09:02
  • @FBergo Is there something else I have to do in VS2017 other than just add triangle.c as a source file? (tricall.c is just an example program that uses triangle.c) When I add triangle.c I get more errors (which is weird to me since it should be an already working program). Scrolling through the errors, they seem to be either about some functions being "unsafe" or using an undefined struct (timeval and timezone). – Connor McDermott May 01 '18 at 13:35
  • I figured out the structs. I needed to uncomment "#define NO_TIMER" in triangle.c since I'm using Windows. From what I can tell about the other ones I either need to change the functions to the safer versions or tell the complier to ignore it. It seems like changing them would normally be better, but this is not my code so I don't know if i should. – Connor McDermott May 01 '18 at 13:48
  • It's reasonably safe to ignore those warnings. This code was originally written for Linux, some of the "safer" functions VS recommends do not exist there (or exist with different names). – FBergo May 01 '18 at 14:30
  • @FBergo thanks, it's working now – Connor McDermott May 01 '18 at 15:22

0 Answers0