0

I want to use libmpdclient-dev in my C++ program. I installed it on my system.

Under /usr/include/mpd I found all the header files.

My test program looks like this:

#include<iostream>  
#include<mpd/client.h>

using namespace std;

struct mpd_connection *conn;

int main() {
    conn = mpd_connection_new("192.168.1.151", 6600, 0);

    if(conn == NULL) {
        cout << "Not connected \n";
    }
    else {
        cout << "Connected \n";
    }

    return 0;
}

It compiles fine:

g++ -g  -Wfatal-errors  -std=c++11 main.cpp -c -o main.o

However, running the linker as follows:

g++ -g  -Wfatal-errors  -std=c++11 main.o -o run

gives this error:

main.o: In Function `main':
~/mpd/main.cpp:13: undefined reference to `mpd_connection_new'
collect2: error: ld returned 1 exit status

What do I need to add or change to link successfully?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
SteWe
  • 1
  • 1
  • 1
    Installing the library is not enough, you need to link explicitely to the installed library. Probably with something like this: `g++ -g -Wfatal-errors -std=c++11 -lmpdclient-dev main.o -o run`, or `g++ -g -Wfatal-errors -std=c++11 /usr/include/mpd/libmpdclient-dev.a main.o -o run` – Jabberwocky Jul 26 '18 at 10:22
  • thank you for your help. -lmpdclient made it working – SteWe Jul 26 '18 at 11:29

0 Answers0