ctime std:: namespace conflict - c++

I have project with many C and C++ files. I try to add thread-safe queue.
In my header:
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
// Some code..
When I try to compile this, its fault with this errors:
In file included from /usr/include/c++/4.9/chrono:41:0,
from /usr/include/c++/4.9/mutex:39,
from queue.hpp:4,
from main.cpp:24:
/usr/include/c++/4.9/ctime:60:11: error: '::clock_t' has not been declared
using ::clock_t;
/usr/include/c++/4.9/condition_variable:161:23: error: 'time_t' in namespace 'std' does not name a type
static_cast<std::time_t>(__s.time_since_epoch().count()),
As I understand it, compiler try to find std::time_*, but why? And how fix it?
Thanks!
UPD: main.cpp
#include "gpu.hpp" //Error here
int main(int argc, char const *argv[]) {
return 0;
}
gpu.hpp
#pragma once
#include "filter.hpp"
#include "queue.hpp" //Error here
#include <nvcuvid.h>
#include <avformat.h>
#include <vector>
queue.hpp
#pragma once
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
template<typename T>
class CQueue
{
std::queue<T> m_queue;
std::mutex m_mutex;
std::condition_variable m_cond;
// ...
First error message:
In file included from queue.hpp:3:0,
from gpu.hpp:3,
from main-test.cpp:2:
/usr/include/c++/4.9/ctime:60:11: error: '::clock_t' has not been declared
using ::clock_t;
Makefile:
FFMPEG_INCLUDES := -I$(FFMPEG_PATH) $(FFMPEG_PATH)/libavutil ...
$(OBJECTS_DIRS)/app-main-test.o: src/app/main-test.cpp
$(CXX) $(CXXFLAGS) $(FFMPEG_INCLUDES) $(CUDA_INCLUDES) -o $# -c $<

The problem was in my Makefile.
I has include path to each ffmpeg folder. FFMPEG_INCLUDES := -I$(FFMPEG_PATH) $(FFMPEG_PATH)/libavutil ... FFMPEG have time.c in ffmpeg/libavutil It causes conflict with ctime.
I replaced #include <log.h> to #include<libavutil/log.h> and fixed include path in makefile FFMPEG_INCLUDES := -I$(FFMPEG_PATH)
Thank you #user2807083 for help.

Related

Precompiled headers don't make me compile faster

I've just coded this little project to try out pre-compiled headers with g++ :
main.cpp :
#include "header.hpp"
int main() {
std::cout << "Hello world !" << std::endl;
}
header.hpp :
#include <iostream>
#include <algorithm>
#include <functional>
#include <memory>
#include <thread>
#include <utility>
#include <string>
#include <stack>
#include <deque>
#include <array>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
First, I compile the project normally :
$ time g++ main.cpp
real 0m0.738s
user 0m0.646s
sys 0m0.070s
Then I delete the output and I type g++ header.hpp so I can get the precompiled header file : header.hpp.gch
I compile the project again :
$ time g++ main.cpp
real 0m0.721s
user 0m0.626s
sys 0m0.072s
The compile time is exactly the same ! What's the use of pre-compiled headers if they don't improve the compilation speed !?
Did I make a mistake or what ?

Undefined reference in a simple makefile test CPP

I was trying to make a simple makefile to learn how to use headers and make in C++, however it doesn't seems to work, returning "undefined reference" at the end. What is going wrong here? I don't believe there are any mistakes in the .cpp or .hpp files, and I searched a bit about makefile to be sure that it should be working.
My main.cpp:
#include <iostream>
#include <fstream>
#include "test.hpp"
using namespace std;
int main(int argc, char *argv[]){
test::print();
return 0;
}
For test.hpp and test.cpp:
#include <iostream>
#include <fstream>
#pragma once
class test{
private:
public:
static void print();
};
#include <iostream>
#include <fstream>
#include "test.hpp"
using namespace std;
static void print(){
cout << "aaaaaa\n";
}
Finally, the makefile:
all:
rm *.o
g++ -c -g *.cpp
g++ *.o -o main.exe
rm *.o

Main file don't sees incuded header in stdafx.h

Hey I have strange problem...
When I'm adding a class into my project in VS 2015
Im including new class into stdafx.h, and stdafx I including to file with main function. When I compiling a program, VS throws Error:
I show it on the code:
stdafx.h
#pragma once
#include <time.h>
#include <map>
#include "targetver.h"
#include <math.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <deque>
#include <GL/freeglut.h>
#include "FreeImage.h"
#include "vec3.h"
#include "vec2.h"
#include "vec4.h"
#include "TextureManager.h"
#include "SceneObject.h"
#include "Obj3d.h"
#include "Door.h"
#include "Collider.h"
#include "TrashGen.h"
#include "Player.h"
#include "Scene.h"
Door.h:
class Door
{
public:
Door();
~Door();
int key;
};
Door.cpp:
#include "stdafx.h"
#include "Door.h"
Door::Door()
{
key = 10;
}
Door::~Door()
{
}
When i add in main function:
in this file I included stdafx.h
Door *dor;
i have an error:
Error C2065 'dor': undeclared identifier IROBOTGAME
Error C2065 'Door': undeclared identifier IROBOTGAME
Sometimes, Visual C++ looses its way with the precompiled headers, depending on the sequence of your edits.
Simply do a full 'Rebuild', that might fix it.

OpenCV errors when including it's headers in C++

I'm trying to use OpenCV into an existing project; when I add the header #include <opencv2/opencv.hpp> to my source.cc file (just the header, the rest of the code makes no difference), I get the following error.
make all
Building file: source.cc
Invoking: GCC C++ Compiler
g++ -I../__GXX_EXPERIMENTAL_CXX0X__ -I/usr/include/eigen3 -O3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"source.d" -MT"source.d" -o "source.o" "source.cc"
In file included from /usr/local/include/opencv2/core.hpp:3105:0,
from /usr/local/include/opencv2/opencv.hpp:46,
from source.cc:13:
/usr/local/include/opencv2/core/operations.hpp: In member function ‘bool cv::internal::Matx_FastSolveOp<_Tp, m, n>::operator()(const cv::Matx<_Tp, m, m>&, const cv::Matx<_Tp, m, n>&, cv::Matx<_Tp, m, n>&, int) const’:
/usr/local/include/opencv2/core/operations.hpp:131:18: error: missing template arguments before ‘(’ token
return LU(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n) != 0;
^
make: *** [MapMaker_target_init.o] Error 1
The code looks something like this:
MMaker.cc >>>
#include "MMaker.h"
#include "IMatcher.h"
#include <cvd/image_ref.h>
#include <cvd/vector_image_ref.h>
#include <cvd/vision.h>
#include <cvd/image_interpolate.h>
#include <opencv2/opencv.hpp> // <<<<<<<<<< This is the evil line
bool MMaker::init(AR::IMatcher &matcher)
{
//actually empty
}
MMaker.h >>>
#ifndef __MMAKER_H
#define __MMAKER_H
#include <queue>
namespace AR { class IMatcher; }
class MMaker: protected CVD::Thread
{
public:
bool init(AR::IMatcher &matcher);
// ... more stuff ...
}
//...
IMatcher.h >>>
#ifndef __IMATCHER_H
#define __IMATCHER_H
#include <vector>
#include <string>
namespace cv { class Mat; }
namespace AR
{
class IMatcher
{
// ... IMatcher members declaration ...
}
}
IMatcher.cc >>>
#include "IMatcher.h"
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
// ... IMatcher members implementation ...
I know this is related to how/which files are included or their order perhaps, but haven't got to find what's missing.

Multiple definition of C++. How to split C++ program properly?

I wrote a program that works without problems, but the thing I am afraid of is that I get a lot of warnings, when I compile it with -Wall option ( it's program written in C and C++). Actually there is only one type of warning, but occurs many times : Multiple definition of .... ( contructors , destructors, and functions ). I thought I did it correcly, but obviously I am wrong. I have 9 files:
Server.h
Server.cpp - implements methods declared in Server.h
RankingCreator.h
RankingCreator.cpp - implements methods declared in RankingCreator.h
Parser.h
Parser.cpp - implements methods declared in Parser.h
PageHandler.h
PageHandler.cpp - implements methods declared in PageHandler.h
and
Main.cpp
- all header files are included in this file, because I use and combine
functionality of all classes here
Each .cpp file except Main.cpp contains only one corresponding .h file included, for instance Server.cpp contains #include "server.h" and no more .h/.cpp files listed above ( but it does contain headers like stdio.h and string.h ). I can post whole warning message here and code of classes, but the lenght of error is about 50 lines, and all classes would be about 1000 lines, so tell me if it is really needed to solve this. Any idea how to solve this? Do I have to make every function inline or something? Every header file has #if def block at the beginning.
EDIT:
Here is the warning log :
g++ LearnTidyCurl.cpp MyParser.cpp PageHandler.cpp RankingCreator.cpp Server.cpp -lcurl -ltidy -o -Wall Wynik
Here is code of one of my header files, see the way of ifdefs :
#ifndef RANKINGCREATOR_H_
#define RANKINGCREATOR_H_
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
//using namespace std;
struct rankingElement {
std::string url;
int rank;
bool operator() (rankingElement i, rankingElement j) { return (i.rank > j.rank);}
} ;
bool operator==(const rankingElement& elem, const std::string& url);
class RankingCreator {
public:
rankingElement compareRankingElements;
const static int MAX_QUERY_RESULT_SIZE = 20;
RankingCreator();
virtual ~RankingCreator();
bool checkPageRank( rankingElement rElement, std::vector<rankingElement> &ranking );
void insertIntoRanking( rankingElement rElement, std::vector<rankingElement>& ranking);
};
#endif /* RANKINGCREATOR_H_ */
I threw warning message out, because it makes this topic unreadable.
Btw. I use include guards auto-generated by Eclipse - shouldn't they be just fine? ( When creating a new class they are automatically created )
EDIT:
You can download gedit file with error log here :
http://www4.zippyshare.com/v/62324366/file.html
I didn't want to post 105-lined error here and in addition it is in crap format, so would not good look here.
Server.h :
#ifndef SERVER_H_
#define SERVER_H_
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <vector>
...body...
#endif /* SERVER_H_ */
PageHandler.h
#ifndef PAGEHANDLER_H_
#define PAGEHANDLER_H_
#include <tidy.h>
#include <buffio.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
#include <string.h>
#include <curl/curl.h>
#include <vector>
#include <algorithm>
#include <stdexcept>
... body ...
#endif /* PAGEHANDLER_H_ */
MyParser.h
#ifndef MYPARSER_H_
#define MYPARSER_H_
#include <vector>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <algorithm>
#include <queue>
#include <stdlib.h>
...body...
#endif /* MYPARSER_H_ */
Main.cpp
#include <stdio.h>
#include <iostream>
#include <queue>
#include "MyParser.h"
#include "PageHandler.h"
#include "RankingCreator.h"
#include "Server.h"
#define NO_ERROR 0
std::string convertIntToString(int input) {
std::ostringstream ss;
ss << input;
std::string tmpStr = ss.str();
return tmpStr;
}
int main(int argc, char **argv) {
... body ...
return 0;
}
MyParser.cpp
#include "MyParser.h"
PageHandler.cpp
#include "PageHandler.h"
Server.cpp
#include "Server.h"
RankingCreator.cpp
#include "RankingCreator.h"
Change your inclusion guards:
#ifndef FILENAME_H
#define FILENAME_H
//Code
#endif
And I bet your problem goes away. Obviously make sure each FILENAME_H is unique :) And remember - each header needs this around all it's code, but it shouldn't be in your source files.
Write your header files the following way:
#ifndef SERVER_H
#define SERVER_H
//...
#endif
They are called #include guards to avoid double inclusion problems. Read about them here
If you are ever developing on MVSC++, you can use #pragma once as a first line of each header, but the first solution is portable on every platform.