C ++,如何动态地包含.h [英] C++, How include .h dynamically
问题描述
例如:
//
//文件:Main.cpp
//
#include< stdlib.h>
int main()
{
printf(TEST_DEFINE);
return(EXIT_SUCCESS);
}
标题:
//
//文件:test_define.h
//
#ifndef TEST_DEFINE_H_INCLUDED
#define TEST_DEFINE_H_INCLUDED
#define TEST_DEFINE
#endif
我希望包含该标题,编译器或其他程序。我不想在CPP文件中包含此标题。
我如何处理?我愿意接受所有提案。
这种情况非常棘手,但对我的POC计划来说很必要。
>解决方案 GCC有一个选项 -include
file ,它使预处理器包含标题,就好像第一个你的程序的一行说 #includefile
请参阅 manual
例如给定一个固定版本的主(它应该 #include
):
#include
#include< stdio.h>
int main()
{
printf(TEST_DEFINE);
return(EXIT_SUCCESS);
}
和您的标题:
//
//文件:test_define.h
//
#ifndef TEST_DEFINE_H_INCLUDED
#define TEST_DEFINE_H_INCLUDED
#define TEST_DEFINEhello\\\
#endif
然后我可以像这样使用它:
$ g ++ -include test_define.h main.cpp -o main
$ ./main
hello
sorry for my bad english.
For example :
//
// File : Main.cpp
//
#include <stdlib.h>
int main()
{
printf(TEST_DEFINE);
return (EXIT_SUCCESS);
}
Header :
//
// File : test_define.h
//
#ifndef TEST_DEFINE_H_INCLUDED
#define TEST_DEFINE_H_INCLUDED
#define TEST_DEFINE
#endif
I want include that header, by the compiler or other program. I not want include this header in CPP file.
How can i process ? I'm open to all proposal.
This situation is quite tricky, but necesary for my POC program.
GCC has an option -include
file which causes the preprocessor to include the header as though the first line of your program said #include "file"
See the manual
e.g. given a fixed version of your main (which should #include <stdio.h>
):
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf(TEST_DEFINE);
return (EXIT_SUCCESS);
}
and your header:
//
// File : test_define.h
//
#ifndef TEST_DEFINE_H_INCLUDED
#define TEST_DEFINE_H_INCLUDED
#define TEST_DEFINE "hello\n"
#endif
Then I can use it like this:
$ g++ -include test_define.h main.cpp -o main
$ ./main
hello
这篇关于C ++,如何动态地包含.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!