On Wednesday 29 September 2010 06:06:21 David C. Rankin wrote:
Guys,
This is a simple question, but I've googled and gnu c'ed myself into confusion on how to declare and make a simple static const char * have global scope and be available to a different source file without generating a warning that it is 'defined but not used' in the original file. Here is what I mean. I have a set of functions and data in files atm_fn.c and atm_fn.h (atmospheric functions) In atm_fn.h I have:
#include <stdio.h> #include <stdlib.h> #include <math.h>
#ifndef __atm_fn_h__ #define __atm_fn_h__
enum shapes {c172, airfoil, sphere_smooth, sphere_rough, cone, cube45, cyl_long, airfoil_2, cube, cyl_short, human, plate_flat_3d, plate_flat_2d };
static const char *shape_names[] = {"c172", "airfoil", "sphere_smooth", "sphere_rough", "cone", "cube45", "cyl_long", "airfoil_2", "cube", "cyl_short", "human", "plate_flat_3d", "plate_flat_2d" };
so, every single file that includes this creates space for it - an extra copy.
<snip>
#endif
I use the function in a test file strarray.c. There I have:
#include <stdio.h> #include <stdlib.h> #include <math.h>
#include <string.h>
#include "atm_fn.h"
const char *get_shape(int shp); <snip>
const char *get_shape (int shp) { return shape_names[shp]; } <snip>
When I compile it, I get the warning:
22:50 alchemy:~/dev/prg/ccpp/src-c/prj/test> gcc -o strt -Wall -lm -std=c99 strarray.c atm_fn.c atm_fn.h:14: warning: ‘shape_names’ defined but not used
14 static const char *shape_names[]...
I can't figure out how to get rid of the warning -- or whether I should even care. Second, the enum gives no warning. I'm sure that there is a rule that I don't know that says the enum is fine like this, but what about the warning on 'shape_names'?
It's a minor waste of memory.
Since there are a number of functions that will access the 'shape_names' array, I don't want to duplicate it. What's the best way to handle this? Wrap it in a struct (class like) data structure and declare a new instance in whatever source I'm using it in? Dunno -- that's why I'm asking the smart folks :p
Mark it extern in the header and add it in only one file with the names, Andreas -- Andreas Jaeger, Program Manager openSUSE, aj@{novell.com,opensuse.org} Twitter: jaegerandi | Identica: jaegerandi SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) Maxfeldstr. 5, 90409 Nürnberg, Germany GPG fingerprint = 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org