Templete Function called from shared object file(DLL)

H

Haro Panosyan

Suppose I have defined a templete function prototype in a header
file and the body in .cpp file and created executable without actually
calling the templete function.

Now let say from that executable I am loading a shared object and
executing(dlsym-ing) some f() function, which inside calls the templete
function.
(Of course the header file with templete prototype is included in
shared object source file, so it could be compiled.)

If I run the executable then I am getting an error:

"referenced symbol not found"

which is exactly what I am expecting, because there was no actual
call of the templete function in the executable, hence no such
function code was generated by a compiler.

My question is it possible to make this work?
To me it is not possible, but who knows, may be there are some compile
options specificly designed for this case, something like to tell
dynamic linker to create a function code during runtime.
Sounds to hard to be true :), or I am lost.

Thanks,
-haro
 
V

Victor Bazarov

Haro said:
Suppose I have defined a templete function prototype in a header
file and the body in .cpp file and created executable without actually
calling the templete function.

Then there is no function.
Now let say from that executable I am loading a shared object and
executing(dlsym-ing) some f() function, which inside calls the
templete function.

It can't call the template. I can only call a specialization of that
template.
(Of course the header file with templete prototype is included in
shared object source file, so it could be compiled.)

If I run the executable then I am getting an error:

"referenced symbol not found"

Of course. Since the body of the function wasn't available to the
compiler, it didn't know what that function looks like. So it decided
that the function will be provided externally, at the link time. It,
of course, wasn't provided, since it was never instantiated.
which is exactly what I am expecting, because there was no actual
call of the templete function in the executable, hence no such
function code was generated by a compiler.
Right.

My question is it possible to make this work?

You need to cause the compiler to instantiate that function. You can
use "explicit instantiation" (look it up).
To me it is not possible, but who knows, may be there are some compile
options specificly designed for this case, something like to tell
dynamic linker to create a function code during runtime.
Sounds to hard to be true :), or I am lost.

Get a good book on templates.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top