首页 > 解决方案 > 如何使用 gnatmake 或在 Ada 代码中将 Ada 库链接到 .adb 文件

问题描述

我正在使用 PLplot 运行测试代码,如下所示:

with PLplot_Auxiliary, PLplot_Standard;
use PLplot_Auxiliary, PLplot_Standard;

procedure xstandard00a is
   x, y : Real_Vector(0 .. 100);
   x_Min, y_Min : Long_Float := 0.0;
   x_Max : Long_Float := 1.0;
   y_Max : Long_Float := 100.0;
begin
   -- Prepare data to be plotted.
   for i in x'range loop
      x(i) := Long_Float(i) / Long_Float(x'length - 1);
      y(i) := y_Max * x(i)**2;
   end loop;

   -- Parse and process command line arguments.
   Parse_Command_Line_Arguments(Parse_Full);

   -- Initialize plplot.
   Initialize_PLplot;

   -- Create a labelled box to hold the plot.
   Set_Environment(x_Min, x_Max, y_Min, y_Max,
       Justification => Not_Justified,
       Axis_Style    => Linear_Box_Plus);
   Write_Labels
      (X_Label     => "x",
       Y_Label     => "y=100 x#u2#d",
       Title_Label => "Simple PLplot demo of a 2D line plot");

   -- Plot the data that was prepared above.
   Draw_Curve(x, y);

   -- Close PLplot library.
   End_PLplot;
end xstandard00a;

它不使用命令在 geany 中构建,gnatmake "%e"也不通过命令编译gcc -Wall -c "%f"

它给了我错误:

 xstandard00a.adb:20:05: file "plplot_auxiliary.ads" not found
 xstandard00a.adb:21:05: file "plplot_standard.ads" not found

显然我必须链接到图书馆。PLplot的*.ali文件位于/usr/lib64/ada/adalib/plplotada.

如何与构建命令链接gnatmake,或者如何添加额外的代码xstandard00a.adb来查找那些 PLplot 库*.ali

更新:我运行带有示例(xstandard00a.adb)的 Make 文件,它编译成功。我打开 make 文件以查看它用于编译的标志。我准确地放置了这些标志并重新编译了另一个看起来像 xstanrd00a.adb 的示例,但它未能找到库。显然,除了 gnat make 使用的标志之外,还有其他东西。我错过了一些东西。Make文件编写如下,SHELL = /bin/bash GNAT_EXECUTABLE_BUILDER = /usr/bin/gnatmake
"-aI/usr/share/ada/adainclude/plplotada" "-aL/usr/lib64/ada/adalib /plplotada" EXEEXT =

 PKG_CONFIG_ENV = PKG_CONFIG_PATH="/usr/lib64/pkgconfig::/usr/lib64/pkgconfig:/usr/share/pkgconfig"
 install_tree_ada_RPATHCMD = 

 EXECUTABLES_list = \
 xtraditional00a$(EXEEXT) 

 all: $(EXECUTABLES_list)

 clean:
    rm -f $(EXECUTABLES_list) *.ali *.o

 # target_link_libraries( plplotada plplot)
 # Due to command-line limitations of gnatmake cannot continue (by    trailing \)
 # anything after -cargs below.
 .adb$(EXEEXT):
  $(GNAT_EXECUTABLE_BUILDER) $< \
 -cargs $(shell $(PKG_CONFIG_ENV) pkg-config  --cflags plplot-ada   plplot) -largs $(install_tree_ada_RPATHCMD) $(shell $(PKG_CONFIG_ENV) pkg-   config  --libs plplot-ada plplot)

 .SUFFIXES: .adb $(EXEEXT)

我在编译/链接库中缺少什么?我使用 gnatmake -P 尝试了 gpr 方法,但仍然无法链接。到目前为止,唯一对我有用的方法是制作文件。但是,我想手动编译以弄清楚这个东西是如何工作的。我在 Geany IDE 的构建命令中写的是这样的:

gnatmake "%e" -aI/usr/share/ada/adainclude/plplotada -aL/usr/lib64/ada/adalib/plplotada

“%e”指的是 IDE 中的任何 adb 文件,在我的例子中是 simpleplot.adb,它基本上是 xstandard00a.adb

标签: linkershared-librariesada

解决方案


在源代码旁边添加一个 .gpr 文件,例如,这个文件名为 xstandard.gpr:

with "/usr/share/gpr/plplotada.gpr";

project XStandard is
   for Create_Missing_Dirs use "True";
   for Source_Dirs use (".");
   for Object_Dir use "obj";
   package Builder is
      for Executable ("xstandard00a.adb") use "xstandard";
   end Builder;
end XStandard;

(已编辑;我已将“/usr/share/ada/adainclude/plplotada”放在此示例项目的源代码中,但最好使用包提供的 .gpr 文件)

然后执行gprbuild. 我已经对此进行了测试,它适用于您给定的源文件。

我通过首先安装 plplotada 找到要包含的 gprfile,然后列出安装在我的系统上的所有文件:

sudo apt install libplplotada[Tab]
dpkg-query -L libplplotada2-dev

推荐阅读