0

I created a shared library "A" that use an other shared library "B".

I have a problem when I link my program with the shared library "A".

When I use some function from the other shared library ("B") inside cpp file of the shared library "A", all is fine.

But when I use these functions inside .h file (inside a templated method or an inlined method) of the shared library "A", the symbol is not loaded and I get an error "undefined reference to symbol".

I used g++ 7.2. I think the option -l forget to load the symbols used in header file.

Do you have an idea to avoid this?

Update 2:

Here a full reproducible example:

A.cpp

#include "A.h"

A.h

#ifndef A_H
# define A_H

#include <type_traits>
#include "B.h"

class A
{
    public:
    template <typename Type>
    std::enable_if_t<std::is_arithmetic<Type>::value,void> funcA(Type   value);
};

template <typename Type>
std::enable_if_t<std::is_arithmetic<Type>::value,void> A::funcA(Type value)
{
    B tmp;

    tmp.funcB(value);
}

#endif

B.cpp

#include "B.h"
#include <iostream>

void B::example()
{
    std::cout << "works" << std::endl;
}

B.h

#ifndef B_H
# define B_H

class B
{
    public:
    void funcB(int value);
    private:
    void example();
};

inline void B::funcB(int value)
{
    value += 1;
    example();
}

#endif

main.cpp

#include "A.h"

int main()
{
    A tmp;
    tmp.funcA(5);

    return 1;
}

Compile

g++ -std=c++17 -m64 -O2 -DNDEBUG -Wall -Wextra -Werror -fPIC -o A.o -c A.cpp 
g++ -std=c++17 -m64 -O2 -DNDEBUG -Wall -Wextra -Werror -fPIC -o B.o -c B.cpp  
g++ -std=c++17 -m64 -O2 -DNDEBUG -Wall -Wextra -Werror -fPIC -o main.o -c main.cpp
g++ -o libB.so B.o -shared
g++ -o libA.so A.o -shared -L. -lB 
g++ -o application main.o -L . -lA 

Error

main.o: In function `main':
main.cpp:(.text.startup+0x1a): undefined reference to `B::example()'
collect2: error: ld returned 1 exit status

Thank you,

SOLVED:

Finally, I solved my problem with this thread: GCC 4.5 vs 4.4 linking with dependencies

Thank you!

  • where do you instantiate the template? do you link only A or A and B to your executable? – 463035818_is_not_an_ai Feb 20 '18 at 19:26
  • A is linked with B, and the application link with A. The template is instantiate in the application. –  Feb 20 '18 at 19:26
  • It would be useful if you put some code that follows [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Sunil Feb 20 '18 at 19:55
  • Yes, sorry, I wrote a full example. Thank you. –  Feb 20 '18 at 20:14
  • You'll need to find an MCVE that actually reproduces the problem – Mike Kinghan Feb 20 '18 at 20:27
  • Where is the template? – Chris Uzdavinis Feb 20 '18 at 20:28
  • Okay, I'll try to find a reproducible example. –  Feb 20 '18 at 21:04
  • I added a MCVE. I finally found a reproducible example. Thank you. –  Feb 21 '18 at 18:23
  • *`g++ -std=c++17 -m64 -O2 ...`* - You need to add `-g` for symbols. `-g3` will probably be most helpful since that level includes symbolic defines. You might also want to throttle back to `-O0` or `-O1`. If GCC is new enough, there is also `-Og` to minimally disrupt the debugging experience. – jww Feb 22 '18 at 06:53
  • Yes, I know but I paste the command line for the release mode. With my debug mode I have -O0 -g -DDEBUG, but I tried with debug mode and I have the same error without more explication. –  Feb 22 '18 at 09:25

0 Answers0