Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

c++ - How to use a macro in an #include directive?

I'm confused on how to use macros in the #include directive. I've done this:

#include "../../../../GlobalDefintions.h"
#include "../../../../lib/libc++/" ARCH_FAMILY_S  "/" ARCH_S  "/stkl/printkc/printkc.h"

GlobalDefintions.h:

#ifndef _GlobalDefintions_
#define _GlobalDefintions_

/*Architecture Information Start*/


#define ARCH i386
#define ARCH_FAMILY x86


#define ARCH_S "i386"
#define ARCH_FAMILY_S "x86"

/*Architecture Information End*/

#endif /*_GlobalDefintions_*/

But what that gives me is this:

kernel.c++:24:88: fatal error: ../../../../lib/libc++/: No such file or directory  

#include "../../../../lib/libc++/" ARCH_FAMILY_S  "/" ARCH_S  "/stkl/printkc/printkc.h"

Is there a way to successfully append ARCH_FAMILY_S and ARCH_S to my #include directive string?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use a series of macros to create your include file. Unfortunately, I can't think of any cleaner (in-source) way of doing this. This works for arm-eabi-none-gcc v5.4.1.

#define LIBC_DIR ../../../../lib/libc++/

#define STRINGIFY_MACRO(x) STR(x)
#define STR(x) #x
#define EXPAND(x) x
#define CONCAT(n1, n2) STRINGIFY_MACRO(EXPAND(n1)EXPAND(n2))
#define CONCAT5(n1, n2, n3, n4, n5) STRINGIFY_MACRO(EXPAND(n1)EXPAND(n2)EXPAND(n3)EXPAND(n4)EXPAND(n5))

// Concatenate the five elements of your path.
// Of course, this can be simplified if there is only a prefix and a suffix
// that needs to be added and the ARCH_FAMILY, /, and ARCH are always present
// in the macro-generated #include directives.
#include CONCAT5(LIBC_DIR,ARCH_FAMILY,/,ARCH,/stkl/printkc/printkc.h)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...