[opensuse] C help needed (howto eliminate: warning: ‘shape_names’ defined but not used)
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" }; <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'? 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 -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
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
On 09/28/2010 11:49 PM, Andreas Jaeger wrote:
Mark it extern in the header and add it in only one file with the names, Andreas
Thanks Andreas, That where I was confused. I read the extern part of the gnu c manual at least 3 times and still couldn't get it sorted. I'll give it another go. This is what confused me more: http://gcc.gnu.org/gcc-4.3/porting_to.html Since I'm using the -std=c99 flag for strtof, that put me within the caveat: "When compiling with -std=c99 or -std=gnu99, the extern inline keywords changes meaning. GCC 4.3 conforms to the ISO C99 specification, where extern inline is very different thing than the GNU extern inline extension. " Which is where my confusion stemmed from. I'll play with it a little later. I'm 3 hours post-op and I'm going back to bed :p Thanks. -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 09/29/2010 03:12 PM, David C. Rankin wrote:
On 09/28/2010 11:49 PM, Andreas Jaeger wrote:
Mark it extern in the header and add it in only one file with the names, Andreas
Thanks Andreas,
That where I was confused. I read the extern part of the gnu c manual at least 3 times and still couldn't get it sorted. I'll give it another go. This is what confused me more:
http://gcc.gnu.org/gcc-4.3/porting_to.html
Since I'm using the -std=c99 flag for strtof, that put me within the caveat:
"When compiling with -std=c99 or -std=gnu99, the extern inline keywords changes meaning. GCC 4.3 conforms to the ISO C99 specification, where extern inline is very different thing than the GNU extern inline extension. "
Which is where my confusion stemmed from. I'll play with it a little later. I'm 3 hours post-op and I'm going back to bed :p
Thanks.
The solution was found here: http://www.linuxforums.org/forum/programming-scripting/133145-warning-define... and was to 'declare' shape_names using extern in atm_fn.h (and don't assign it in .h): extern const char *shape_names[]; and then define it (i.e. without using extern) in exactly one .c file (atm_fn.c): 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" }; -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (2)
-
Andreas Jaeger
-
David C. Rankin