English 中文(简体)
Create a shared lib using another shared lib
原标题:

I have a shared library "libwiston.so". I am using this to create another shared library called "libAnimation.so", which will be used by another project. Now, the second library "libAnimation.so" can t be used in test code correctly. So I doubt that the creation of the second lib "libAnimation.so" is right. The gcc command to create this lib is

 g++ -g -shared -Wl,-soname,libwiston.so -o libAnimation.so $(objs) -lc". 

Has someone come across this problem?

问题回答

That looks like a weird link line - you are creating libAnimation.so, but its internal DT_SONAME name is libwiston.so.

I don t think that what you wanted to do. Don t you want to link libAnimation.so against libwiston.so (-lwiston)?

g++ -g -shared -o libAnimation.so $(objs) -lc -lwiston

I think it would be easier to wrap your build in automake/autoconf and rely on libtool to get the shared library creation correct.

I ll do a humble review on the process of creating shared libraries.

Let s begin by creating libwiston.so. First we implement the function we would like to export and then define it on a header so other programs knows how to call it.

/* file libwiston.cpp
 * Implementation of hello_wiston(), called by libAnimation.so
 */
#include "libwiston.h"

#include <iostream>

int hello_wiston(std::string& msg)
{
    std::cout << msg << std::endl;

    return 0;
}

and:

/* file libwiston.h
 * Exports hello_wiston() as a C symbol.
 */
#include <string>

extern "C" {
int hello_wiston(std::string& msg);
};

This code can be compiled with: g++ libwiston.cpp -o libwiston.so -shared

Now we implement the second shared library, named libAnimation.so that calls the function exported by the first library.

/* file libAnimation.cpp
 * Implementation of call_wiston(). 
 * This function is a simple wrapper around hello_wiston().
 */
#include "libAnimation.h"
#include "libwiston.h"

#include <iostream>

int call_wiston(std::string& param)
{
    hello_wiston(param);

    return 0;
}

and header:

/* file libAnimation.h
 * Exports call_wiston() as a C symbol.
 */
#include <string>

extern "C" {
int call_wiston(std::string& param);
};

Compile it with: g++ libAnimation.cpp -o libAnimation.so -shared -L. -lwiston

Finally, we create a small application to test libAnimation.

/* file demo.cpp
 * Implementation of the test application.
 */
#include "libAnimation.h"
int main()
{
    std::string msg = "hello stackoverflow!";
    call_wiston(msg);
}

And compile it with: g++ demo.cpp -o demo -L. -lAnimation

There s an interesting tool named nm that you can use to list the symbols exported by your shared library. Using these examples, you could execute the following commands to check for the symbols:

nm libAnimation.so | grep call_wiston

outputs:

00000634 t _GLOBAL__I_call_wiston
000005dc T call_wiston

and also:

nm libwiston.so | grep hello_wiston

outputs:

0000076c t _GLOBAL__I_hello_wiston
000006fc T hello_wiston




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签