Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't compile with time- and clock-related functions C++

I starting my first GNU project based on another GNU project to improve upon it and change the implementation.

I tried to implement my own build method, but time- and clock-related functions broke my build.

I've read a lot of questions on Stack Overflow, but I very confused with the three libraries chrono, ctime and time.h.

This is the build errors:

/src/gamed/Logger.cpp

#include "Logger.h"
#include <errno.h>
#include <string.h>

#include <stdarg.h>

#include <time.h>

const std::string Logger::CurrentDateTime()
{
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);

    return buf;
}

Error: time, localtime and strftime identifier not found

/src/gamed/Packets.h

#ifndef _PACKETS_H
#define _PACKETS_H

#include <time.h>
#include <cmath>
#include <set>

{...}

class GamePacket : public BasePacket {

public:
   GamePacket(uint8 cmd = 0, uint32 netId = 0) : BasePacket(cmd, netId) {
      buffer << (uint32)clock();
   }

};

Error: clock identifier not found

/src/gamed/Pathfinder.cpp

#include "Logger.h"
#include "Pathfinder.h"
#include "Map.h"
#include "AIMesh.h"
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <chrono>
#include "Logger.h"
#include "Minion.h"
#include "Champion.h"

Map * Pathfinder::chart = 0;
auto g_Clock = std::clock();

Error: clock isn't member of std

What am I doing wrong?

like image 894
Ahri Love Dev Avatar asked Dec 24 '22 23:12

Ahri Love Dev


2 Answers

I very confused with the three libraries chrono, ctime and time.h.

There's only 2 libraries in that sentence. <chrono> is part of the c++ standard library and was introduced in c++11 version of the standard. None of your code seem to use anything from <chrono>. <time.h> is part of the c standard library. <ctime> is a header in c++ standard library which wraps <time.h> inside std namespace instead of global namespace which is the only "namespace" in c.

#include <time.h>
// ....
auto g_Clock = std::clock();

Error: clock isn't member of std

You included the c header but try to refer to the std namespace. That is not correct. Include <ctime> instead so that clock will be in std.

#include <time.h>
// ...
time_t     now = time(0);

Error: time (...) identifier not found

At a glance, your code there seems correct. Double check that's actually the code you're compiling and getting the errors from. Here's simplified version of that function which compiles fine http://coliru.stacked-crooked.com/a/664f568053103f32

Stylewise, I wouldn't recommend mixing <cXXX> and <XXX.h> headers. Pick one.

like image 144
eerorika Avatar answered Dec 31 '22 12:12

eerorika


Solved!

The problem is the project use enet library, and has time.h, renaming file to enet_time.h the build work great (It's temporary fix, I think is better using namespaces).

Thanks to all and sorry for the inconvenients, I learn more of wrapping C libraries into C++ thanks to all responses.

A greeting

like image 29
Ahri Love Dev Avatar answered Dec 31 '22 12:12

Ahri Love Dev