Click here to Skip to main content
Sign Up to vote bad
good
See more: C++CLinux
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
//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:
 
#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 20 Jun '12 - 3:22
zzzteph360


2 solutions

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]
  Permalink  
Comments
Albert Holguin - 20 Jun '12 - 9:51
I'm pretty sure you're right... +5
Richard MacCutchan - 20 Jun '12 - 10:01
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:00
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:27
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:35
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.
Richard MacCutchan - 20 Jun '12 - 16:03
Probably not; always best to run a proper test before commenting.
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 Frown | :( So I'll just leave the contents of the files I used.
 
Part 1 - the library.
libMain.cpp
#include "mySharedLib.h"
 
mySharedLib.h
#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
#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
  Permalink  
Comments
Albert Holguin - 20 Jun '12 - 13:10
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:48
Agreed; I just tried this and it will not build the DLL.
enhzflep - 20 Jun '12 - 19:28
Hall of Shame methinks!
enhzflep - 20 Jun '12 - 19:27
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:15
lol, happens... :)

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 586
1 Maciej Los 255
2 Slacker007 240
3 OriginalGriff 220
4 CPallini 210
0 Sergey Alexandrovich Kryukov 9,118
1 OriginalGriff 7,134
2 CPallini 3,803
3 Rohan Leuva 3,135
4 Maciej Los 2,558


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid