Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Linux
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 4:22am
zzzteph360
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v4
Comments
Albert Holguin at 20-Jun-12 9:51am
   
I'm pretty sure you're right... +5
Richard MacCutchan at 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 at 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 at 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 at 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.
Richard MacCutchan at 20-Jun-12 16:03pm
   
Probably not; always best to run a proper test before commenting.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
v2
Comments
Albert Holguin at 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 at 20-Jun-12 15:48pm
   
Agreed; I just tried this and it will not build the DLL.
enhzflep at 20-Jun-12 19:28pm
   
Hall of Shame methinks!
enhzflep at 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 at 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)

  Print Answers RSS
0 OriginalGriff 165
1 Sergey Alexandrovich Kryukov 134
2 Sebastiaan Meijerink 131
3 Rana Waqas 110
4 Maciej Los 84
0 Sergey Alexandrovich Kryukov 6,558
1 OriginalGriff 6,288
2 Peter Leow 2,534
3 Abhinav S 2,358
4 Maciej Los 2,352


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100