Search code examples
c++qtcpu-usagesigar

undefined reference to Sigar library


I'm writing a code to get the CPU Usage with the Sigar library but when i try to compile the code i get this error: undefined reference to `sigar_cpu_perc_calculate(sigar_cpu_t*, sigar_cpu_t*, sigar_cpu_perc_t*)@12' What is wrong with my code? in the .pro file i added

INCLUDEPATH += "path_to\\sigar-bin\\include"
LIBS += "path_to\\sigar-bin\\lib\\sigar-x86-winnt.lib"

Code:

#include <QDebug>
#include <sigar.h>
#include <sigar_format.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    sigar_t *sigarcpu1;
    sigar_cpu_t cpu1;
    sigar_open(&sigarcpu1);
    sigar_cpu_get(sigarcpu1, &cpu1);
    sigar_close(sigarcpu1);

    sigar_t *sigarcpu2;
    sigar_cpu_t cpu2;
    sigar_open(&sigarcpu2);
    sigar_cpu_get(sigarcpu2, &cpu2);
    sigar_close(sigarcpu2);

    sigar_cpu_perc_t perc;
    sigar_cpu_perc_calculate(&cpu1, &cpu2, &perc);
    qDebug() << perc.combined;

    return a.exec();
}

Solution

  • Try this:-

    extern "C" {
    #include <sigar_format.h>
    }
    

    See here:-

    http://communities.vmware.com/thread/389369