Function defined in shared object and also in the EXE
  Home FAQ Contact Sign in
comp.os.linux.misc only
 
Advanced search
POPULAR GROUPS

more...

 Up
Function defined in shared object and also in the EXE         

Group: comp.os.linux.misc · Group Profile
Author: ambarish.mitra
Date: Jan 16, 2008 00:37

Hi,
I have a shared library, which defines a function, say, fn2(). The
same function is also defined in the EXE that loads the shared
library.

In that case, when I execute the exe, the exe symbol is called. Is
there any way (any linker flag or runtime environment) to make the
symbol in the shared library to be called?

To demo the above, I have written very small programs. I am on g++
3.4.5 and Redhat Linux, kernel 2.6.9-34.EL

///////////////////////////////////////
DEMONSTRATION ////////////////////////////////////////////

/////////////////// File: libtest.h (//
remark: the test library include file)
#ifndef _LIBTEST_H
#define _LIBTEST_H

#include
using namespace std;

void fn1();
void fn2();
#endif

/////////////////// File: libtest.cpp (//
remark: the test library implementation)
#include "libtest.h"
void fn1(){
cout << "libtest - fn1()\n";
}

void fn2(){
cout << "libtest - fn2()\n";
}

/////////////////// File: main.cpp (// The main
program which links with the test library)
#include "libtest.h"
#include
using namespace std;

void fn2(); //this call is present
in the library as well as in the exe

int main() {
fn1(); // the library call
fn2(); // Which does it resolve? This exists in the main and
also in the lib

return 0;
}

void fn2(){
cout << "main - fn2()\n";
}

Now, build the shared library:

g++ -c -fPIC -Wall -o libtest.o libtest.cpp
g++ -Wl,-Bsymbolic -shared -o libtest.so libtest.o

Now, build the EXE (in the same directory):
g++ -c main.cpp
g++ -L. -ltest -o main main.o

Set the LD_LIBRARY_PATH
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

Now, execute the main: ./main

/// The following is the output ------

libtest - fn1()
main - fn2()

So, you see the fn2() function from the main is executed. I want the
fn2 function from the libtest.so to be executed.

Is this possible in any way?
no comments
diggit! del.icio.us! reddit!

RELATED THREADS
SubjectArticles qty Group
Hypergeometric functions and beta functionssci.math ·