Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > #define a macro that #includes

Reply
Thread Tools

#define a macro that #includes

 
 
.rhavin grobert
Guest
Posts: n/a
 
      04-10-2008
first of all: sorry for perhaps posting something already answered
anywhere, i couldnt find any useful info - define, macro, include and
so on are very poor keywords

my problem:

i want to do something like...

INCLUDE_TYPES(#include);

....that resolves to ...

#include "file1.h"
#include "file2.h"
#include "file3.h"


.... and my try was a...

#define INCLUDE_TYPES(Q_L) \
Q_L "file1.h" \
Q_L "file2.h" \
Q_L "file3.h" \

....but my compiler complains about...

'#' : invalid character : possibly the result of a macro expansion

....so that way dont work. i cant do a simple...

#include "files.h"

....with all other includes in that file so im looking for some
different idea and would appreciate any useful help.

TIA, -.rhavin
 
Reply With Quote
 
 
 
 
jxh
Guest
Posts: n/a
 
      04-10-2008
On Apr 10, 10:41*am, ".rhavin grobert" <(E-Mail Removed)> wrote:
>
> my problem:
>
> i want to do something like...
>
> INCLUDE_TYPES(#include);
>
> ...that resolves to ...
>
> #include "file1.h"
> #include "file2.h"
> #include "file3.h"
>
> ... and my try was a...
>
> #define INCLUDE_TYPES(Q_L) \
> Q_L "file1.h" \
> Q_L "file2.h" \
> Q_L "file3.h" \
>
> ...but my compiler complains about...
>
> *'#' : invalid character : possibly the result of a macro
> expansion
>
> ...so that way dont work.


Right. You can't expand a preprocessor macro into a
preprocessor directive. You can use the presence of
a macro to determine whether or not to include your
header files by:

#ifdef INCLUDE_TYPES
# include "file1.h"
# include "file2.h"
# include "file3.h"
#endif

> i cant do a simple...
>
> #include "files.h"
>
> ...with all other includes in that file so im looking for some
> different idea and would appreciate any useful help.
>


You need to explain why you can't do that.

-- James
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-10-2008
In article <(E-Mail Removed)>,
..rhavin grobert <(E-Mail Removed)> wrote:

>my problem:


>i want to do something like...


>INCLUDE_TYPES(#include);


>...that resolves to ...


>#include "file1.h"
>#include "file2.h"
>#include "file3.h"


You cannot do that. No macro can expand to a preprocessor directive.
--
"Walter is a great man." -- Dennis Green
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      04-10-2008
On Thu, 10 Apr 2008 10:41:30 -0700, .rhavin grobert wrote:
> first of all: sorry for perhaps posting something already answered
> anywhere, i couldnt find any useful info - define, macro, include and so
> on are very poor keywords
>
> my problem:
>
> i want to do something like...
>
> INCLUDE_TYPES(#include);
>
> ...that resolves to ...
>
> #include "file1.h"
> #include "file2.h"
> #include "file3.h"


That's not possible in C. Macro expansions cannot ever generate
preprocessing directives.

> ... and my try was a...
>
> #define INCLUDE_TYPES(Q_L) \
> Q_L "file1.h" \
> Q_L "file2.h" \
> Q_L "file3.h" \
>
> ...but my compiler complains about...
>
> '#' : invalid character : possibly the result of a macro expansion


Right. Since #include doesn't appear at the start of a line in your code,
it's not a directive, and the # is left and causes syntax errors.

> ...so that way dont work. i cant do a simple...
>
> #include "files.h"
>
> ...with all other includes in that file so im looking for some different
> idea and would appreciate any useful help.


Why can't you do that? If you have a problem you want to solve, ask about
that problem. Your attempted solution doesn't work and can't be made to
work. Asking how to change that solution so that it will work won't get
you useful answers. Asking how to solve your original problem may.
 
Reply With Quote
 
.rhavin grobert
Guest
Posts: n/a
 
      04-10-2008
On 10 Apr., 20:15, jxh <(E-Mail Removed)> wrote:
> On Apr 10, 10:41 am, ".rhavin grobert" <(E-Mail Removed)> wrote:
> > my problem:
> > i want to do something like...
> > INCLUDE_TYPES(#include);
> > ...that resolves to ...
> > #include "file1.h"
> > #include "file2.h"
> > #include "file3.h"
> > ... and my try was a...
> > #define INCLUDE_TYPES(Q_L) \
> > Q_L "file1.h" \
> > Q_L "file2.h" \
> > Q_L "file3.h" \
> > ...but my compiler complains about...
> > '#' : invalid character : possibly the result of a macro
> > expansion
> > ...so that way dont work.

> Right. You can't expand a preprocessor macro into a
> preprocessor directive. You can use the presence of
> a macro to determine whether or not to include your
> header files by:
> #ifdef INCLUDE_TYPES
> # include "file1.h"
> # include "file2.h"
> # include "file3.h"
> #endif
> > i cant do a simple...
> > #include "files.h"
> > ...with all other includes in that file so im looking for some
> > different idea and would appreciate any useful help.

> You need to explain why you can't do that.


i have a pack that consists of 3 different classes meant for
deriviation by the client (yes, thats c++)

they client may specify differnt additional classes and they shall be
included, so i created a file that is a headerfile and consists of
certain macros allowing the client to modify the package. and wantet
to find a solution to have it all in one configuration file, one part
of the config file looks for example like this:
____________________________________________


//-----------------------------------------------------------------------------
// list types' corresponding info-classes and file-classes
// usage: Q_T([FILETYPE]) Q_I([Header-Class]) Q_F([File-Class]) Q_B
// escape EOLs (lineends) to have a single #define-line
#define QFILEMANAGER_TYPECLASSES(Q_T, Q_I, Q_F, Q_B) \
Q_T(FZT_RECORD) Q_I(CInfRec) Q_F(CFZRecord) Q_B \
Q_T(FZT_POOL) Q_I(CInfPool) Q_F(CFZPool) Q_B \
Q_T(FZT_DEFN) Q_I(CInfDef) Q_F(CFZDef) Q_B \
Q_T(FZT_BASE) Q_I(CInfBase) Q_F(CFZBase) Q_B \
Q_T(FZT_LOCAL) Q_I(CInfLocal) Q_F(CFZLocal) Q_B \
Q_T(FZT_STUDY) Q_I(CInfStudy) Q_F(CFZStudy) Q_B \

____________________________________________

in my pack i have the following definitions

#define QFM_NOP(s)
#define QFM_EMPTY
#define QFM_CASE(s) case s:
#define QFM_NEW(s) new s; break;
#define QFM_DELETE(s) delete (s*)
#define QFM_BREAK ;break;

....by now 'calling' the above macro with the following code in my
class...

switch (type) {
QFILEMANAGER_TYPECLASSES(QFM_CASE, QFM_NOP, pFile = QFM_NEW,
QFM_EMPTY);
default:
//
}

....i get my allocator and by calling it that way ...

switch (pInfo->TypeGet()) {
QFILEMANAGER_TYPECLASSES(QFM_CASE, QFM_DELETE, QFM_NOP, pInfo
QFM_BREAK);
default:
return false;
}

....i delocate (correct word?) it. But that definition file is included
in many files in the pack where i dont want to have the #include-
sections, so im looking for a way to have a....

//-------------------------------------
// put yout file pathes here
MYMACRO(somewhat) \
"filepath" \
"filepath" \

....and decide in every file where i include the user-config-header if
i want to also include his specified files. the...

#ifdef INCLUDE_TYPES
# include "file1.h"
# include "file2.h"
# include "file3.h"
#endif

....seems to be a possibility, so'll try that, thx, .rhavin
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-10-2008
".rhavin grobert" <(E-Mail Removed)> writes:

> first of all: sorry for perhaps posting something already answered
> anywhere, i couldnt find any useful info - define, macro, include and
> so on are very poor keywords
>
> my problem:
>
> i want to do something like...
>
> INCLUDE_TYPES(#include);
>
> ...that resolves to ...
>
> #include "file1.h"
> #include "file2.h"
> #include "file3.h"
>
>
> ... and my try was a...
>
> #define INCLUDE_TYPES(Q_L) \
> Q_L "file1.h" \
> Q_L "file2.h" \
> Q_L "file3.h" \
>
> ...but my compiler complains about...
>
> '#' : invalid character : possibly the result of a macro expansion


The result of a macro expansion can't be a pre-processing directive --
no matter how much it looks like one. No macro can generate a
#include, #define etc. You can generate those tokens, but they are
not treated as directives.

> ...so that way dont work. i cant do a simple...
>
> #include "files.h"
>
> ...with all other includes in that file so im looking for some
> different idea and would appreciate any useful help.


Backup a bit. What problem were you trying to solve?

--
Ben.
 
Reply With Quote
 
jxh
Guest
Posts: n/a
 
      04-10-2008
On Apr 10, 11:41*am, ".rhavin grobert" <(E-Mail Removed)> wrote:
> On 10 Apr., 20:15, jxh <(E-Mail Removed)> wrote:
> > On Apr 10, 10:41 am, ".rhavin grobert" <(E-Mail Removed)> wrote:
> > > i cant do a simple...
> > > #include "files.h"
> > > ...with all other includes in that file so im looking for some
> > > different idea and would appreciate any useful help.

> > You need to explain why you can't do that.

>
> i have a pack that consists of 3 different classes meant for
> deriviation by the client (yes, thats c++)
>
> they client may specify differnt additional classes and they shall be
> included, so i created a file that is a headerfile and consists of
> certain macros allowing the client to modify the package.

[...]

It doesn't seem to me you are leveraging the full capabilities of C++.
If you describe your general architecture problem of accessing user
defined helper classes from your library's base class, I am sure you
would get good answers from comp.lang.c++.

-- James
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      04-11-2008
(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) wrote:
> .rhavin grobert <(E-Mail Removed)> wrote:
> >my problem:
> >i want to do something like...
> >INCLUDE_TYPES(#include);
> >...that resolves to ...
> >#include "file1.h"
> >#include "file2.h"
> >#include "file3.h"

>
> No macro can expand to a preprocessor directive.


True, but preprocessing directives, including #include,
can expand to macros.

It's possible to do something like what the OP wants
(upto a static limit), but it's questionable as to
whether it's worth doing. Since the OP has admitted
to using C++, it's more likely there are better C++
paradigms to achieve what they really need to do.

But there is a Boost header that does many surprising
pre-processor tricks, and is both C and C++ compatible.

--
Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments