Variable in include file c++

huangapple go评论69阅读模式
英文:

Variable in include file c++

问题

I am trying to include files in a cpp, but the filenames should be based on a variable.

In my case, the filenames should be filename_X.cpp, where X ranges from 0 to 90, and filename is a predefined text. There are a total of 90 include statements. I would like to know if it is possible to:

  1. Create a loop that includes 90 files.

  2. Include using a variable filename.

From searching the internet, I have an approach to point 2) as below:

#define FILENAME some_file_name

#define QUOTE(x) _QUOTE(x)
#define _QUOTE(x) #x

#define _CONCAT(x, y) x##_##y.cpp
#define CONCAT(x, y) _CONCAT(x, y)

#define _INCLUDE_FILE(x, y) (QUOTE(CONCAT(x, y)))
#define INCLUDE_FILE(x, y) _INCLUDE_FILE(x, y)

//#include INCLUDE_FILE(FILENAME, 0)

which works fine if I am calling a print function:

printf(INCLUDE_FILE(FILENAME, 70));

It will print some_file_name_70.cpp.

However, when I try to use it in an include statement #include INCLUDE_FILE(FILENAME, 0), an error shows up as:

pred.cpp:22:10: error: expected "FILENAME" or <FILENAME>
#include INCLUDE_FILE(FILENAME, 0)
         ^
pred.cpp:20:28: note: expanded from macro 'INCLUDE_FILE'
#define INCLUDE_FILE(x, y) _INCLUDE_FILE(x, y)
                           ^
pred.cpp:19:29: note: expanded from macro '_INCLUDE_FILE'
#define _INCLUDE_FILE(x, y) (QUOTE(CONCAT(x, y)))
                            ^
1 error generated.

Does anyone know why the error happens? Thank you very much!

英文:

I am trying to include files in a cpp, but the filenames should be based on a variable.

In my case, the filenames should be filename_X.cpp where X ranges from 0 to 90 and filename is a predefined text, i.e. there is in total 90 includes statement. I would like to know if it is possible to

  1. Create a loop that includes 90 files

  2. Include using a variable filename

From searching through the internet, I have an approach to the point 2) as below:

#define FILENAME some_file_name

#define QUOTE(x) _QUOTE(x)
#define _QUOTE(x) #x

#define _CONCAT(x, y) x##_##y.cpp
#define CONCAT(x, y) _CONCAT(x, y)

#define _INCLUDE_FILE(x, y) (QUOTE(CONCAT(x, y)))
#define INCLUDE_FILE(x, y) _INCLUDE_FILE(x, y)

//#include INCLUDE_FILE(FILENAME, 0)

which works fine if I am calling a print function

printf(INCLUDE_FILE(FILENAME, 70));

It will print some_file_name_70.cpp .

However, when I try to use it in an include statement #include INCLUDE_FILE(FILENAME, 0), an error shows up as:

pred.cpp:22:10: error: expected &quot;FILENAME&quot; or &lt;FILENAME&gt;
#include INCLUDE_FILE(FILENAME, 0)
         ^
pred.cpp:20:28: note: expanded from macro &#39;INCLUDE_FILE&#39;
#define INCLUDE_FILE(x, y) _INCLUDE_FILE(x, y)
                           ^
pred.cpp:19:29: note: expanded from macro &#39;_INCLUDE_FILE&#39;
#define _INCLUDE_FILE(x, y) (QUOTE(CONCAT(x, y)))
                            ^
1 error generated.

Does anyone know why does the error happen? Thank you very much!

答案1

得分: 3

这是翻译后的代码部分:

这在`_INCLUDE_FILE`宏中移除括号后对我有效:

#define FILENAME t

#define QUOTE(x) _QUOTE(x)
#define _QUOTE(x) #x

#define _CONCAT(x, y) x##y.h
#define CONCAT(x, y) _CONCAT(x, y)

#define _INCLUDE_FILE(x, y) QUOTE(CONCAT(x, y))
#define INCLUDE_FILE(x, y) _INCLUDE_FILE(x, y)

#include INCLUDE_FILE(FILENAME,1)

int main () {
}

宏中的括号要小心处理...

请注意,不应该包括.cpp文件。不建议这样做,只包括.h文件。除非您确切了解自己正在做的奇怪事情。

英文:

That works for me, if I remove the parenthesis in the _INCLUDE_FILE macro:

#define FILENAME t

#define QUOTE(x) _QUOTE(x)
#define _QUOTE(x) #x

#define _CONCAT(x, y) x##y.h
#define CONCAT(x, y) _CONCAT(x, y)

#define _INCLUDE_FILE(x, y) QUOTE(CONCAT(x, y))
#define INCLUDE_FILE(x, y) _INCLUDE_FILE(x, y)

#include INCLUDE_FILE(FILENAME,1)

int main (){
}

Always take care of parenthesis in macros...

Note that you should not include .cpp files. That is not recommended, include only .h files. Except if you really know the weird thing your are doing.

huangapple
  • 本文由 发表于 2023年5月17日 22:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定