Click here to Skip to main content
12,763,162 members (37,261 online)
Rate this:
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:
In both .cpp files i declared extern function
//Some headers
extern void hello_printer(int _T);
class KL
   int printer()

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

void hello_printer(int _T)
int main()
     //some code with calling class KL

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

Is it possible? Please,can someone help me to manage with this problem?
Posted 20-Jun-12 4:22am
Rate this: bad
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.

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.
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.
Richard MacCutchan 20-Jun-12 16:03pm
Probably not; always best to run a proper test before commenting.
Rate this: bad
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 :( So I'll just leave the contents of the files I used.

Part 1 - the library.
#include "mySharedLib.h"

#ifndef mySharedLib_h
 #define mySharedLib_h

extern int some_function(int t);

class KA
        void print()


Part 2 - The program
#include "../mySharedLib/mySharedLib.h"
#include <stdio.h>

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

int main()
    KA classInst;

you entered 10
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:27pm
What on earth was I thinking?
That's gotta be the dumbest thing I've said/done all month.
enhzflep 20-Jun-12 19:28pm
Hall of Shame methinks!
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2017
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