Such a Noob Question about Macros

Hi,

As the title of the topic says, I can't figure out this basic concept of macro inclusion. I'm writing a small static lib, that has the following in the header file called main_header.h:

1
2
3
4
5
6
7
#if defined(UNICODE) && defined(_UNICODE)
typedef std::wstring tstring;
typedef std::wstringstream tstringstream;
#else
typedef std::string tstring;
typedef std::stringstream tstringstream;
#endif 


There is more to this, but basically I want to be able to do this in a header file outside the project

1
2
3
4
#define UNICODE
#define _UNICODE

#include "main_header.h" 

However, the #defines do not get passed into main_header.h. How can I do this without manually changing main_header.h?

I am missing something so obvious :(
Everything you define before the inclusion should be reprocessed in the header as well.
Are you #including that header multiple times?
Basically, I have a main header file that includes a bunch of other headers. Each of these other headers has a corresponding cpp file. In each of these headers it checks for unicode and does the appropriate typedef. I wish to define Unicode in, lets say, the cpp file containing main, and then in that file include main-header.h.
1
2
3
4
5
6
7
8
//main.cpp
#define UNICODE
#define _UNICODE
#include "main-header.h"
int main()
.
.
.

1
2
3
4
5
6
7
8
9
10
11
//main-header.h
#ifndef MAIN_HEADER_H
#define MAIN_HEADER_H

#include "header1.h"
#include "header1.h"
.
.
.

#endif 

1
2
3
4
5
6
7
//header1.h
#ifndef HEADER_1_H
#define HEADER_1_H

//typedeffing....

#endif 


The problem is that the unicode definition is not getting from main.cpp to source1.cpp (source file of header1.h), atleast not when I view it in the debugger.
I probably simply have a design flaw here.

I don't want to have to define UNICODE in any of the headers, because that is the purpose of my building a ANSI and UNICODE supported lib.
Last edited on
A macro just copies and pastes text. Each .cpp file plus its includes is compiled as a separate translation unit.
So I should really just create a typedef header that optionally defines Unicode and have all source files in the lib include it. And simply rebuild the lib based on what I need. I'm hoping someday Unicode will become the standard.
I don't know what you mean by typedef header (a header with commonly shared typedefs?) but yes, that how this is usually done.
Topic archived. No new replies allowed.