Get Ada (compiled with GNAT) to import files from outside current directory?

六月ゝ 毕业季﹏ 提交于 2019-12-05 02:35:11

I would use the GNAT Project facility with command-line gnatmake.

I just set up a little example (so I can be sure what I say works!). I have 3 directories; teacher/ contains the teacher-provided source, which I assume you won't want to change and may not have write access to anyway, jacks_lib/ contains teacher.gpr which points to teacher/ (you could put your own library code there as well) and jack/ contains your code main.adb and main.gpr.

jacks_lib/teacher.gpr:

project Teacher is
   --  This project calls up the teacher-supplied source.

   --  This is a list of paths, which can be absolute but
   --  if relative are relative to the directory where this .gpr
   --  is found.
   for Source_Dirs use ("../teacher");

   --  Keep the built objects (.ali, .o) out of the way. Use the -p
   --  gnatmake flag to have directories like this built
   --  automatically.
   for Object_Dir use ".build";
end Teacher;

jack/main.gpr:

--  teacher.gpr tells where to find library source and how to build it.
with "../jacks_lib/teacher";

project Main is
   --  for Source_Dirs use ("."); (commented out because it's the default)

   --  Keep built objects out of the way
   for Object_Dir use ".build";

   --  Build executables here rather than in Object_Dir
   for Exec_Dir use ".";

   --  What's the main program? (there can be more than one)
   for Main use ("main.adb");
end Main;

jack/main.adb:

with Foo;
procedure Main is
begin
   null;
end Main;

Then, in jack/,

$ gnatmake -p -P main.gpr
object directory "/Users/simon/tmp/jacks_lib/.build" created for project teacher
object directory "/Users/simon/tmp/jack/.build" created for project main
gcc -c -I- -gnatA /Users/simon/tmp/jack/main.adb
gcc -c -I- -gnatA /Users/simon/tmp/teacher/foo.ads
gnatbind -I- -x /Users/simon/tmp/jack/.build/main.ali
gnatlink /Users/simon/tmp/jack/.build/main.ali -o /Users/simon/tmp/jack/main

I should add that I'm using GCC 4.7.0 on Mac OS X, but this should work fine with any recent GNAT.

Compiler options are the way you manage source code locations for a build--defining "search paths"--particularly the "-I" (include) option for gcc-based (like GNAT) and most other compilers.

If you're building from the command line, it's simply a matter of:

gnatmake -I../../lib/foo -Iother/path -Iyet/another/path project1_main.adb

gnatmake -I../../lib/foo -Isome/path -Iyet/another/path project2_main.adb

If you're using GPS (GNAT Programming Studio), then open the Project Properties dialog, select the Source dirs tab, and add the search paths there. (You can also directly edit a Project Properties file (".gpr") directly, but I rarely do that. YMMV.) Compiler settings are easily set up on a per-project basis, so there's no "global compiler setting" issue with which one would have to concern themself.

Amplifying on @MarcC's answer, the GNAT User's Guide covers gnatmake in chapter 6, under §6.2 Switches for gnatmake: Source and library search path switches; the guide should be included with your distribution.

The gnatmake command is a convenient target for make, as shown in this example.

Its not been explicitly mentioned in the answers sofar, so I will say it:

The with & use clauses in Ada make no claims about the location of the withed & used units, only that they exist.

When it comes to actually finding the units, that is entirely up to the make scripts & .gpr files & compilation options or whatever else you come up with.

This is how Ada creates a degree of code portability (by not binding the code to a directory structure!), You just need to correct the compilation options :)

Also, learning about compiler options is almost never a bad idea, it will serve you well in Ada & many other languages.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!