在 Ada 编译时指定不同平台特定的包 (GNAT)

发布于 2024-09-11 00:51:05 字数 544 浏览 2 评论 0原文

我对 Ada 编程世界还是个新手,所以如果这个问题很明显,请原谅我。

我正在考虑开发一个应用程序(在 Ada 中,使用 2005 版本中的功能),该应用程序从串行端口读取数据,并基本上执行从外部设备接收的字符串和数字的操作。

现在我的意图是可能首先使用 Florist 和 POSIX 终端接口在 Linux 上完成所有串行工作...我将在其他时间访问 Windows/MacOS/etc...但我想将该选项保留为打开状态。

无论我做什么,我都想遵循 Ada 最佳实践。因此,我不想像 C 下的条件编译那样进行黑客攻击(我知道 Ada 无论如何都没有),我想知道您应该如何从命令行指定包文件中的更改(例如 gnatmake)?

我现在唯一能想到的是我可以将所有平台包命名为完全相同(即包名称 Serial.Connector 具有相同的文件名)并将它们放置在项目存档中的不同文件夹中,然后在编译时指定目录/库使用 -I 参数查找文件并更改不同平台的目录名称。

这是我使用 C/C++ 向 GCC 展示的方式...这仍然是使用 GNAT 的 Ada 的最佳方式吗?

谢谢, -乔什

I'm still new to the Ada programming world so forgive me if this question is obvious.

I am looking at developing an application (in Ada, using the features in the 2005 revision) that reads from the serial port and basically performs manipulation of the strings and numbers it receives from an external device.

Now my intention was to likely use Florist and the POSIX terminal interfaces to do all the serial work on Linux first....I'll get to Windows/MacOS/etc... some other time but I want to leave that option open.

I would like to follow Ada best practices in whatever I do with this. So instead of a hack like conditional compilation under C (which I know Ada does not have anyway) I would like to find out how you are suppose to specify a change in package files from the command line (gnatmake for example)?

The only thing I can think of right now is I could name all platform packages exactly the same (i.e. package name Serial.Connector with the same filenames) and place them in different folders in the project archive and then upon compilation specify the directories/Libraries to look in for the files with -I argument and change directory names for different platforms.

This is way I was shown for GCC using C/C++...is this still the best way with Ada using GNAT?.

Thanks,
-Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月野兔 2024-09-18 00:51:05

这是处理这种情况的一种完全可以接受的方式。如果可能的话,您应该有一个通用的包规范(或多个规范,如果多个合适的话),所有特定于平台的内容都严格限制在相应的包体变体中。

(如果您确实想沿着预处理器路径走下去,有一个名为 gnatprep 可以使用,但我也不喜欢条件编译,所以我建议继续使用单独的子目录方法。)

That's a perfectly acceptable way of handling this kind of situation. If at all possible you should have a common package specification (or specifications if more than one is appropriate), with all the platform-specific stuff strictly confined to the corresponding package body variations.

(If you did want to go down the preprocessor path, there's a GNAT preprocessor called gnatprep that can be used, but I don't like conditional compilation either, so I'd recommend staying with the separate subdirectories approach.)

守护在此方 2024-09-18 00:51:05

您可以使用 GNAT 项目文件package Naming:摘录自一个真实的示例,我想在同一目录中的两个版本的包之间进行选择,其中一个带有调试添加,是为了

...
type Debug_Code is ("no", "yes");
Debug : Debug_Code := External ("DEBUG", "no");
...
package Naming is
   case Debug is
      when "yes" =>
         for Spec ("BC.Support.Managed_Storage")
           use "bc-support-managed_storage.ads-debug";
         for Body ("BC.Support.Managed_Storage")
           use "bc-support-managed_storage.adb-debug";
      when "no" =>
         null;
   end case;
end Naming;

选择特殊的命名,将环境变量 DEBUG 设置为 yes 或使用 gnatmake -XDEBUG=yes 进行构建。

You could use the GNAT Project file package Naming: an extract from a real example, where I wanted to choose between two versions of a package in the same directory, one with debug additions, is

...
type Debug_Code is ("no", "yes");
Debug : Debug_Code := External ("DEBUG", "no");
...
package Naming is
   case Debug is
      when "yes" =>
         for Spec ("BC.Support.Managed_Storage")
           use "bc-support-managed_storage.ads-debug";
         for Body ("BC.Support.Managed_Storage")
           use "bc-support-managed_storage.adb-debug";
      when "no" =>
         null;
   end case;
end Naming;

To select the special naming, either set the environment variable DEBUG to yes or build with gnatmake -XDEBUG=yes.

感受沵的脚步 2024-09-18 00:51:05

是的,在 Ada 中处理此问题的普遍接受的方法是使用由构建系统选择的不同文件来执行此操作。 Gnu make 几乎是多平台的,并且可以允许您在不同的配置下构建不同的文件(具有不同的名称和/或目录以及所有内容)。

事实上,我发现这是在 C 语言中执行此操作的一种更好的方法(优于 #ifdef)。

Yes, the generally accepted way to handle this in Ada is to do it with different files, selected by your build system. Gnu make is about as multiplatform as it gets, and can allow you to build different files (with different names and/or directories and everything) under different configurations.

As a matter of fact, I find this a superior way (over #ifdefs) to do it in C as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文