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?
You can set
-DDATADIR='.'on Windows to get the desired behavior. You can useconfigure.acto check whether you are compiling on Windows. Here is one way to do it, adapted from the GTK source code:Then, add
@DATADEF@to yourmyprogram_CPPFLAGSinMakefile.am.