包含include语句的宏 [英] macro containing include statement

查看:75
本文介绍了包含include语句的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好,


我想构建一个宏,它有条件包括

另一个源文件。即这样的事情:

#MPIinclude(FileName)(#include(FileName))/ *另类一个 -

实际上包括MPI源* /


#MPIinclude(FileName)/ *备选方案二 -

a noop * /


这样代码的主体可以包含:


MPIinclude(" path / to / mpi_code / mpi_file.c")

但是,当前处理宏定义时,我得到

以下错误信息(来自gnu cpp):


fphs.c:20:32:''#''后面没有宏参数


,结果代码稍后会在最终编译中失败。


这可能吗?


Joakim

-

Joakim Hove

hove AT ift uib no

+47(55 5)8 27 90
http://www.ift.uib.no/~hove /

解决方案

Joakim Hove写道:

你好,

我想构建一个宏,它有条件包括另一个源文件。即这样的事情:

#MPIinclude(FileName)(#include(FileName))/ *替代的一个 -
实际上包括MPI源* /

#MPIinclude (FileName)/ *备选方案二 -
一个noop * /

这样代码的主体可以包含:

MPIinclude(" path / to / mpi_code / mpi_file.c")

然而,当上面的宏定义预处理时,我得到了以下错误消息(来自gnu cpp):

fphs.c :20:32:'''''后面没有一个宏参数

后来生成的代码在最终的编译中失败了。

这可能吗?

Joakim




尝试这样的事情:


#ifdef USE_MPI_SOURCE

#define MPI(文件)"文件"

#else

#define MPI(文件)

#endif


#include MPI(路径/到/ mpi_code / mpi_file.c)


马克

-

<<删除电子邮件的del>>


C预处理器非常简单。

不幸的是,#define(又名宏)不能包含预处理指令

,如#define,#include,#error,#pragma ......


看看那里:
http://www.lns.cornell.edu/public/CO...p/cpp_toc.html


" Joakim Hove" <豪** @ ift.uib.no>在留言中写道

news:bj ************* @ aquifer1.fi.uib.no ...


您好,

我想构建一个宏,它有条件包括另一个源文件。即这样的事情:

#MPIinclude(FileName)(#include(FileName))/ *另类一个 -
实际上包括MPI
来源* /
#MPIinclude (FileName)/ *备选方案二 -
一个noop * /

这样代码的主体可以包含:

MPIinclude(" path / to / mpi_code / mpi_file.c")

然而,当上面的宏定义预处理时,我得到了以下错误消息(来自gnu cpp):

fphs.c :20:32:'''''后面没有一个宏参数

后来生成的代码在最终的编译中失败了。

这可能吗?

Joakim

-
Joakim Hove
hove AT ift uib no
+47(55 5)8 27 90
< a rel =nofollowhref =http://www.ift.uib.no/~hove/target =_ blank> http://www.ift.uib.no/~hove/




Capstar< ne ** @ deleg.homeip.net>写道:


您好,


感谢您的回答。但不幸的是它不起作用(至少

不用gnu cpp):

尝试这样的事情:

#ifdef USE_MPI_SOURCE
#define MPI(文件)"文件"
#else
#define MPI(文件)
#endif


[ ...]

#include MPI(path / to / mpi_code / mpi_file.c)




当定义变量USE_MPI_SOURCE时,我得到:


bash%cpp src_file.c

src_file.c:405:35:file:没有这样的文件或目录


而当它没有定义时我得到:


bash%cpp src_file.c

src_file.c:405:35:#include expect " FILENAME"或者< FILENAME>


这看起来很简单,但它确实包含嵌套的

预处理程序指令,这可能是非法的?


问候


Joakim


-

Joakim Hove

hove AT ift uib no

+47(55 5)8 27 90
http://www.ift.uib.no/~hove/



Hello,

I would like to construct a macro, which does conditional include of
another source file. i.e. something like this:
#MPIinclude(FileName) (#include (FileName)) /* Alternative one -
actually includes MPI source */

#MPIinclude(FileName) /* Alternative two -
a noop */

So that the body of the code can contain:

MPIinclude("path/to/mpi_code/mpi_file.c")

However, when preprosessing the macro definition above I get the
following error message (from gnu cpp):

fphs.c:20:32: ''#'' is not followed by a macro parameter

and the resulting code later fails in the eventual compilation.

Is this possible?

Joakim
--
Joakim Hove
hove AT ift uib no
+47 (55 5)8 27 90
http://www.ift.uib.no/~hove/

解决方案

Joakim Hove wrote:

Hello,

I would like to construct a macro, which does conditional include of
another source file. i.e. something like this:
#MPIinclude(FileName) (#include (FileName)) /* Alternative one -
actually includes MPI source */

#MPIinclude(FileName) /* Alternative two -
a noop */

So that the body of the code can contain:

MPIinclude("path/to/mpi_code/mpi_file.c")

However, when preprosessing the macro definition above I get the
following error message (from gnu cpp):

fphs.c:20:32: ''#'' is not followed by a macro parameter

and the resulting code later fails in the eventual compilation.

Is this possible?

Joakim



Try something like this:

#ifdef USE_MPI_SOURCE
#define MPI(file) "file"
#else
#define MPI(file)
#endif

#include MPI(path/to/mpi_code/mpi_file.c)

Mark
--
<<Remove the del for email>>


The C preprocessor is really simple.
Unfortunately, a #define (aka macro) can''t contain preprocessing directive
like #define, #include, #error, #pragma...

Have a look there:
http://www.lns.cornell.edu/public/CO...p/cpp_toc.html

"Joakim Hove" <ho**@ift.uib.no> wrote in message
news:bj*************@aquifer1.fi.uib.no...


Hello,

I would like to construct a macro, which does conditional include of
another source file. i.e. something like this:
#MPIinclude(FileName) (#include (FileName)) /* Alternative one -
actually includes MPI source */
#MPIinclude(FileName) /* Alternative two -
a noop */

So that the body of the code can contain:

MPIinclude("path/to/mpi_code/mpi_file.c")

However, when preprosessing the macro definition above I get the
following error message (from gnu cpp):

fphs.c:20:32: ''#'' is not followed by a macro parameter

and the resulting code later fails in the eventual compilation.

Is this possible?

Joakim
--
Joakim Hove
hove AT ift uib no
+47 (55 5)8 27 90
http://www.ift.uib.no/~hove/




Capstar <ne**@deleg.homeip.net> writes:

Hello,

thanks for answering. However unfortunately it does not work (at least
not with gnu cpp):

Try something like this:

#ifdef USE_MPI_SOURCE
#define MPI(file) "file"
#else
#define MPI(file)
#endif
[...]
#include MPI(path/to/mpi_code/mpi_file.c)



When the variable USE_MPI_SOURCE is defined I get:

bash% cpp src_file.c
src_file.c:405:35: file: No such file or directory

whereas when it is not defined I get:

bash% cpp src_file.c
src_file.c:405:35: #include expects "FILENAME" or <FILENAME>

It seemed such a simple construction, but it does contain nested
preprocessor directives, and that is maybe illegal?

Regards

Joakim

--
Joakim Hove
hove AT ift uib no
+47 (55 5)8 27 90
http://www.ift.uib.no/~hove/


这篇关于包含include语句的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆