Reputation: 19
i think in python:
i can use:
def include_something(s):
exec('import %s'%(s))
to import package dynamically
but in c++:
void include_something(const std::string& v) {
# include v.c_str();
}
seems doestnt work.
So, is there any methods can do this? and will the futures c++ support this kind of function?
Upvotes: 0
Views: 439
Reputation: 190
There is no way in C/C++ to achieve dynamic header loading.
BUT
I'll do as the following:
Create a simple PowerShell script to create a master header file, that will contain all the headers from a given directory.
$source_dir = "C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\um\alljoyn_c\"
$master_headerfile = 'master.h'
Remove-Item $master_headerfile
New-Item $master_headerfile -ItemType File -Value "// Master header file`n"
Get-ChildItem $source_dir -Filter *.h |
Foreach-Object {
Add-Content $master_headerfile "#include `"$_`""
}
Now just include this master.h
in your source file.
I hope, it will help you.
Thanks :)
Upvotes: 0
Reputation: 1016
Simple answer: No, You can't
Answer with details:
You can't do this with either include
or import
. As You can see on the picture below, include
works on preprocess time, and import
works on precompile time. While c_str
is running at compile time (if it is constexpr
), or at run time (if casual class or function), therefore, in any case, the import(or include) will be completed before the program learns the name of the module
source of image
Upvotes: 2