janbar / noson

C++ library for accessing SONOS devices.
GNU General Public License v3.0
27 stars 9 forks source link

Correct linker settings #10

Closed kalsan closed 4 years ago

kalsan commented 4 years ago

After installing, the library, I'm currently using gcc -lnosonos mystuff.cpp in the makefile. However whatever I'm trying to compile, I'm getting linker errors. For instance, here's a minimal example:

#include <noson/sonossystem.h>
#include <noson/sonosplayer.h>
#include <noson/contentdirectory.h>
#include <noson/didlparser.h>
#include <noson/imageservice.h>
#include <noson/filestreamer.h>
#ifdef HAVE_PULSEAUDIO
#include <noson/pulsestreamer.h>
#endif

SONOS::System *gSonos = 0;
SONOS::PlayerPtr gPlayer;
int gDebug = 0;

The error message for this example is:

g++ -o binary -lnoson main.cpp
/usr/bin/ld: /tmp/ccv53Urr.o: in function `SONOS::shared_ptr<SONOS::Player>::reset()':
main.cpp:(.text._ZN5SONOS10shared_ptrINS_6PlayerEE5resetEv[_ZN5SONOS10shared_ptrINS_6PlayerEE5resetEv]+0x26): undefined reference to `SONOS::IntrinsicCounter::Decrement()'
/usr/bin/ld: main.cpp:(.text._ZN5SONOS10shared_ptrINS_6PlayerEE5resetEv[_ZN5SONOS10shared_ptrINS_6PlayerEE5resetEv]+0x6d): undefined reference to `SONOS::IntrinsicCounter::~IntrinsicCounter()'
collect2: error: ld returned 1 exit status

What do I need to feed to my linker in order to be able to write noson code?

janbar commented 4 years ago

The links must be after the source.

Build and install noson as shared lib:

mkdir build && cd build cmake .. -DBUILD_SHARED_LIBS=ON make sudo make install

The sample program:

#include <noson/sonossystem.h>
#include <noson/sonosplayer.h>
#include <noson/contentdirectory.h>
#include <noson/didlparser.h>
#include <noson/imageservice.h>
#include <noson/filestreamer.h>
#ifdef HAVE_PULSEAUDIO
#include <noson/pulsestreamer.h>
#endif

SONOS::System *gSonos = 0;
SONOS::PlayerPtr gPlayer;
int gDebug = 0;

void handleEventCB(void* handle)
{
  fprintf(stderr, "#########################\n");
  fprintf(stderr, "### Handling event CB ###\n");
  fprintf(stderr, "#########################\n");
}

int main (int argc, char** argv) {

  gSonos = new SONOS::System(0, handleEventCB);
  if (gSonos->Discover())
  {
    printf("Discovered !!!\n");
  }

  delete gSonos;
  exit(0);
}

Then compile you program: note the link follows the source or object file

g++ -o binary main.cpp -lnoson

If you want to use the static lib:

g++ -o binary main.cpp /usr/local/lib/libnoson.a -lpthread -lz -lssl -lcrypto -lFLAC++ -lFLAC -lpulse-simple -lpulse -lm -lrt
./binary
#########################
### Handling event CB ###
#########################
Discovered !!!
#########################
### Handling event CB ###
#########################
kalsan commented 4 years ago

Thanks a lot! The static lib worked right away, the shared lib approach after running sudo ldconfig. Thank you very much for your blazing fast and very helpful reply. I'll happily code on.