linker problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I'm developing an application that uses a modular design in C++. The modular
design allows to the developers to add functionality to the main application
just adding libraries. 

The library concept it's very similar to the plugins concept. Each library
is a separate project that creates a static library "libXXX.a" that includes
all ".o" modules for the library. There is a main project that call a well
know function in each library to initialize it. During the initialization
the library creates all the functionality that it provides. Mainly that's
the application design.

So, I'm having strange link behaviors when a class is defined in a library
and it is used in another library. 

If we define a class A in classA.cpp file that creates the classA.o compiled
module that belongs to libA.a library...

...and...

...we define a class B t in classB.cpp file that creates the classB.o
compiled module that belongs to libB.a library. 
We define a function InitLibraryB( void ) that creates an instance of classB
within initlibrary.cpp that creates initlibrary.o that belongs to libB.a
library.

Then we have a main.cpp file that contains the main function that
indirectely creates an instace of class B calling the "IniLibraryB"
function.

Then If I try to link the application using

"g++ -o main main.cpp libA.a libB.a" then the linker finds a problem looking
for the class A method implementations.

But if instead of using "g++ -o main main.cpp libA.a libB.a" I use " g++ -o
main main.cpp libA.a libB.a classA.o" the linker successfully links the
application. 

Another way to get the application linked is to simulate the use the class A
in the main module(1) or using class A inside the libA.a library.(2) 

- Example for (1): If I create in main.cpp an if clause that never enters
but the condition is written in a way that the compiler can't optimize it
then "g++ -o main main.cpp libA.a libB.a" links the application. 

int main( int argn, char **argc )
{
  if( argn==-123 ) // We have never enter in the if statement
  {
    class B;  // That creates the instance
    B.Init(); // we use the class in order to avoid the compiler "var not 
              //use" message.
  }

  InitLibraryA( void );
  InitLibraryB( void );       

  return 0;
}

- Example for (2): If within the libA.a exists a function InitLibraryA that
creates a class A and this function is called from the main function from
main.cpp then "g++ -o main main.cpp libA.a libB.a" links the application. 

I'm using the first workaround but I would like to understand what's wrong
and to solve the problem in a better way.

Thanks

Javier Cuevas Domingo
e-mail   : javier.cuevas@xxxxxxxxxxxxxxx







[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux