C headers, modular programming and linking

Hello, I have a problem when trying to compile a program with multiple modules. I've made an Abstract Data Type and placed all the declarations and function prototypes into a header "hwconf.h" . The implementation of the ADT is in a different file "hwconf.c" where I included the header. To test this module I've made a simple program "cli.c". The problem is that when I try to compile the whole program I get a linker error, seeing multiple declarations of the constants defined in "hwconf.h". I tried to figure out what's wrong and I found out that the C compiler , unlike the c++ one, it actualy allocates memory for the constants. How can I avoid the linker error, without usig the "#define" dirrective ? What are the standards for C headers?

Comments

Now you've got one copy of global_x, notionally stored in hwconf.c, but accessible to everyone who includes hwconf.h.You must not use the identifier global_x for a different global in the same program, or you will get a collision. For this reason most people would call it something like hwconf_x.

You don't use global variables at all in modular programming, they are considered very poor programming, and so is the "extern" keyword when used for variables. Also, there is never a reason to use global variables in the C language.

This is how you write a code module properly in C:

[code]/* h file for "module" */

#ifndef MODULE_H /* this is called header guards and they prevent linking errors */#define MODULE_H

/* A private variable. The "_" syntax is commonly used for private variables or functions. The static keyword guarantees that the variable can't be accessed from outside of the module, neither accidently nor with purpose. It can however be accessed through the whole code module. */

Now , I figured out that it's better to use "#define" for the non-string values(to save some memory) but for the default values HW_TYPES I needed a way to iterate through them with a for. I don't know another way other than the one with an array of const char *. I don't see a reason to make get functions for global constants. I was planning to make setters and getters for my HwConf object, but for now, it works this way.

: Isn't that the encapsulation common for Object Orientated Programming?

Yes, encapsulation is indeed a fundamental cornerstone in OO.

I had problems with the declarations of my global : constants

Strictly speaking, you shouldn't need any global constants. The few you need could perhaps be declared as extern, but it isn't pretty and will likely lead to "spaghetti". The important part here is to consider whether the whole program actually needs to know about a particular variable or not. A get function is a better solution, and most modern C compilers know how to inline such functions.

: Now , I figured out that it's better to use "#define" for the : non-string values(to save some memory)

How exactly does that save memory? #defined constants usually consume -more- memory than const, because when you call a #defined constant from the code, it is usually hardcoded into the machine code, and therefore the program itself gets larger. With const, the compiler is often able to optimize better.

Also, when you write const you can write the int size explicitly, declare 1 byte ints as chars etc. With defines, everything will be "int".

Well, I want these values to make my code more readable and easier to maintain. I thought that by using macros I saved the space requiered by those constants, but since the exe is loaded into memory, you're right.. I saved nothig or spent more.

For my curiosity , I opened some headers (the ones found in /usr/src and /usr/include on a linux machine) and saw alot of defines for default values, like those from "limits.h".Since I need some of these values at compile-time, I still need to use #define(unlike c++ where I can use only const).