Mixing C and C ++ Libraries

I had a strange problem creating an executable code written in C ++ that uses the C ++ library, which itself relies on the C library. I compiled the C modules that make up the C library using gcc and all other source modules using g ++ . Both C and C ++ libraries are static libraries.

When I include the header file from the C library in the C ++ source code, I also wrap it in extern "C":

extern "C"
{
  #include <c-library-header.h> 
}

The strange thing is that when linking, "undefined reference" errors occur, but they change depending on the order in which I list the libraries:

  • If I first listed the C library, all the characters in this library referenced by the C ++ modules are displayed as "undefined".
  • If I first listed the C ++ library, all the characters in this library referenced by the C ++ modules are displayed as "undefined".

I would think that the order in which static libraries appear on the g ++ command line would be completely irrelevant. Someone tell me?

+4
source share
2 answers

Order is important.

If libxxx depends on libyyy, then libxxx should be specified first, i.e. -lxxx -lyyy

In the unsuccessful case, when both are dependent on each other, then one library can be mentioned twice

-lxxx -lyyy -lxxx

Cm:

+7
source

, ( , ), GNU ld, , . start_group/end_group:

g++ <...flags...> -Wl,--start-group -lxxx -lyyy -Wl,--end-group <...other flags...>

, "" :

g++ <...flags...> -Wl,--start-group xxx.a yyy.a -Wl,--end-group <...other flags...>

​​ , .

+2

Source: https://habr.com/ru/post/1534748/


All Articles