使用 GNU AutoTools 设置和使用数据目录路径
我正在尝试将 GNU AutoTools 用于我的 C++ 项目。我编写了configure.ac、makefile.am等。我有一些程序在执行过程中使用的文件,例如模板文件、XML模式等。因此,我沿着可执行文件安装/复制这些文件,为此我使用类似的文件:
abcdir = $(bindir)/../data/abc/
abc_DATA = ../data/knowledge/abc.cc
现在它正确复制了文件,我的程序安装结构如下所示:
<installation_dir>/bin/<executableFile>
<installation_dir>/data/abc/abc.cc
现在的问题是,在源代码中我实际上使用了这些文件(abc.cc 等),为此我需要这些文件所在的路径打开它们。一种解决方案是定义(使用 AC_DEFINE)一些变量,例如 _ABC_PATH_ ,它指向安装路径,但究竟如何做到这一点?或者有没有更好的方法可以做到这一点。例如,在源代码中,我做了类似的事情:
...
ifstream input(<path-to-abc-folder> + "abc.cc"); // how to find <path-to-abc-folder>?
..
I am trying to use GNU AutoTools for my C++ project. I have written configure.ac, makefile.am etc. I have some files that are used by the program during execution e.g. template files, XML schema etc. So, I install/copy these files along the executable, for which I use something like:
abcdir = $(bindir)/../data/abc/
abc_DATA = ../data/knowledge/abc.cc
Now it copies the file correctly and My program installation structure looks somethings as follows:
<installation_dir>/bin/<executableFile>
<installation_dir>/data/abc/abc.cc
Now the problem is that in the source code I actually use these files (abc.cc etc.) and for that I need path of where these files resides to open them. One solution is to define (using AC_DEFINE) some variable e.g. _ABC_PATH_ that points to the path of installation but how to do that exactly?. OR is there any better way to do that. For example, in source code, I do something like:
...
ifstream input(<path-to-abc-folder> + "abc.cc"); // how to find <path-to-abc-folder>?
..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AC_DEFINE 解决方案原则上很好,但需要进行类似 shell 的变量扩展。也就是说,_ABC_PATH_ 将扩展为“${bindir}/../data/abs”,而不是/data/abc。
一种方法是通过 -D 标志定义路径,该路径由 make: 扩展
,原则上工作正常,但您必须在 myprogram 的依赖项中包含 config.status 。
如果您有许多此类替换变量,则应该推出一个 paths.h 文件,该文件是
由 automake 生成,规则如下:
作为旁注,您确实了解 ${prefix} 和 ${datarootdir} 以及朋友,不是吗?如果没有,最好阅读它们;如果用户确实设置了 ${exec_prefix},则 ${bindir}/.. 不一定等于 ${prefix}。
The AC_DEFINE solution is fine in principle, but requires shell-like variable expansion to take place. That is, _ABC_PATH_ would expand to "${bindir}/../data/abs", not /data/abc.
One way is to define the path via a -D flag, which is expanded by make:
which works fine in principle, but you have to make include config.status in the dependencies of myprogram.
If you have a number of such substitution variables, you should roll out a paths.h file that is
generated by automake with a rule like:
As a side-note, you do know about ${prefix} and ${datarootdir} and friends, don't you? If not, better read them up; ${bindir}/.. is not necessarily equal to ${prefix} if the user did set ${exec_prefix}.