包含路径的字符串连接 [英] String concatenation for include path

查看:116
本文介绍了包含路径的字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法连接2个字符串文字形成包含路径?



代码存根:

  #define INCLUDE_DIR/ include
#include INCLUDE_DIR/dummy.h

查看此问题,答案指向不同的方向(编译器命令行)。有人提到这里似乎不可能,但我想知道这个话题是否已经挖得足够。



(我有一个使用案例,这是相关的,请只集中你的回答/评论这个问题。)

解决方案

这似乎是不可能的。我会在这里报告相关部分,从 Eric Postpischil的回答(他没有


编译器将在 #include line(per C
2011 [N1570] 6.10.2 4),但是语义没有完全定义,
不能用于连接文件路径组件,而不需要额外的
帮助C实现。所有这些允许你
do是一些简单的替换,提供一个完整的路径,例如:

  define MyPath../../path/to/my/file.h
#include MyPath


< blockquote>

链接到文档。特别是这一部分对于便携式解决方案没有什么希望:



a之间使用一系列预处理令牌的方法< > 预处理令牌对或一对字符
组合到单个标题名称预处理令牌是
实现定义。







为了完整起见,您可以使用 http://stackoverflow.com/a/ 27830271/2436175 。我会调查,当我有一会儿...


Is there a way to concatenate 2 strings literals to form an include path?

Code stub:

#define INCLUDE_DIR "/include"
#include INCLUDE_DIR "/dummy.h"

Looking at this question, the answers point in a different direction (compiler command line). It is mentioned here that it is seemingly not possible, but I wonder if the topic has been dug enough.

(I do have an use case in which this is relevant, please focus your answers/comments on this question only.)

解决方案

It really seems this is not possible. I will report here the relevant section from Eric Postpischil's answer (he doesn't seem to be active anymore).

The compiler will do macro replacement on an #include line (per C 2011 [N1570] 6.10.2 4), but the semantics are not fully defined and cannot be used to concatenate file path components without additional assistance from the C implementation. So about all this allows you to do is some simple substitution that provides a complete path, such as:

#define MyPath "../../path/to/my/file.h"
#include MyPath

Link to documentation. In particular this section doesn't leave much hope for portable solutions:

The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.


For completeness, maybe something can be tried using http://stackoverflow.com/a/27830271/2436175. I'll investigate that when I have a moment...

这篇关于包含路径的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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