- code::blocks version 17.12
- Ubuntu MATE 19.10 1.22.2
- gcc - 4:9.2.1-3.1ubuntu1
- libavahi-client3 - 0.7-4ubuntu5 (although probably not relevant)
I have C++ app which uses avahi to communicate across the network. This compiles fine and links with:
g++ -o bin/Debug/Zeroconf obj/Debug/AVAHi.o {...} -lavahi-common -lavahi-core -lavahi-common -lavahi-client
So far, so good. I want to put the AvaHi functionality into a static library to make it easier for other developers. Ideally, I would like to include the Avahi library (among others) in my static library to make linking easier but it looks like the libraries it contains have to be linked by the executable - okay, fair enough.
Building the library works fine and goes off without a hitch and generates libHomeLib.a. Running nm on this shows the calls to AvaHi similar to:
U avahi_address_snprint
U avahi_alternative_service_name
so, not munged into C++ format.
Now, creating a simple C++ executable and adding the library to the linker options along with the Avahi libraries compiles fine and links with:
g++ -o bin/Debug/HomeConsole obj/Debug/main.o -lglib-2.0 -lavahi-common -lavahi-client /home/mike/Projects/HomeLib/bin/Debug/libHomeLib.a
(no -l for the library I added?). main () simply referenced a static object declared in the factory:
CHomeFactory *pFactory = CHomeFactory::get_factory ();
However, the link fails with:
/home/mike/Projects/HomeLib/AVAHi.cpp:27: undefined reference to `avahi_client_new'
/usr/bin/ld: /home/mike/Projects/HomeLib/AVAHi.cpp:29: undefined reference to `avahi_service_browser_new'
...
and so on, naming all of the functions directly or indirectly called in the library which is odd since I could call any function from the current code without difficulty - I did by by adding a call in main:
avahi_client_new (NULL, (AvahiClientFlags)0, NULL, NULL, NULL);
This time, the compiler only complained:
/usr/bin/ld: /home/mike/Projects/HomeLib/bin/Debug/libHomeLib.a(AVAHi.o): undefined reference to symbol 'avahi_string_list_get_service_cookie'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libavahi-common.so: error adding symbols: DSO missing from command line
So I added a call to "avahi_string_list_get_service_cookie(NULL);" to main and got a clean link.
At the risk of being specific in my details but vague in my question, does anyone know what the hell is going on? Why does the link fail to connect the library calls unless they are referenced in the main executable?
It looks almost as if the first stage of the linker sees that the library isn't called in the code and so doesn't bother to include it which leads the final fixup stage to be unable to find the functions.