1

I want to use local .h file for define same names used in my lib. My lib has a default value definition for this names, but I'd like change this default value using local .h file. However, I'm having unwanted behaviour. How can I solve this?

test.c

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "conf.h"
#include "mylib.h"

int main ()
{
  printf("Value in main: %d\n", NAMEDEFINITION);
  fn();

  return 0;
}

conf.h

#define NAMEDEFINITION 42 

mylib.h

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#ifndef NAMEDEFINITION
  #define NAMEDEFINITION 84 
#endif

void fn(); 

mylib.c

#include "mylib.h"

void fn()
{
  printf("Value in fn: %d\n", NAMEDEFINITION);
  return;
}

My compiling line and output:

user@local:~/user/test/c$ gcc test.c mylib.c -o test
user@local:~/user/test/c$ ./test
Value in main: 42
Value in fn: 84

[EDITED]

I'd like NAMEDEFINITION be "42" when I define it in conf.h, then print "42" in two main() calls. When it is not defined in conf.h, it would be its default value, "84" (print "84" in two main() calls.

Mario J.
  • 11
  • 2

3 Answers3

4
Value in main: 42

Reason for this is that in test.c you have included conf.h Now even though you include mylib.h, NAMEDEFINITION is visible in test.c and NAMEDEFINITION will have a value of 42. The ifdef in mylib.h will not be valid.

Value in fn: 84

In mylib.c you have not included conf.h. So the line #ifndef NAMEDEFINITION will be true and the value of NAMEDEFINITION will be 84.

If you want the value in fn to also print 42, you need to include conf.h in the mylib.c or mylib.h

Then if you comment out the line #define NAMEDEFINITION 42 in conf.h the value 84 will be printed both times.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
2

I see 3 ways you could go with this:

1. Make config file mandatory part of your library

mylib.h

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "conf.h"
#ifndef NAMEDEFINITION
  #error NAMEDEFINITION hasn't beed defined! Please edit conf.h!
#endif

void fn(); 

2. Define NAMEDEFINITION with compilation option

Example:

-D NAMEDEFINITION=42

With this approach you'd not use conf.h file at all. Downside to this method is that you have to make sure that you remember to include this option for your both .c file compilations.

3. Include conf.h with compilation option

Example:

-include conf.h

This is similar to method 2, but instead of defining symbol directly, you would force the inclusion of the conf.h instead.


What ever you do, make sure your header files have include guards. (Thanks @ChristianGibbons)

user694733
  • 15,208
  • 2
  • 42
  • 68
  • Note: My experience with gcc is limited, and mistakes are possible. If you have problems with 2 or 3, see gcc manual for details. – user694733 Sep 28 '18 at 13:01
  • 1
    Getting to the point of including a header within a header, it is probably also time to start talking about adding include guards to prevent multiple-inclusion. – Christian Gibbons Sep 28 '18 at 13:09
  • yes @ChristianGibbons! I use include guards. I forget to use it in this example. – Mario J. Sep 28 '18 at 13:19
  • @ChristianGibbons, I don't understand why include guards works for not include lib twice and not work for symbol definition. I think that defining symbol in main file, compiler will be "informed" of this definition when compiling mylib. – Mario J. Sep 28 '18 at 13:27
  • @MarioJ. You are dealing with different translation units. The translation unit associated with `main.c` has `NAMEDEFINITION` defined when it includes `conf.h`, but the translation unit associated with `mylib.c` does not have `NAMEDEFINITION` defined until `mylib.h` is included. – Christian Gibbons Sep 28 '18 at 14:13
1

The explanation is very simple.

First example (main) in the conf.h you define it. When you include mylib.h it is already defined and it is not redefined. So the value is 42

In the second example (fn) you just include mylib.h and #ifndef condition is true.

Macros are expanded before the compilation and are valid only in one compilation unit.,

0___________
  • 60,014
  • 4
  • 34
  • 74