无法从 plotutils 文档编译 libplot 示例程序

Unable to compile libplot example program from plotutils documentation

我正在尝试编译在 plotutils 文档中找到的示例代码。我为 plot.h 头文件添加了适当的搜索路径,并将二进制文件链接到在安装 plotutils 2.6 时创建的每个目标文件。我在 OS X 10.10.3 和 Xcode 6.3.2 上,我是 C 编程或使用 Xcode.

的新手

我尝试编译的示例代码是:

#include <stdio.h>
#include <plot.h>
#define MAXORDER 12
void draw_c_curve (plPlotter *plotter, double dx, double dy, int order)
{
    if (order >= MAXORDER)
    /* continue path along (dx, dy) */
        pl_fcontrel_r (plotter, dx, dy);
    else
    {
        draw_c_curve (plotter,
                      0.5 * (dx - dy), 0.5 * (dx + dy), order + 1);
        draw_c_curve (plotter,
                      0.5 * (dx + dy), 0.5 * (dy - dx), order + 1);
    }
}

int main ()
{
    plPlotter *plotter;
    plPlotterParams *plotter_params;
    /* set a Plotter parameter */
    plotter_params = pl_newplparams ();
    pl_setplparam (plotter_params, "PAGESIZE", "letter");
    /* create a Postscript Plotter that writes to standard output */
    if ((plotter = pl_newpl_r ("ps", stdin, stdout, stderr,
                               plotter_params)) == NULL)
    {
        fprintf (stderr, "Couldn’t create Plotter\n");
        return 1;
    }
    if (pl_openpl_r (plotter) < 0) /* open Plotter */
    {
        fprintf (stderr, "Couldn’t open Plotter\n");
        return 1;
    }
    pl_fspace_r (plotter, 0.0, 0.0, 1000.0, 1000.0); /* set coor system */
    pl_flinewidth_r (plotter, 0.25); /* set line thickness */
    pl_pencolorname_r (plotter, "red"); /* use red pen */
    pl_erase_r (plotter); /* erase graphics display */
    pl_fmove_r (plotter, 600.0, 300.0); /* position the graphics cursor */
    draw_c_curve (plotter, 0.0, 400.0, 0);
    if (pl_closepl_r (plotter) < 0) /* close Plotter */
    {
        fprintf (stderr, "Couldn’t close Plotter\n");
        return 1;
    }
    if (pl_deletepl_r (plotter) < 0) /* delete Plotter */
    {
        fprintf (stderr, "Couldn’t delete Plotter\n");
        return 1;
    }
    return 0;
}

Xcode 确定的 2 个问题是:

Undefined symbols for architecture x86_64:
  "__pl_z_maybe_output_image", referenced from:
      __maybe_output_image in b_defplot.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

b_defplot.o 和其他目标文件是在我尝试安装 plotutils 2.6 时生成的,首先下载它,然后执行 ./configure、make 和 make install。

我的最终目标是在我正在编写的需要生成一些图的程序中使用 libplot 包,并且我希望我的程序二进制文件是独立的(即如果我执行我的程序二进制文件在没有安装 plotutils 的任何其他计算机上,它应该仍然可以工作)。这就是为什么我将我的二进制文件与我如上所述安装 plotutils 时在 libplot 文件夹中创建的每个目标文件链接起来的原因。

任何对我遇到的错误的帮助或对我做的非常错误的事情的启发,请记住我的最终目标是什么,将不胜感激。

我按照以下步骤成功编译了 libplot 示例程序:

  1. 通过 Homebrew 安装 plotutils 使用:brew install plotutils
  2. 找到(在 /usr/local/ 内或 Homebrew 安装所在的任何位置)并 link libplot.2.2.4.dylib 到您要构建的 C 程序(在这种情况下,C 程序是问题中提供的那个)。
  3. 还要确保在 header 搜索路径中包含 plot.h header 文件。

通过执行这些步骤,我能够干净地编译上面的代码(经过如下所示的修改)并能够导出如下所示的 png。 Homebrew 似乎让我省去了自己构建 plotutils 的麻烦。

我用 if ((plotter = pl_newpl_r ("png", stdin, fp, stderr, plotter_params)) == NULL) 替换了 if ((plotter = pl_newpl_r ("ps", stdin, stdout, stderr, plotter_params)) == NULL) 其中 fp 定义如下:

FILE *fp; fp = fopen("/Users/username/path/Test.png", "w"); // rest of example code shown above fclose(fp);