包含带有g ++的库

编程入门 行业动态 更新时间:2024-10-27 16:36:39
本文介绍了包含带有g ++的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的cpp项目使用这些库:

#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>

wiringPi.h 在 / home / pi / wiringPi / wiringPi ,xively.h在 / root / libxively / src / libxively

在使用 xively.h 之前,我可以使用 g ++ -o ilc ilc.cpp -lwiringPi 。我现在尝试 g ++ -o ilc ilc.cpp -I / root / libxively / src / libxively -I / home / pi / wiringPi / wiringPi 。这不工作,并且报告了对wiringPi库的所有函数调用的未定义的引用。

解决方案

Xively C库目前没有'

您需要静态链接到 libxively.a 。

p>

下面是应该让你开始的指令。

git的克隆--recursive github/xively/libxively CD libxively / src目录进行的mkdir MY_PROJECT CD MY_PROJECT

在 my_project ,创建 main.cpp 使用以下代码:

#includexively.h #includexi_err .h #include< stdio.h> #include< string.h> #define XI_FEED_ID 1234 //设置Xively Feed ID(数字,没有quoutes #define XI_API_KEYINSER_YOUR_API_KEY//设置Xively API密钥(双引号字符串) INT的main(){ xi_feed_t饲料; memset的(安培;饲料,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-&数据点[0]; //创建xively库上下文 xi_context_t * xi_context = xi_create_context(XI_HTTP,XI_API_KEY,feed.feed_id); //检查一切是否正常 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; }

现在我们可以编译它并静态链接到 libxively .a :

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

code> ../ bin / my_project 应该产生这样的输出:

%../bin/my_project [222@xively.c] - 获取comm层... [222@xively.c] - 获取传输层... [222@xively.c] - 获取数据层... [231@xively.c] - 连接到端点... [231@xively.c] - 发送数据: PUT /v2/feeds/1234.csv HTTP / 1.1 主机:api.xively 用户代理:libxively-posix / 0.1.x-1a79892 接受:* / * 的X ApiKey:INSER_YOUR_API_KEY 的Content-Type:text / plain的的Content-Length:25 富,0.123000 bar,unknown [231@xively.c] - 发送:218 [231@xively.c] - 读取数据... [231@xively.c] - 收稿日期:512 [231@xively.c] - 响应: HTTP / 1.1 401未经授权日期:星期三,2013年7月17日12时38分零零秒GMT 内容 - 类型:text / plain; charset = utf-8 Content-Length:50 连接:keep-alive WWW验证:Basic realm =Web Password X-Request-Id:0a25d76545c371a2bd6ef368cc1859f1fd5abb9a 将cookie的:_pachcore_app_session = BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTY4ZmU1NjUxMjhmNmI2MDlhN2E1ZDNkZmMyNjNhNGYwBjsAVA%3D%3D - 9510f98ab1aa46a3978e06e41b2548280d4d2a63; domain = .xively; path = /; expires = Wed,2013年7月31日12:38:00 GMT; HttpOnly 您没有访问此资源的权限

插入API密钥和Feed ID,您应该全部设置。 要添加您提到的其他库,只需添加 -lwiringPi 到编译器命令行。

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.

解决方案

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 github/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 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; 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.

更多推荐

包含带有g ++的库

本文发布于:2023-06-08 14:28:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/583076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!