编译过程请参照《libcurl库nmake编译方案》,下面是主要步骤简单摘抄与修改。
如果你不方便从github上下载,这里有https://autumoon.lanzoub.com/im40i36zznra
1. 找到x64 Native Tools Command Prompt for VS 2022 (本人使用的是VS2022)
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\VC
2. 运行x64 Native Tools Command Prompt for VS 2022
3. 输入命令nmake.exe /f Makefile.vc MODE=dll DEBUG=no MACHINE=x64 GEN_PDB=no WINBUILD_ACKNOWLEDGE_DEPRECATED=yes 即可编译x64位的release库
4. 生成成果物在curl-8.16.0\builds\libcurl-vc-x64-release-dll-ipv6-sspi-schannel 下
下面直接提供封装类与测试类。
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <memory>
#include <filesystem>
// Include curl header directly to avoid type conflicts
#include <curl/curl.h>
namespace qycommon
{
// FTP listing item (moved to namespace scope)
struct RemoteEntry
{
std::string name;
bool is_dir = false;
};
class CLibCurlUser
{
public:
CLibCurlUser();
~CLibCurlUser();
// Basic configuration
void set_url(const std::string& url);
void set_credentials(const std::string& user, const std::string& pwd); // empty for anonymous
void set_connect_timeout(long seconds); // connect timeout
void set_transfer_timeout(long seconds); // transfer timeout (overall)
void set_passive(bool enable); // FTP passive/active
void set_verbose(bool enable); // internal debug only, no stdout/stderr
// Error info
const std::string& last_error() const;
// File operations
bool upload_file(const std::string& local_path, const std::string& remote_url);
bool download_file(const std::string& remote_url, const std::string& local_path);
// Directory operations (recursive)
bool upload_directory(const std::string& local_dir, const std::string& remote_base_url);
bool download_directory(const std::string& remote_base_url, const std::string& local_dir);
private:
// Utils
bool ensure_parent_directory_exists(const std::filesystem::path& filePath);
bool create_local_directory(const std::filesystem::path& dirPath);
// Remote directory ops
bool remote_mkdir_recursive(const std::string& remote_dir_url);
// FTP listing and parsing
bool list_directory_mlsd(const std::string& remote_dir_url, std::vector<qycommon::RemoteEntry>& entries);
bool list_directory_list(const std::string& remote_dir_url, std::vector<qycommon::RemoteEntry>& entries);
// Error handling
void set_error(const std::string& err);
// Common curl options
bool apply_common_opts(CURL* curl, const std::string& url);
// URL helpers
std::string url_join(const std::string& base, const std::string& name);
std::string url_dirname(const std::string& url);
std::string url_basename(const std::string& url);
private:
std::string m_baseUrl; // optional default url
std::string m_user;
std::string m_pwd;
long m_connectTimeoutSec = 15;
long m_transferTimeoutSec = 0; // 0 means unlimited
bool m_passive = true;
bool m_verbose = false;
std::string m_lastError;
};
}
#include "CLibCurlUser.h"
#include <curl/curl.h>
#include <cstdio>
#include <fstream>
#include <sstream>
namespace fs = std::filesystem;
namespace qycommon
{
namespace
{
inline void trim(std::string& s)
{
auto is_space = [](unsigned char c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
};
// trim left
size_t i = 0;
while (i < s.size() && is_space(static_cast<un

最低0.47元/天 解锁文章
393

被折叠的 条评论
为什么被折叠?



