英文:
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:
-
Create a loop that includes 90 files.
-
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
-
Create a loop that includes 90 files
-
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 "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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论