Question

I know how to write .h files and include guards. I want to write a new bar123.h file that contains global variables. Assume that bar123.c does not exist.

Other header files e.g. bar.h would include bar123.h, to make the global variables visible to where they are needed.

One question is, if foo.c includes foo.h which includes bar.h which includes bar123.h, does foo.c always include bar123.h?

Another question is, how should I modify the Makefile and the Kbuild files?

I am looking for a simple hackish solution.

Was it helpful?

Solution

To Answer your first query- Yes foo.c will always contain bar123.h .

Second query , In Make file the only change you need add is add this bar123.h in the list of header include file . since you dont have any new .o files being generated you dont need to change that part .

Hope it answers your queries

OTHER TIPS

A global variable must exist somewhere, and only in one place. That means it needs to be declared in one place only.

Remember: H files just tell the compiler that things exist somewhere. C files provide the actual definition of those things.

Let's say your global belongs nowhere else, we'll add a globals.c:

#include "globals.h"
int g_myGlobal;

There should be a corresponding globals.h:

#ifndef _GLOBALS_H
#define _GLOBALS_H

extern int g_myGlobal;

#endif // _GLOBALS_H

It is almost certainly an error to have a global variable declared in a .h file without extern. If you do such a thing, the compiler will try to declare that variable in every C file that includes that header, which will lead to "multiple definition" errors at link-time. (I'm assuming this is why you were asking "foo.c always include bar123.h?", and explains why it should not matter.)

main.h:

#include "globals.h"
int main() {
    g_myGlobal = 42;
}

The short of it is this. You can't include a global in a header file and then have that header includes elsewhere. You'll get the nasty "re-definition" error. Unless....

This is what I do.

Header file happy.h

extern int happy; // Global variable

Main file

#include "happy.h"

int happy = 12;

Other file.c

#include "happy.h"

int happy = 10;

On question 2: That depends on the compiler. Some will implicitly add it, however, good coding would be one where you include any .h files that you are intending to use a function from.

Note* It is never a good idea to place globals in a header. The intended use of a header is struct definitions and "public" function declarations. Struct / Union etc definitions are only included in headers when it becomes necessary. Example:

typedef struct
{
    int happy;
    char sad;
} my_mood_t;

my_mood_t *what_is_my_mood( int dog_ate_my_lunch );

From comment of OP:

Compiles fine but I am still getting the 'Unknown symbol baz' during runtime. ('umac: Unknown symbol baz (err 0)' on the openwrt router serial terminal Tera Term if that helps.)

If application linked fine, but fails to run with that error, it means that during build time linking, a dynamic library with that symbol was available. However, during runtime, that symbols is not available. A quick fix:

  • Determine what library has the symbol
  • Make sure that library exists where you are running the application, and note it's path
  • Instead of just running application, use command LD_LIBRARY_PATH=/path/to/the/library/directory application (you can also separately export LD_LIBRARY_PATH with that value, but then it will be set for every program, not just that one).

This is not the only way. You could also copy the library to system library directory and run ldconfig to update system library cache. Or you could use rpath mechanism. But start with LD_LIBRARY_PATH environment variable to get things working, before looking into these.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top