On Sat, Dec 20, 2008 at 11:45 AM, Joseph Smith joe@settoplinux.org wrote:
Hello, I'm trying to setup a #define macro that can be used through out the program and also part of another macro. I know this won't work but you may get the idea of what I am trying to do:
#define PARALLEL_PORT parport0 #define PARPORT_BASE_FILE "/proc/sys/dev/parport/"PARALLEL_PORT"/base-addr" ... ... fd = open("/dev/PARALLEL_PORT",O_RDWR); ... printf(PARPORT_BASE_FILE\n); ...
you can do this kind of magic. I frown on it. Why? Because it is 1. not totally portable 2. saving time on a one-time operation which takes a trivial amount of time 3. over-emphasizing cpp magic, which is way overdone nowadays. 4. inflexible; why are you hard-coding a parallel port name at compile time? What if you need to use a different one-- what do you do, recompile the program?
An easy and portable and flexible way to do this: char *partport = "parport0"; static char parportname[MAXPATHLEN];
sprintf(parportname, "/proc/sys/dev/parport/%s/base-addr", parport );
now add the code to set parport at runtime based on options.
In general, but especially for those things which can change, it's a very bad idea to hard-code them in cpp magic.
Of course, this code is still very linux specific, but ...
thanks
ron