gpt4 book ai didi

c++ - 使用 g++ 包含库

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:49:48 26 4
gpt4 key购买 nike

我的 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 库的所有函数调用的“ undefined reference ”。

最佳答案

Xively C 库目前没有实现高级 C++ 包装器。

您需要使用 libxively.a 进行静态链接。

下面是可以帮助您入门的说明。

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

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

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

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

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

运行 ../bin/my_project 应该产生如下输出:

% ../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

您现在可以插入 API key 和 Feed ID,您应该已准备就绪。要添加您提到的其他库,只需将 -lwiringPi 添加到编译器命令行即可。

关于c++ - 使用 g++ 包含库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17661058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com