Linux libghttp库的安装和使用

Linux libghttp库的安装和使用

下载地址:https://github.com/sknown/libghttp

./configure --enable-static --prefix=/home/jiang/code/libghttp/target_x86
make && make install

出现错误x86_64-unkown-linux-gun

需要安装libtool。
sudo apt-get install libtool

需要将config.guess和config.sub这两个文件替换掉。
cp /usr/share/libtool/config/config.guess ./
cp /usr/share/libtool/config/config.sub ./

./configure --enable-static --prefix=/home/jiang/code/libghttp/target_x86
make && make install

编译完成。

#库文件复制到/usr/lib目录下
sudo cp -r /home/jiang/code/libghttp/target_x86/lib/* /usr/lib/
#编译
gcc test.c -o test -I/home/jiang/code/libghttp/target_x86/include -L/home/jiang/code/libghttp/target_x86/lib -lghttp
#赋予权限
chmod 777 test
#执行
./test
#include <stdio.h>
#include <ghttp.h>

int main(int argc, char *argv[])
{
    // This is the http request object
    ghttp_request *request = NULL;
    // Allocate a new empty request object
    request = ghttp_request_new();
    // Set the URI for the request object
    ghttp_set_uri(request, "http://chanpinxue.cn/about");
    // Close the connection after you are done.
    ghttp_set_header(request, http_hdr_Connection, "close");
    // Prepare the connection
    ghttp_prepare(request);
    // Process the request
    ghttp_process(request);
    // Write out the body. Note that the body of the request may not be null terminated so we have to be careful of the length. 
    char *result = ghttp_get_body(request);
    printf("%s\n", result);
    //fwrite(ghttp_get_body(request), ghttp_get_body_len(request), 1, stdout);
    // Destroy the request. This closes any file descriptors that may be open and will free any memory associated with the request. 
    ghttp_request_destroy(request);
    
    return 0;
}

同理 生成 arm 平台 lib的编译方法:

#在源码目录配置编译:
./configure --build=i686 --host=arm-linux-gnueabihf --enable-static --prefix=/home/jiang/code/libghttp/target_arm
make && make install

 

其他:
1、Windows中静态库是以 .lib 为后缀的文件,动态库是以 .dll 为后缀的文件。
2、Linux中静态库是以 .a 为后缀的文件,动态库是以 .so为后缀的文件。

发表回复

您的电子邮箱地址不会被公开。