记录 Linux zstd测试程序

系统版本

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

  1.linux命令行环境下 如何从github上获取源代码

  直接git clone 项目的github地址(http开头),如git clone https://github.com/facebook/zstd.git

 

  2.Linux安装cmake 3.5.2

  2.1直接安装centos  

sudo yum install cmake

  2.2直接安装ubuntu

sudo apt update
sudo apt install cmake

  2.3安装指定版本  (centos)  

# 查看当前版本
cmake --version

# 如果存在低版本,则需要先删除当前版本
yum -y remove cmake 

# 安装相关依赖
yum install -y gcc gcc-c++ build-essential autoconf libtool pkg-config

# 下载压缩包
wget https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz --no-check-certificate

# 解压
tar -zxvf cmake-3.5.2.tar.gz

# 安装
cd cmake-3.5.2
./bootstrap
make install 

# 验证是否安装成功
cmake --version

  2.4.cmake如果直接无法使用,需要添加到Linux的环境变量中

  比如cmake新安装在/usr/local/bin下,但是path中没有此目录  

echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

  下面列出三种添加方法:(我们配置时需要结合1-2或者1-3)

  1. 直接用export命令:(但是这种方法只会对当前用户及当前命令行窗口生效,退出后就会丢失)
    export PATH=/usr/local/bin:$PATH
  2. 编辑修改.bashrc文件:(这种方法只会对当前用户生效,需重启系统)
    vim /root/.bashrc 
    #在最后一行添上:
    export PATH=/usr/local/bin:$PATH
  3. 编辑修改profile文件:(这种方法对所有用户生效,需重启系统)
    vim /etc/profile
    export PATH=/usr/local/bin:$PATH

  

  3.库文件,生成zstd库文件

  静态库:Linux中是.a文件;windows中是.lib文件

  动态库:Linux中是.so文件;windows中是.dll文件

 

  4.编译生成测试代码

  保存好c文件后接下来就是编译成可执行文件,输入命令:gcc Hello.c -o hello,gcc是编译c文件的命令,若是.cpp文件则用g++,-o后面的hello就是可执行的文件名(可随意起一个filename);

  目前只有一个cpp,并且所有文件都在同一个目录。

g++ -std=c++11 -static main.cpp libzstd.a libz.a -o main -lpthread

  4.1.跨平台#include “stdafx.h”问题

  (1).替换头文件

  查看#include “stdafx.h”具体包含的内容,直接替换掉

#include <stdio.h>
#include <tchar.h>

  (2).添加头文件,名为:stdafx.h

  4.2.跨平台#include <io.h>问题

  将程序替换为: #include <sys/io.h>

  4.3.跨平台memset memcpy等问题

//使用如下2行即可 
#include<iostream> 
#include <cstring>

  4.4.跨平台fopen_s问题

  不同操作系统对于函数的定义、名称以及参数有所不同,需要在文件前加入代码:

#ifdef __unix
 
#define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename), (mode)))==NULL
 
#endif

  4.5 跨平台搜索目录下项目

vector<string> getFiles(const string& path)
{
    vector<string> vPath;

#ifdef WIN32 
    struct _finddata_t fileinfo;
    auto handle = _findfirst(path.c_str(), &fileinfo);
    if (handle == -1)
    {
        return vPath;
    }

    do
    {
        vPath.push_back(fileinfo.name);
    } while (!_findnext(handle, &fileinfo));

    _findclose(handle);
#else 
    DIR *pDir;
    struct dirent* ptr;
    if (!(pDir = opendir(path.c_str())))
        return vPath;
    while ((ptr = readdir(pDir)) != 0) {
        if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0)
            vPath.push_back(path + "/" + ptr->d_name);
    }
    closedir(pDir);
#endif

    return vPath;
}

  4.6.-std=c++11

  error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

  4.7.cmake cannot find -lxxx

  -l代表library,意思是没有找到对应的包。

  我们可以使用locate libxxx.a查看对应静态库的位置,并将其添加至LD_LIBRABY_PATH环境变量中。

  如果没有找到,则安装相关的环境。

  比如对于stdc++:

  centos:

yum install libstdc++-static

  Ubuntu:

sudo apt-get install libstdc++-11-dev

  4.8.跨平台pthread问题

  pthread不是Linux下的默认的库,也就是在链接的时候,无法找到phread库中哥函数的入口地址,于是链接会失败。
  解决:在gcc编译的时候,附加要加 -lpthread参数即可解决。

 

posted @ 2023-11-23 11:35  暗月梦影  阅读(87)  评论(0编辑  收藏  举报