Hi,

I am trying to prepare a shared library using another shared library.
but the second shared library does not contian the definations of funtions which are defined in first shared library.

lets say i have libfirst.so. in this there are no undefined functions.
while creating libsecond.so, i am linking the libfirst.so using -lfirst, there is no probelm in creating libsecond.so.

but when try to compile some applictions which use libsecond.so i am getting undefined reference to some functions in libsecond.so which are actually part of libfirst.so.

please help me , am i missing some thing or is there any other procedure i need to follow in creating libsecond.so

Recommended Answers

All 5 Replies

are you linking with both libraries?

What about header files? They declare the functions that are in the shared libraries. If this is linux (which I assume since you say you are using .so files), then you need the developer package for the library, which will install the headers for you. Then, as AD asked, are you linking with both? FWIW, you can extract the object files from the first .so and link them into the second one so you don't need to link both to an executable. Actually, I think what you did is ok, but when creating libsecond.so you may want to re-link libfirst.so to it because of possible recursive dependencies. This has bitten me in the nether regions all too frequently in the past! :-)

Actually I forgot to mention that i am mixing c and c++ files.
so in the make i am using the commands like

CC=gcc
CXX=g++
EXECUTABLE  := $(patsubst %.c,%,$(wildcard *Main.c))

executable: SomeMain.o
$(CXX) $(CFLAGS) SomeMain.o $(LDFLAGS) -o $(EXECUTABLE)

%.o: %.c
    $(CXX) -c $<  $(CFLAGS) -o $(LDFLAGS) @

Adding all ibraries required to LDFLAGS and also paths of those libraries.
I am using CXX for compiling .c files because some of the .so are in C++ , which are required by my .c files

NOTE:

some times if i compile with CC and link with CXX libraries are generating successfully, but when other applications trying to use those .so gain getting undefined symbols even though it is defined in .so

please help if there is any standard way for linking libraries when c and c++ files are mixed.

Thanks for your time.

yeah ...

solved it by adding the declarations in

#ifdef __cplusplus
extern "C" {
    c function declarations
}
#endif

I want to point out why this can be a problem for future readers of this thread.

C++ does name mangling based on the function signature. Consider the following code:

int fun () { return 0; }
int main () {
    return fun ();
}

An assembly listing of that looks like: (stripped to just the function name)

_Z3funv: ; int fun (void)

And with overloading based on arguments:

int fun () { return 0; }
int fun (int x) { return x; }
int main () {
    return fun ();
}

You get:

_Z3funv: ; int fun (void)
_Z3funi: ; int fun (int)

Notice the mangling of that function name allows for overloading based on arguments because a distinct symbol is used for each function signature. Now, in C, you can not overload the fun function by it's arguments and there is no mangling. But what you can do is turn off name mangling in C++ by wrapping code within an extern "C" {} construct. This comes with a cost, however. Consider the second example above within an extern "C":

extern "C" {
    int fun () { return 0; }
    int fun (int x) { return x; }
}

Trying to compile that yields:

In function ‘int fun(int)’:
error: declaration of C function ‘int fun(int)’ conflicts with
error: previous declaration ‘int fun()’ here

You can no longer overload based on function signatures.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.