Using functions from this project, I am trying to compile an run this code:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <curl/curl.h>
#include <json/json.h>
#define BINANCE_HOST "https://api.binance.com"
const __int64 DELTA_EPOCH_IN_MICROSECS = 11644473600000000;
using namespace std;
class BinaCPP {
static string api_key;
static string secret_key;
static CURL* curl;
public:
static void curl_api(string& url, string& result_json);
static void curl_api_with_header(string& url, string& result_json, vector <string>& extra_http_header, string& post_data, string& action);
static size_t curl_cb(void* content, size_t size, size_t nmemb, string* buffer);
static void init(string& api_key, string& secret_key);
static void cleanup();
};
class BinaCPP_logger {
static int debug_level;
static string debug_log_file;
static int debug_log_file_enable;
static FILE* log_fp;
static void open_logfp_if_not_opened();
public:
static void write_log(const char* fmt, ...);
static void write_log_clean(const char* fmt, ...);
static void set_debug_level(int level);
static void set_debug_logfile(string& pDebug_log_file);
static void enable_logfile(int pDebug_log_file_enable);
};
struct timezone2
{
__int32 tz_minuteswest; /* minutes W of Greenwich */
bool tz_dsttime; /* type of dst correction */
};
struct timeval2 {
__int32 tv_sec; /* seconds */
__int32 tv_usec; /* microseconds */
};
int gettimeofday(struct timeval2* tv/*in*/, struct timezone2* tz/*in*/)
{
FILETIME ft;
__int64 tmpres = 0;
TIME_ZONE_INFORMATION tz_winapi;
int rez = 0;
ZeroMemory(&ft, sizeof(ft));
ZeroMemory(&tz_winapi, sizeof(tz_winapi));
GetSystemTimeAsFileTime(&ft);
tmpres = ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
/*converting file time to unix epoch*/
tmpres /= 10; /*convert into microseconds*/
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (__int32)(tmpres * 0.000001);
tv->tv_usec = (tmpres % 1000000);
//_tzset(),don't work properly, so we use GetTimeZoneInformation
rez = GetTimeZoneInformation(&tz_winapi);
tz->tz_dsttime = (rez == 2) ? true : false;
tz->tz_minuteswest = tz_winapi.Bias + ((rez == 2) ? tz_winapi.DaylightBias : 0);
return 0;
}
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void BinaCPP::init(string& api_key, string& secret_key)
{
curl_global_init(CURL_GLOBAL_DEFAULT);
BinaCPP::curl = curl_easy_init();
BinaCPP::api_key = api_key;
BinaCPP::secret_key = secret_key;
}
void
BinaCPP::cleanup()
{
curl_easy_cleanup(BinaCPP::curl);
curl_global_cleanup();
}
void
BinaCPP::curl_api(string& url, string& result_json) {
vector <string> v;
string action = "GET";
string post_data = "";
curl_api_with_header(url, result_json, v, post_data, action);
}
size_t
BinaCPP::curl_cb(void* content, size_t size, size_t nmemb, std::string* buffer)
{
BinaCPP_logger::write_log("<BinaCPP::curl_cb> ");
buffer->append((char*)content, size * nmemb);
BinaCPP_logger::write_log("<BinaCPP::curl_cb> done");
return size * nmemb;
}
void
BinaCPP::curl_api_with_header(string& url, string& str_result, vector <string>& extra_http_header, string& post_data, string& action)
{
BinaCPP_logger::write_log("<BinaCPP::curl_api>");
CURLcode res;
if (BinaCPP::curl) {
curl_easy_setopt(BinaCPP::curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(BinaCPP::curl, CURLOPT_WRITEFUNCTION, BinaCPP::curl_cb);
curl_easy_setopt(BinaCPP::curl, CURLOPT_WRITEDATA, &str_result);
curl_easy_setopt(BinaCPP::curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(BinaCPP::curl, CURLOPT_ENCODING, "gzip");
if (extra_http_header.size() > 0) {
struct curl_slist* chunk = NULL;
for (int i = 0; i < extra_http_header.size(); i++) {
chunk = curl_slist_append(chunk, extra_http_header[i].c_str());
}
curl_easy_setopt(BinaCPP::curl, CURLOPT_HTTPHEADER, chunk);
}
if (post_data.size() > 0 || action == "POST" || action == "PUT" || action == "DELETE") {
if (action == "PUT" || action == "DELETE") {
curl_easy_setopt(BinaCPP::curl, CURLOPT_CUSTOMREQUEST, action.c_str());
}
curl_easy_setopt(BinaCPP::curl, CURLOPT_POSTFIELDS, post_data.c_str());
}
res = curl_easy_perform(BinaCPP::curl);
/* Check for errors */
if (res != CURLE_OK) {
BinaCPP_logger::write_log("<BinaCPP::curl_api> curl_easy_perform() failed: %s", curl_easy_strerror(res));
}
}
BinaCPP_logger::write_log("<BinaCPP::curl_api> done");
}
//////////////////////////////////////////////
/////////////////////////////////////////////////
//-----------------------------------------------
void
BinaCPP_logger::write_log(const char* fmt, ...)
{
if (debug_level == 0) {
return;
}
if (debug_log_file_enable == 1) {
open_logfp_if_not_opened();
}
va_list arg;
char new_fmt[1024];
timeval2 tv;
gettimeofday(&tv, NULL);
time_t t = tv.tv_sec;
struct tm* now = localtime(&t);
sprintf(new_fmt, "%04d-%02d-%02d %02d:%02d:%02d %06ld :%s\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, tv.tv_usec, fmt);
va_start(arg, fmt);
if (debug_log_file_enable && log_fp) {
vfprintf(log_fp, new_fmt, arg);
fflush(log_fp);
}
else {
vfprintf(stdout, new_fmt, arg);
fflush(stdout);
}
va_end(arg);
}
//-----------------------------------------------
// Write log to channel without any timestamp nor new line
void
BinaCPP_logger::write_log_clean(const char* fmt, ...)
{
if (debug_level == 0) {
return;
}
if (debug_log_file_enable == 1) {
open_logfp_if_not_opened();
}
va_list arg;
va_start(arg, fmt);
if (debug_log_file_enable && log_fp) {
vfprintf(log_fp, fmt, arg);
fflush(log_fp);
}
else {
vfprintf(stdout, fmt, arg);
fflush(stdout);
}
va_end(arg);
}
//---------------------
void
BinaCPP_logger::open_logfp_if_not_opened() {
if (debug_log_file_enable && log_fp == NULL) {
log_fp = fopen(debug_log_file.c_str(), "a");
if (log_fp) {
printf("log file in %s\n", debug_log_file.c_str());
}
else {
printf("Failed to open log file.\n");
}
}
}
//---------------------
void
BinaCPP_logger::set_debug_level(int level)
{
debug_level = level;
}
//--------------------
void
BinaCPP_logger::set_debug_logfile(string& pDebug_log_file)
{
debug_log_file = pDebug_log_file;
}
//--------------------
void
BinaCPP_logger::enable_logfile(int pDebug_log_file_enable)
{
debug_log_file_enable = pDebug_log_file_enable;
}
int main()
{
BinaCPP Session;
string api_key = "";
string secret_key = "";
Session.init(api_key, secret_key);
cout << endl;
system("pause");
return 0;
}
I installed co[libcurl], libwebsockets and jsoncpp via vcpkg and then integrated the installs (x86 version) for visual studio. When I try to compile the code (x86 version also) I have those unresolved externals symbols:
LNK2001 unresolved external symbol "private: static int BinaCPP_logger::debug_level" (?debug_level@BinaCPP_logger@@0HA)
LNK2001 unresolved external symbol "private: static int BinaCPP_logger::debug_log_file_enable" (?debug_log_file_enable@BinaCPP_logger@@0HA)
LNK2001 unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits,class std::allocator > BinaCPP::api_key" (?api_key@BinaCPP@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
LNK2001 unresolved external symbol "private: static void * BinaCPP::curl" (?curl@BinaCPP@@0PAXA)
I have no clue how to solve this. Are the librairies installed not the correct ones ?
Thank you for the help