我正在大学开设编程课程,选择的语言是Ada.我在Kate编写代码并使用GNAT 4.6.3进行编译.我们必须为我们的程序使用教师提供的库,如下所示:
with foo;
use foo;
Run Code Online (Sandbox Code Playgroud)
当然,然后该文件foo.adb必须包含在与我的源文件相同的目录中.由于多个项目依赖于这个库,并且我喜欢将每个项目保存在自己的子目录中,因此我必须将库文件复制到每个新项目中.更不用说我的库代码和源代码都在同一目录中.
那么有什么方法可以去:
with ../../lib/foo
use ../../lib/foo
Run Code Online (Sandbox Code Playgroud)
?
我已经尝试了一下,但我发现的只是关于编译器选项的东西.我宁愿不必乱用那些,特别是因为只有某些项目需要这个特定的库,所以将它添加到全局编译器设置并让编译器毫无意义地搜索路径它没有意义不需要搜索.
我会在命令行中使用GNAT Project工具gnatmake.
我只是设置了一个小例子(所以我可以肯定我所说的有效!).我有3个目录; teacher/包含教师提供的源代码,我假设您不想更改,也可能没有写入权限,jacks_lib/包含teacher.gpr哪些内容teacher/(您可以将自己的库代码放在那里)并jack/包含您的代码main.adb和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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
jack/main.adb:
with Foo;
procedure Main is
begin
null;
end Main;
Run Code Online (Sandbox Code Playgroud)
然后,在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
Run Code Online (Sandbox Code Playgroud)
我应该补充一点,我在Mac OS X上使用的是GCC 4.7.0,但这对于任何最近的GNAT都应该可以正常工作.
编译器选项是您管理构建的源代码位置的方式 - 定义"搜索路径" - 特别是基于gcc(如GNAT)和大多数其他编译器的"-I"(包括)选项.
如果您是从命令行构建的,那只需要:
gnatmake -I../../lib/foo -Iother/path -Iyet/another/path project1_main.adb
gnatmake -I../../lib/foo -Isome/path -Iyet/another/path project2_main.adb
Run Code Online (Sandbox Code Playgroud)
如果您正在使用GPS(GNAT Programming Studio),请打开"项目属性"对话框,选择"源目录"选项卡,然后在其中添加搜索路径.(您也可以直接直接编辑项目属性文件(".gpr"),但我很少这样做.YMMV.)编译器设置可以基于每个项目轻松设置,因此没有"全局编译器设置"问题哪一个人不得不关心自己.