简体   繁体   English

使用autotools安装前缀

[英]Use autotools installation prefix

I am writing a C++ program using gtkmm as the window library and autotools as my build system. 我正在编写一个C ++程序,使用gtkmm作为窗口库,autotools作为我的构建系统。 In my Makefile.am, I install the icon as follows: 在我的Makefile.am中,我按如下方式安装图标:

icondir = $(datadir)/icons/hicolor/scalable/apps
icon_DATA = $(top_srcdir)/appname.svg

EDIT : changed from prefix to datadir 编辑 :从prefix更改为datadir

This results in appname.svg being copied to $(datadir)/icons/hicolor/scalable/apps when the program is installed. 这导致appname.svg在安装$(datadir)/icons/hicolor/scalable/apps时被复制到$(datadir)/icons/hicolor/scalable/apps In my C++ code, I would like to access the icon at runtime for a window decoration: 在我的C ++代码中,我想在运行时访问该图标以进行窗口装饰:

string iconPath = DATADIR + "/icons/hicolor/scalable/apps/appname.svg";
// do stuff with the icon

I am unsure how to go about obtaining DATADIR for this purpose. 我不确定如何为此目的获得DATADIR I could use relative paths, but then moving the binary would break the icon, which seems evident of hackery. 我可以使用相对路径,但随后移动二进制文件会破坏图标,这似乎是hackery的显而易见。 I figure that there should be a special way to handle icons separate from general data, since people can install 3rd party icon packs. 我认为应该有一种特殊的方法来处理与一般数据分开的图标,因为人们可以安装第三方图标包。 So, I have two questions: 所以,我有两个问题:

  1. What is the standard way of installing and using icons with autotools/C++ /gtkmm ? 使用 autotools / C ++ / gtkmm 安装 和使用 图标的标准方法是什么?

Edit : gtkmm has an IconTheme class that is the standard way to use icons in gtkmm. 编辑 :gtkmm有一个IconTheme类,它是在gtkmm中使用图标的标准方法。 It appears that I add_resource_path() (for which I still need the installation prefix), and then I can use the library to obtain the icon by name. 看来我是add_resource_path() (我仍然需要安装前缀),然后我可以使用该库来按名称获取图标。

  1. What is the general method with autotools/C++ to access the autotools installation prefix? 使用autotools / C ++访问autotools安装前缀的一般方法是什么?

In your Makefile.am, use the following 在Makefile.am中,使用以下命令

AM_CPPFLAGS = -DPREFIX='"$(prefix)"'

See Defining Directories in autoconf's manual. 请参阅autoconf手册中的定义目录

To convey data determined by configure to your source files, the primary methods available are to write them in a header that your sources #include or to define them as macros on the compiler command line. 要将configure确定的数据传送到源文件,可用的主要方法是将它们写入源#include的头中,或者在编译器命令行中将它们定义为宏。 These are handled most conveniently via the AC_DEFINE Autoconf macro. 通过AC_DEFINE Autoconf宏可以最方便地处理这些问题。 Under some circumstances, you might also consider converting source files to templates for configure to process, but except inasmuch as Autoconf itself uses an internal version of that technique to build config.h (when that is requested), I wouldn't normally recommend it. 在某些情况下,您可能还会考虑将源文件转换为模板以供configure处理,但由于Autoconf本身使用该技术的内部版本来构建config.h (当请求时),我通常不推荐它。

HOWEVER, the installation prefix and other installation directories are special cases. 但是,安装前缀和其他安装目录是特殊情况。 They are not finally set until you actually run make . 在你真正运行make之前,它们不会被最终设置。 Even if you set them via the configure 's command-line options, you can still override that by specifying different values on the make command line. 即使您通过configure的命令行选项设置它们,您仍然可以通过在make命令行上指定不同的值来覆盖它。 Thus, it is not safe to rely on AC_DEFINE for this particular purpose, and in fact, doing so may not work at all ( will not work for prefix itself). 因此,它是不是安全的依靠AC_DEFINE这一特定目的,而事实上,这样做可能不会在所有的工作( 不会为工作prefix本身)。

Instead, you should specify the appropriate macro definition in a command-line option that is evaluated at make time. 相反,你应该指定,在评估一个命令行选项相应的宏定义make时间。 You can do this for all targets being built by setting the AM_CPPFLAGS variable in your Makefile.am files, as demonstrated in another answer . 您可以通过在Makefile.am文件中设置AM_CPPFLAGS变量为所有正在构建的目标执行此操作,如另一个答案所示 That particular example sets the specified symbol to be a macro that expands to a C string literal containing the prefix. 该特定示例将指定的符号设置为扩展为包含前缀的C字符串文字的宏。 Alternatively, you could consider defining the whole icon directory as a symbol. 或者,您可以考虑将整个图标目录定义为符号。 If you need it only for one target out of several then you might prefer setting the appropriate onetarget_CPPFLAGS variable. 如果只需要一个目标中的一个目标,那么您可能更喜欢设置适当的onetarget_CPPFLAGS变量。

As an aside, do note that $(prefix)/icons/hicolor/scalable/apps is a nonstandard choice for the installation directory for your icon. 另外,请注意$(prefix)/icons/hicolor/scalable/apps是图标安装目录的非标准选择。 That will typically resolve to something like /usr/local/icons/hicolor/scalable/apps . 这通常会解决为/usr/local/icons/hicolor/scalable/apps The conventional choice would be $(datadir)/icons/hicolor/scalable/apps , which will resolve to something like /usr/local/share/icons/hicolor/scalable/apps . 传统的选择是$(datadir)/icons/hicolor/scalable/apps ,这将解析为/usr/local/share/icons/hicolor/scalable/apps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM