Solved How do I make #defines in a header file visible to the whole project?
-
I'm building a library (trying to get Qt to build the Google protocol buffer runtime).
First I ran the autoconfig scripts that Google provides, which generated a config.h file that #defines a bunch of flags for the environment.
I've added that config.h file to my Qt project along with all the necessary source, and also added a line to project's _global.h file to include config.h.
However, when I build, the #defines in config.h are not seen. It's probably a pretty basic problem, but I don't know what it is. Anybody?
Thanks!
-
As you are already aware, macros are only visible in the compilation unit where they exist. Inclusion of a global header that contains macros will make those macros available anywhere that header in explicitly included. Where you can run into trouble are if conditional macros block inclusion of the global macros...such is the case with guard macros like you'd use to keep a class definition from being multiply defined. Without digging into the specific code it would be hard to further diagnose your problem.
As a diagnostic step you could add the g++ option to preprocess each source file without actually compiling, and then inspect and trace the preprocessed output.
I can't really be any more help that that.
-
The config.h file contains, among many other things, this:
/* Define if you have POSIX threads libraries and header files. */ #define HAVE_PTHREAD 1
This is not seen in other files, causing compilation to fail as a result of the following:
#ifdef _WIN32 #define WIN32_LEAN_AND_MEAN // We only need minimal includes #include <windows.h> #define snprintf _snprintf // see comment in strutil.cc #elif defined(HAVE_PTHREAD) #include <pthread.h> #else #error "No suitable threading library available."
Somehow this config.h file works, in some environment. The question is why it's not working in this one.
-
@Stokestack said in How do I make #defines in a header file visible to the whole project?:
The question is why it's not working in this one.
Because you did not include config.h here?
-
It's in [project]_global.h.
Modifying every header file in the entire library to inject an include statement for this header file can't be correct. The configuration scripts don't do that, so there must be a way to compile it without editing every source file.
-
As @Kent-Dorfman already mentioned above you have to include the header file if you want to use it's content.
Configuration scripts (from whereever) only do what you tell them to do so I don't see what this should have to do with your problem. -
Well I'm curious as to how this header file is ever effective then. Because there is no script that I can see that goes into every header file in this library and injects an include statement for this config.h file.
What is the role of the [project]_global.h file if not to declare project-wide definitions and include project-wide headers?
-
Hi
I would guess on they use the features of autoconf+automake to
make every unit use it. -
@mrjj said in How do I make #defines in a header file visible to the whole project?:
I would guess on they use the features of autoconf+automake to
make every unit use it.Somewhere the generated config.h is included - either directly or indirectly. There is no other way to (except some compiler specific switches but this is more a hack)
-
@Christian-Ehrlicher
ok, i have not used autoconf+automake that much so if you say
they cant rig a make file to auto include pr unit then config.h must be included as
there is no other magic possible :) -
Thanks a lot for the replies. I just put it into Google's common header file and called it a day.
I have much bigger problems to solve in regard to building protobuf for cross-platform use. Seems like no one else is doing it.
-
@Stokestack said in How do I make #defines in a header file visible to the whole project?:
and called it a day.
If your issue is solved, please don't forget to mark your post as such. Thanks.