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?
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.
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
In C++ you should use the "c" prefix on all your C-library #includes.
So #include <time.h> should become: #include <ctime>.
But note that when you use #include <ctime> everything in time.h will now be in the std namespace.
So clock() must become std::clock().
For more info see: http://www.parashift.com/c++-faq/include-c-hdrs-system.html
Related
I have a problem with the inclusion of header "queue" via a precompiled header file in c++ (VS 2019).
My Visual Studio solution consists of two projects, one engine project (static library) and one sandbox project (links the engine project) to test the engine's functionality. The engine project uses a precompiled header file, in which i just included "queue" since i am implementing a message system. Both projects are using C++17.
Visual Studio compiles the engine project without a problem, the sandbox project then throws the following error while compiling:
Error C2039 'queue': is not a member of 'std' (path\to\engine\message_handler.h)
As soon as I include "queue" directly in message_handler.h (see code excerpt), the error vanishes and the project starts up just fine.
I have been using the precompiled header for almost a year now and never had any problems.
Can anyone help me with this?
Following are the relevant excerpts from my code.
Precompiled header:
#pragma once
//memory
#include <memory>
//timing
#include <chrono>
#include <ctime>
//container
#include <vector>
#include <queue>
#include <unordered_map>
#include <array>
//string and streams
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
//misc
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <random>
message.h
struct Message
{
Message(){}
uint32_t id_;
};
message_handler.h
#pragma once
//#include <queue> //fixes the issue
#include "message.h"
using UnqPtr = std::unique_ptr<Message>;
class MessageHandler
{
public:
MessageHandler();
private:
static constexpr uint32_t maxMessages_ = 10000;
std::queue<UnqPtr<Message>, std::vector<UnqPtr<Message>>> msqQueue_;
};
message_handler.cpp
#include "trpch.h" //precompiled header
#include "message_handler.h"
MessageHandler::MessageHandler()
{
//reserve space in the underlying vector
std::vector<UnqPtr<Message>> queue;
queue.reserve(maxMessages_);
msqQueue_ = std::queue{queue};
}
I'm writing an MFC application on Visual Studio 2015 in C++. I added some code which uses members of std library and suppose to take an int and create from it a hex char* with the prefix "0x". I tried to build the project on VS 2015 and VS 2017 from two different computers and I get the same errors - VS doesn't recognize the std library. I've tied running the code on other programs (Clion) and it worked well.
When I include #include <stdlib> I get the following error:
cannot open source file "stdlib"
I've tried re-installing VS, and checked I have all the necessary extensions to support C++, but I guess there's still something missing. How can I fix it?
The code:
std::ostringstream ss;
int i = 7;
ss << std::hex << std::showbase << i;
std::string str = ss.str();
const char *output = str.c_str();
std::cout << output << std::endl;
and included the following headers:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
I get the following errors:
'Ostringstream': is not a member of 'std'
'Ostringstream': undeclared identifier
'ss': undeclared identifier
'hex': is not a member of 'std'
'showbase': is not a member of 'std'
'string': is not a member of 'std'
'string': undeclared identifier
Thank you.
I've included the headers in the wrong order. In every C++ project in Visual Studio it includes "stdafx.h" library automatically. This library contains many of the commonly used libraries such as <string> and etc. The solution was to write the includes in the following way:
#include "stdafx.h"
// other headers of the form "header.h"
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
instead of:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
#include "stdafx.h"
// other headers of the form "header.h"
a bit more about this in this question
Thanks for everyone who tried to help, I appreciate your time and attention.
I'm trying to print the text "Ääkkösiä ruutuun." to console with c++. I have windows 7 and am using Code::Blocks editor. Searching on the subject I found that maybe these sort of lines would help
_setmode(_fileno(stdout), _O_U16TEXT);
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
But when I try to compile it, I get the error: _fileno was not declared in this scope.
I have all these includes:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdexcept>
#include <cmath>
#include <sstream>
#include <fstream>
#include <codecvt>
#include <locale>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <cstdio>
#include <ostream>
what am I missing?
Also, one other thing I tried was locale, but then locale::empty wasn't found! Why doesn't my c++ have anything in it?
EDIT
Here is a picture of what my program is doing now.
It prints out just the first letter (Ä). What happens to the rest?
Ok, it seems that setmode sets it so that only one letter gets printed. (Even trying to print normal texts with multiple commands, just results in a single letter.) Without it the scandinavian letters don't print correctly, thought. They look like this:
The answer you found is for Visual Studio, not Code::Blocks.
While the C standard specifies what should in in <stdio.h>, it only specifies a minimum. Implementors may add their own functions, and should do so using an _ (underscore prefix). This is why you should NOT use that prefix. You don't know what you'll break. Microsoft clearly signaled their non-standard extensions using the correct prefix.
The answer is tagged C++, but C++ inherits the contents of <stdio.h> from C.
The line
setlocale(LC_CTYPE, ".OCP");
works!
A complete example:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
wstring readFile(const char* filename) {
wifstream wif(filename);
locale myLoc("");
//locale utf8_locale(locale(), new gel::stdx::utf8cvt<true>);
wif.imbue(myLoc);
basic_stringstream<wchar_t> wss;
wss << wif.rdbuf();
return wss.str();
}
int main() {
setlocale(LC_CTYPE, ".OCP");
wstring contents = readFile("test.txt");
wcout<<L"Does anything get printed out at all???"<<endl;
//wcout <<contents<<endl;
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
wcout<<L"Näkyykö äkköset?"<<endl;
return 0;
}
The text read from file (utf-8) still doesn't print correctly, though.
It should be
Hei!
Täällä on kaksi riviä.
ä's go awry there.
Output:
I'm writing a project in Vulkan and it compiles and runs fine. The code is the same as it always was, but after some software updates (Steam, Visual Studio, etc.) an error has been popping up.
I mention steam as that was causing a separate runtime error. The same error as here: https://www.reddit.com/r/vulkan/comments/8ybq6f/need_some_help_debugging/e29qptx/
Anyway, the line:
const std::vector<const char*> validationLayers = { "VK_LAYER_LUNARG_standard_validation" };
and ones like that using std::vector and std::array give me the error: Ambiguous symbol 'const std::vector<const char*>'
My includes and defines are as follows:
#define GLFW_INCLUDE_VULKAN
#define GLM_FORCE_RADIANS
#define STB_IMAGE_IMPLEMENTATION
#include <glfw3.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <stb/stb_image.h>
#include <iostream>
#include <stdexcept>
#include <functional>
#include <vector>
#include <set>
#include <algorithm>
#include <fstream>
#include <string>
#include <array>
#include <chrono>
#ifdef NDEBUG
const bool enableValidationLayers = false;
#else
const bool enableValidationLayers = true;
#endif
So if there's a way to suppress this particular error highlighting, or if there is a genuine conflict, I'd love to know where it is / how to do it.
Like I say, it still runs fine, but it is annoying to look at my scroll bar and see a bunch of red markers that would normally indicate my program not compiling.
This turned out to be an issue with the version of ReSharper I was using. It is also reported here: https://resharper-support.jetbrains.com/hc/en-us/community/posts/360000430159-Intellisense-issue-with-C-17-standard?flash_digest=bbcceaf4d5a9c12c634a59aba32fc2143a325734
The solution is to turn it off or upgrade to R++ 2018.2
When I use CLion on a Mac to compile C++ code for highlight removal in a single image, there is an error:
Please help me fix it.
#ifndef QX_CVPR09_CTBF_BASIC_H
#define QX_CVPR09_CTBF_BASIC_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <numeric>
#include <vector>
#include <process.h>
#include <direct.h>
#include <io.h>
#include <time.h>
#include <string>
#include <memory.h>
#include <algorithm>
#include <functional> // For greater<int>()
#include <iostream>
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
#define QX_DEF_PADDING 10
#define QX_DEF_THRESHOLD_ZERO 1e-6
class qx_timer {public: void start(); float stop(); void time_display(char *disp=""); void fps_display(char *disp=""); private: clock_t m_begin; clock_t m_end;};
It's a part of my code. The full code is too long.
process.h
process.h is a C header file which contains function declarations and
macros used in working with threads and processes. Most C compilers
that target DOS, Windows 3.1x, Win32, OS/2, Novell NetWare or DOS
extenders supply this header and the library functions in their C
library. Neither the header file nor most of the functions are defined
by either the ANSI/ISO C standard or by POSIX.
Depends on which platform you compile and what standard you use. If you are on linux or compile with c99/ansi standard then this header will probably just not be available (which might be your error)