Click here to Skip to main content
15,892,290 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone.

I've got some problem using extern function in shared library.I want to use some extern functions in shared library, which will be declared in main program code.
I've got library file's with some code, let them be:
klib.h
klib.cpp
kklib.cpp
In both .cpp files i declared extern function
C++
//Some headers
extern void hello_printer(int _T);
....
class KL
{
   public:
   int printer()
   {
      hello_printer(10);
      //someaction;
   }
};

After compiling I've got klib.so, put it in path folder.
Also I've got some main.cpp file with:

C++
#include<stdio.h>
void hello_printer(int _T)
{
     for(;_T>0;_T--)printf("%i",_T);
}
int main()
{
     //some code with calling class KL
}

I't also compiled with (linking with klib.so) no error's, but when I run it I've got:
path_to_library/klib.so:undefined symbol: hello_printer()__FiPc


Is it possible? Please,can someone help me to manage with this problem?
Posted

I may be wrong, but I don't think you can call back into an executable from a shared library in this way. Your external function should be in the shared library and the main app makes calls out to it.

[edit]
Looks like I was wrong; see enhzflep's entry.
Correction: this does not work in Windows (and probably not in Linux), as the library will not build unless all external references are satisfied.
[/edit]
 
Share this answer
 
v4
Comments
Albert Holguin 20-Jun-12 9:51am    
I'm pretty sure you're right... +5
Richard MacCutchan 20-Jun-12 10:01am    
Thanks, I'm working from memory as I no longer have access to a Linux system to check it out. A project for the winter months I think.
zzzteph 20-Jun-12 11:00am    
Ok, and what should happens If main.cpp and kklib.cpp both have similar functions hello_printer: if call from main it call's main's hello_printer, if call kklib - kklib's printer? right?
Richard MacCutchan 20-Jun-12 11:27am    
Yes, but why would you want two functions with the same name? You need to look more closely at the design of your system and what problem you are trying to solve.
Albert Holguin 20-Jun-12 14:35pm    
Are you sure you were wrong? See my comment to him... almost looks like he just compiled the library with the program by including the header like that.
Yes, you can do that - its perfectly functional as both a static (.a) and a dynamic lib (.dll) in Windows. It's also just fine as a shared lib (.so) under Linux. (didn't bother to try a static lib in linux) - all code built with Code::Blocks & GCC

I'm pretty tired now, so I'm not really so good at summarising :( So I'll just leave the contents of the files I used.

Part 1 - the library.
libMain.cpp
C++
#include "mySharedLib.h"


mySharedLib.h
C++
#ifndef mySharedLib_h
 #define mySharedLib_h

extern int some_function(int t);

class KA
{
    public:
        void print()
        {
            some_function(10);
        }
};

#endif


Part 2 - The program
main.cpp
C++
#include "../mySharedLib/mySharedLib.h"
#include <stdio.h>

int some_function(int t)
{
    printf("You entered %d\n", t);
    return t;
}

int main()
{
    KA classInst;
    
    classInst.print();
}


Output:
you entered 10
 
Share this answer
 
v2
Comments
Albert Holguin 20-Jun-12 13:10pm    
By including the header file that has the class definition you're compiling the code within the program, so it's not even referencing a binary library.
Richard MacCutchan 20-Jun-12 15:48pm    
Agreed; I just tried this and it will not build the DLL.
enhzflep 20-Jun-12 19:28pm    
Hall of Shame methinks!
enhzflep 20-Jun-12 19:27pm    
What on earth was I thinking?
That's gotta be the dumbest thing I've said/done all month.
Albert Holguin 21-Jun-12 9:15am    
lol, happens... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900