Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a C++ program to read predefined files after installation on linux

My project folder has the following structure

-Project/
        /src
             -Main.cpp
             -MyReader.cpp
        /headers
             -MyReader.h
        /DataFiles
             -File.dat
             -File1.dat

My class Object.cpp has a couple of methods which reads from File.dat and File1.dat and parse the information to Map objects. My problem is that I am using Autotools (in which I'm very very newbie) for generating config and installer files and I don't know how to make all the DataFiles files accessible for the program after installation. The program doesn't work properly because of the code fails when trying to read those files through relative paths. Locally, the program runs perfectly after executing in terminal make && ./program.

How can I solve this issue? Thanks in advance for your help!

like image 293
Vladimir Tepes Avatar asked Nov 21 '25 21:11

Vladimir Tepes


1 Answers

A platform independent way to do this with Autotools is using the $(datadir) variable to locate the system data directory and work relative to that.

So in your Makefile.am file you can create a name like this:

myprog_infodir = $(datadir)/myprog

# Set a macro for your code to use
myprog_CXXFLAGS = -DDATA_LOCATION=\"$(datadir)/myprog\"

# This will install it from the development directories
myprog_info_DATA = $(top_srcdir)/DataFiles/File.dat $(top_srcdir)/DataFiles/File1.dat

# make sure it gets in the installation package
extra_DIST = $(top_srcdir)/DataFiles/File.dat $(top_srcdir)/DataFiles/File1.dat

Then in your program you should be able to refer to the data like this:

std::ifstream ifs(DATA_LOCATION "/File.dat");

Disclaimer: Untested code

like image 194
Galik Avatar answered Nov 23 '25 10:11

Galik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!