0

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?

5
  • 16
    No, you can't. #include is a compile time directive (more precise, pre-compile time). v.c_str() is a runtime value. You can achieve something similar by loading shared libraries in runtime. Commented May 17, 2021 at 5:41
  • 3
    Why do you want to do that? any specific requirement? Commented May 17, 2021 at 6:22
  • 1
    Please, remember that Python is an interpreted language. Hence, such a construct is possible. C++ is a compiled language where compile time and runtime are distinct. Commented May 17, 2021 at 6:22
  • @Sourabh because i have a directory saves a lot of header(they are come from other people, and i need to include them all to load function) Commented May 19, 2021 at 5:25
  • @user15924460 then how does that make you think you need to include source code files at run time? Commented May 23, 2021 at 2:59

2 Answers 2

2

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 compilation process source of image

Sign up to request clarification or add additional context in comments.

3 Comments

"import works on precompile time" what do you mean by that? precompile is cpython optimization
Did you make this image? If not, could you please provide the source? I guess it's from an article and I want to read it.
@VolodymyrBoiko I haven't found any official information about exact moment, when modules works(but it's unambiguously before compilation), so in this case I shared info from another site(link under image)
0

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 `"$_`""

}

Output will be enter image description here

Now just include this master.h in your source file.

I hope, it will help you.

Thanks :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.