2

My cpp project uses these libraries:

#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <cstring>
#include <time.h>

#include <xively.h>
#include <xi_helpers.h>

#include <wiringPi.h>

The wiringPi.h is in /home/pi/wiringPi/wiringPi and the xively.h is in /root/libxively/src/libxively

Before I used the xively.h I could compile it with g++ -o ilc ilc.cpp -lwiringPi. I now tried g++ -o ilc ilc.cpp -I/root/libxively/src/libxively -I/home/pi/wiringPi/wiringPi. This doesnt work and reports an "undefined reference" for all the function calls of the wiringPi library.

Toast
  • 596
  • 2
  • 19
  • 39

2 Answers2

2

Xively C library currently doesn't implement a high-level C++ wrapper.

You need to statically link with libxively.a.

Below are instructions which should get you started.

git clone --recursive https://github.com/xively/libxively
cd libxively/src
make
mkdir my_project
cd my_project

In my_project, create main.cpp with the following code:

#include "xively.h"
#include "xi_err.h"

#include <stdio.h>
#include <string.h>

#define XI_FEED_ID 1234 // set Xively Feed ID (numerical, no quoutes
#define XI_API_KEY "INSER_YOUR_API_KEY" // set Xively API key (double-quoted string) 

int main() {

    xi_feed_t feed;
    memset( &feed, NULL, sizeof( xi_feed_t ) );

    feed.feed_id = XI_FEED_ID;
    feed.datastream_count = 2;

    feed.datastreams[0].datapoint_count = 1;
    xi_datastream_t* foo_datastream = &feed.datastreams[0];
    strcpy( foo_datastream->datastream_id, "foo" );
    xi_datapoint_t* current_foo = &foo_datastream->datapoints[0];

    feed.datastreams[1].datapoint_count = 1;
    xi_datastream_t* bar_datastream = &feed.datastreams[1];
    strcpy( bar_datastream->datastream_id, "bar" );
    xi_datapoint_t* current_bar = &bar_datastream->datapoints[0];

    // create the xively library context
    xi_context_t* xi_context
        = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );

    // check if everything works
    if( xi_context == NULL )
    {
        return -1;
    }

    xi_set_value_str( current_bar, "unknown" );

    xi_set_value_f32( current_foo, 0.123 );

    xi_feed_update(xi_context, &feed);

    return 0;
}

Now we can compile it and statically link with libxively.a:

g++ main.cpp ../obj/libxively.a -I../libxively/ -o ../bin/my_project

And running ../bin/my_project should produce output like this:

% ../bin/my_project 
[222@xively.c] - Getting the comm layer...
[222@xively.c] - Getting the transport layer...
[222@xively.c] - Getting the data layer...
[231@xively.c] - Connecting to the endpoint...
[231@xively.c] - Sending data:
PUT /v2/feeds/1234.csv HTTP/1.1
Host: api.xively.com
User-Agent: libxively-posix/0.1.x-1a79892
Accept: */*
X-ApiKey: INSER_YOUR_API_KEY
Content-Type: text/plain
Content-Length: 25

foo,0.123000
bar,unknown

[231@xively.c] - Sent: 218
[231@xively.c] - Reading data...
[231@xively.c] - Received: 512
[231@xively.c] - Response:
HTTP/1.1 401 Unauthorized
Date: Wed, 17 Jul 2013 12:38:00 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 50
Connection: keep-alive
WWW-Authenticate: Basic realm="Web Password"
X-Request-Id: 0a25d76545c371a2bd6ef368cc1859f1fd5abb9a
Set-Cookie: _pachcore_app_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTY4ZmU1NjUxMjhmNmI2MDlhN2E1ZDNkZmMyNjNhNGYwBjsAVA%3D%3D--9510f98ab1aa46a3978e06e41b2548280d4d2a63; domain=.xively.com; path=/; expires=Wed, 31-Jul-2013 12:38:00 GMT; HttpOnly

You do not have permission to access this resource

You now can insert the API key and feed ID and you should be all set. To add the other library you mentioned, just add -lwiringPi to the compiler command line.

errordeveloper
  • 6,716
  • 6
  • 41
  • 54
0

You are confusing two different things:

  • insertion of an include file (-I switch)
  • linking with external library (-l switch)

You should keep the -lwiringPi switch in your command line, that is

 g++ -o ilc ilc.cpp -I/root/libxively/src/libxively -I/home/pi/wiringPi/wiringPi -lwiringPi
hivert
  • 10,579
  • 3
  • 31
  • 56
  • this returns `fatal error: xively.h: No such file or directory` – Toast Jul 15 '13 at 18:37
  • Does it with the line I just wrote ? – hivert Jul 15 '13 at 18:40
  • Yor line returns five undefined references to functions of the xively library. – Toast Jul 15 '13 at 18:42
  • So now you should add the corresponding ``-l`` command to link with xively. propably something like ``-lxively``. – hivert Jul 15 '13 at 18:45
  • It returns `/usr/bin/ld: cannot find -lxively`. When I downloaded the xively library with the git command I had to type `make all` which failed. Could this be the problem? [Here](https://github.com/xively/libxively) are the instructions (at "Get started"). – Toast Jul 15 '13 at 18:49
  • I successfuly ran the `make all` comand. But I still don't know what the correct -l command is. – Toast Jul 15 '13 at 20:12
  • In some subdirectory of ``/root/libxively/...`` there should be a file named ``libxively.so`` or something. You should tell the compiler where to find this file by adding a ``-L/THE/DIR/OF/THE/SO/FILE`` to the command line. See http://stackoverflow.com/questions/3078602/c-how-to-add-external-libraries – hivert Jul 15 '13 at 21:54
  • I can only find an .o file, but no .so files. – Toast Jul 15 '13 at 22:14
  • In that case ``g++ -o ilc ilc.cpp /path/to/xively.o -I/root/libxively/src/libxively -I/home/pi/wiringPi/wiringPi -lwiringPi`` may write. Otherwise you should ask the maintainer of this xively thing, as I'm not sure it is correctly installed on your computer. – hivert Jul 16 '13 at 08:34