You are not logged in.

#1 2010-03-12 22:39:43

aardwolf
Member
From: Belgium
Registered: 2005-07-23
Posts: 304

Linux programming & configuration help

When making a program that should work both on Linux and on other operating systems such as Windows: how should you specify, in the C(++) source code, where the data directories and so on are?

I've been trying to search for information about this, but didn't find anything. I have found the GNU coding standards: http://www.gnu.org/prep/standards/ and in chapter 7 they mention a few things about configuration and makefiles and some things about paths for data files and so on, but nothing specific about how you can actually implement that in the source code. The mention somewhere that certain paths should not cause a recompile if you do "make install", but they don't mention how you can achieve that.

What I mean is, for example, a directory with data files the program needs to run (e.g. graphics resources), how should your program know the location of that directory?

Should it be hardcoded in the source code? I don't think so, it can't be portable that way.
Should the program read it at runtime from somewhere? If so, from where? From a config file? But then how should the program know the path of that config file?
Should it be a compiler flag, e.g. a preprocessing directive, that will then be built into the program when it is compiled? If so, how should this flag be given to the compiler on various systems?


If you want to release a binary version of your program that works on many distros, how should that be done in a way compatible with the above? What I currently do is to have the data files the program needs, in the same directory in its executable. This works on every distro, because if you put the executable and data files in any directory, and run it, it works. But I was told that this system will never work for packages for distros, because data files should not go in the "bin" directory where the executable is.

And on Windows there's no such thing as a "bin" directory. How can all the above be made compatible with such other OSes as well?

And how should all this be done for the following types of data/directories/...:

*) Fixed data files (such as graphics resources) the program needs to be able to run
*) A configuration file that should be able to be modified in user-space (currently I put this in  ~/.config/ProgramName and I think I got this one right actually, so no problems here, just listing it here for completeness)
*) A directory with plugins: the program supports reading .so files as plugins. Currently I also put this in a folder in the same folder as the executable, but I don't think that is the proper system for linux packages either. What IS the proper system?

So, the question is: what paths to use for these things, and how and when to communicate these paths to the program in a portable way?

Offline

#2 2010-03-13 02:18:28

bremarv
Member
Registered: 2010-03-12
Posts: 26

Re: Linux programming & configuration help

For different OS' I think you have to/should check defines. I cant remember what the different ones are, since I never really use it.

As for figuring out where to put the files, I would have checked where the other packages put their files ( pacman -Ql ).

As for configs I like putting them in ~/.config/progname.

sorry I could not be of more help.


"As a former philosophy major, it disturbs me to think that things disappear when no one is looking at them, but that's exactly what happens in Python" - Mark Pilgrim

Offline

#3 2010-03-13 03:35:11

lagagnon
Member
From: an Island in the Pacific...
Registered: 2009-12-10
Posts: 1,087
Website

Re: Linux programming & configuration help

As far as I am aware if you want to write the same software for both Windows and Linux machines using C++ then you need different versions of the code. Especially if the software uses GUI then you will have to use different widget sets. If you want to write software that works on different OS's without a lot of extra work then you should probably use Java or Tcl/Tk, not C++ (IMHO).


Philosophy is looking for a black cat in a dark room. Metaphysics is looking for a black cat in a dark room that isn't there. Religion is looking for a black cat in a dark room that isn't there and shouting "I found it!". Science is looking for a black cat in a dark room with a flashlight.

Offline

#4 2010-03-13 04:11:04

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

Re: Linux programming & configuration help

I second that, you might be able to make it platform independent by defining constants and other techniques but if it's anything beyond a CLI app your definately going to need multiple versions of code. There are a variety of platform independent languages, each with different styles and purposes, that you can use if porting is too difficult or costly.

Last edited by Anikom15 (2010-03-13 04:11:22)


Personally, I'd rather be back in Hobbiton.

Offline

#5 2010-03-13 04:28:21

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,410
Website

Re: Linux programming & configuration help

You can use configure to detect the OS and select appropriate places for the files.

Offline

Board footer

Powered by FluxBB