3

I would like to know how can I get the --prefix and the --exec-prefix values passed to the configure script (autotools) in my c source code?

For example if I'm using autotools to build a package in linux I do:

./configure --prefix=/usr/local/apps --exec-prefix=/usr/local/apps &&
make &&
make install

And all the files that the application uses are installed depending on that values.

So in my application for open a file, for example, I need to know that value to know where the images where installed and then open it.

I could hard code that value but I don`t want to do that because I could only install the application there.

How can I know that values?

I'm using Anjuta.

Thanks in advance for your help.

jww
  • 97,681
  • 90
  • 411
  • 885
John Smith
  • 31
  • 2

1 Answers1

5

The usual thing is to define a value for the preprocessor, either in config.h via AC_DEFINE, or as a flag in AM_CPPFLAGS. For example, in Makefile.am:

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

This allows the code to contain things like printf( "prefix = %s\n", PREFIX ) Keep in mind that the value of the string in the source code will not reflect any value of DESTDIR, and so may not work if the user does a staged installation.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • The solution via AM_CPPFLAGS worked perfectly. Thanks a lot for your help!!! I was thinking than never would find the answer on the web. All the searches were giving me a lot of results related but without the answer. Now I know how to refine my search. I only had to adjust a bit the slashes: AM_CPPFLAGS = -DPREFIX=\""$(prefix)"\" How I said before was really useful and thanks a lot. Now it's time to me to learn how to do it with AC_DEFINE ;) – John Smith Feb 01 '13 at 22:24
  • 1
    @JohnSmith if the answer worked for you, you might want to "accept" it. – umläute Feb 06 '13 at 09:01