【问题标题】:How to link Ada library to .adb file using gnatmake or in Ada code如何使用 gnatmake 或在 Ada 代码中将 Ada 库链接到 .adb 文件
【发布时间】:2021-02-12 15:22:21
【问题描述】:

我正在使用 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;

它不使用命令gnatmake "%e" 在geany 中构建,也不通过命令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

更新: 我运行随示例提供的 Make 文件 (xstandard00a.adb) 并成功编译。我打开 make 文件以查看它用于编译的标志。我准确地放置了这些标志并重新编译了另一个看起来像 xstanrd00a.adb 的示例,但它未能找到库。显然,除了 gnat make 使用的标志之外,还有其他东西。我错过了一些东西。 Make文件的写法如下, 外壳 = /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 的 build 命令中写的是这样的:

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

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

【问题讨论】:

  • 你找到these GNAT docs on linking了吗?
  • 如果您的库的 .ads、.ali 和 .o 文件都在目录 lib_dir(绝对或相对路径)中,那么只需将 -Ilib_dir 添加到 gnatmake 即可。有些人喜欢通过将各种文件放在不同的目录中来使事情变得更复杂。如果你或 PL Plot 安装程序已经这样做了,那么让它与 gnatmake 一起工作会更复杂,当然。

标签: linker shared-libraries ada


【解决方案1】:

在您的源旁边添加一个 .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”放在此示例项目的 Sources 中,但最好使用包提供的 .gpr 文件)

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

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

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

【讨论】:

  • 但是,如果您真的想要一个 gnatmake 答案,我希望其他人能够提供它。我不知道,因为我从不使用它。我看到它也有been added to Alire,所以这使得在 Ada 中使用该项目的三种方式。
  • 我正在使用 gnatmake 构建 ada 文件并使用 gcc 进行编译。我知道 gpr 方法,但 opensuse 没有它,我不想在系统上做更多不必要的编码只是为了制作一个情节。无论如何,我通过将 PlPlot 的 *.adb、*.ads 和 *.ali 复制到本地目录来尝试暴力破解是我的 xstandard 文件。它找到了必要的文件,但未能构建和编译。大量未定义的引用,最后 gnatmake: *** 链接失败。
  • 虽然 gprbuild 可能存在于某些 linux 系统上,但建议在任何系统上简单地运行 AdaCore's installer,然后添加例如/opt/GNAT/2020/bin 到你的 $PATH。这是因为系统包版本通常已经过时了。
  • 现在 gnatmake 如果能找到它就会调用 gprbuild。
  • 我部分解决了这个问题。我找到了成功编译和链接的 Make 文件示例。但是,当我手动执行此操作时,我无法链接。显然我不完全理解这个过程。我相信这与我不清楚的 Make 文件中的最后一行命令有关。注意:gnatmake 可以通过标记 gnatmake -P 或 gnatmake -p 来处理 gpr。
【解决方案2】:

我的解决方案:

我将为面临同样问题的人提供我的解决方案。

要构建使用 plplot 库的myfile.adb,请编写以下代码,

gnatmake myfile.adb -aI/usr/share/ada/adainclude/plplotada -aL/usr/lib64/ada/adalib/plplotada -cargs -I/usr/include/plplot -largs -lplplotada -lplplot

要编译使用绘图库的myfile.adb,请编写以下代码,

-I/usr/share/ada/adainclude/plplotada -I/usr/include/plplot myfile.adb

在编译之前,请检查库是否如上所示。适用于linux系统。请注意,上述标志很长。如果可能的话,将所有这些标志包含在myfile.adb 中会很好。

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    相关资源
    最近更新 更多