Reputation: 2267
I want a distribute both a data file and a program coded in C which opens the data file and then closes it, and have it portable. The program source would look something like this:
#include <stdio.h>
int main(int argc, char** argv)
{
fclose(fopen("data.dat", "rb"));
return 0;
}
I'm also using Autotools:
$ ls -R
.:
configure.ac
dat
Makefile.am
src
./dat:
data.dat
Makefile.am
./src:
hello.c
Makefile.am
In Linux when installing software the files usually go into specific directories, e.g. hello would go in /usr/local/bin and data.dat would go in /usr/local/share, and the installer can adjust these directories. Therefore the program would have to be adapt to a change in the path of the data file, specifically the datadir variable.
#src/Makefile.am
AM_CPPFLAGS=-DDATADIR='"$(datadir)"'
...
.
//src/hello.c
...
fclose(fopen(DATADIR "/data.dat", "rb+"));
...
However in Windows, software is not installed this way, and all of the different files are usually installed into one directory. To do this, the bindir and datadir could be made to / when running configure, however that would make the fopen argument invalid.
Is there any way to adjust my setup so that the program would refer to the correct path without using #ifdefs?
Upvotes: 0
Views: 498
Reputation: 57920
You can set -DDATADIR='.'
on Windows to get the desired behavior. You can use configure.ac
to check whether you are compiling on Windows. Here is one way to do it, adapted from the GTK source code:
AC_CANONICAL_HOST
AC_MSG_CHECKING([for native Win32])
case "$host" in
*-*-mingw*)
os_win32=yes
;;
*)
os_win32=no
;;
esac
AC_MSG_RESULT([$os_win32])
if test "$os_win32" = "yes"; then
DATADEF='-DDATADIR=.'
else
DATADEF="-DDATADIR=$daatadir"
fi
AC_SUBST(DATADEF)
Then, add @DATADEF@
to your myprogram_CPPFLAGS
in Makefile.am
.
Upvotes: 2