how to fix the error: ‘::max_align_t’? - c++

I get the error
"/usr/include/c++/5/cstddef:51:11: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^"
So I should update the libraries because I find this solution:
"A workaround until libraries get updated is to include <cstddef> or <stddef.h> before any headers from that library."
I wrote some command on the Ubuntu terminal such as:
bash $ sudo apt-get install apt-file
bash $ sudo apt-file update
bash $ apt-file search stddef.h
Then still the error exist.
Thank you

In the .cpp file where this compile error occurs you need to add
#include <cstddef>
before any of the other headers, e.g.
main.cpp (broken)
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Try to compile that:
$ g++ -std=c++11 -o test main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:10: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^
Then fix it:
main.cpp (fixed)
#include <cstddef>
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Compile and run it:
$ g++ -std=c++11 -o test main.cpp
$ ./test
Hello World

I compiled some code with GNU C++ 4.9 on CentOS, and the issue was not solved by ensuring top position #include (or by the older header name stddef.h).
Weird enough, I searched all header files of the compiler libraries for the global definition of max_aling_t as declared in the offending using declaration... and found none! Could it be in some 'internal compiled header?
So I simply commented-out the "using ::max_align_t;" line in the standard header (not proud of doing this indeed) and it solved the problem... and code is running...
if anyone can explain what is the meaning/impact of this max_align_t ?

I also commented-out the using ::max_align_t; line in /usr/include/c++/4.9/cstddef, while, code is running, but I don't know if there are any consequences by doing this...

Related

How to use png++ (C++ library) on Windows?

I am trying to use this C++ library (png++) on Windows, but I unable to compile any program when I use it. Example of Code I am using to test:
#include <png++/png.hpp>
#include <png.h>
int main(){
//anything
}
When I try to compile using g++ -I path/png++ main.cpp -o main, I get
fatal error: png++/png.hpp: No such file or directory
#include <png++/png.hpp>
I understand png++ depends on libpng, I tried adding it as an I- flag, i.e. compile using
g++ -I path/png++ -I path/libpng main.cpp -o main, but it doesn't resolve the issue, png.h is found by the compiler but not png++/png.hpp.
I hope someone will be able to help.
Thanks!

C++ library header not found

I am OS X user and I've recently installed "cppunit" library using brew. When I try to compile "test.cpp" file using TestCase.h header the error occurs:
> test.cpp:3:10: fatal error: 'TestCase.h' file not found
> #include "TestCase.h"
I am compiling this file:
test.cpp
#include <iostream>
#include "TestCase.h"
using namespace CppUnit;
class EmptyTest : public TestCase
{
};
int main()
{
}
Using this command:
g++ -Wall -pedantic -std=c++14 test.cpp -o test.x -lcppunit
I've also tried compiling with -I giving the path to the library directory but still with the same error.
All my friends using cppunit and brew can simply include the header and the program works fine.
I would appreciate every answer.
I've solved this problem. I had issues with Xcode. Reinstall works fine.

error: `boost' has not been declared

I'm trying to use boost libraries in my C++ application. I'm trying to compile it using g++ with different options.e.g g++ -I /usr/include/boost/filesystem/ -o test.out test.cpp however it always prompt error: 'boost' has not been declared.
And here is my code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
using namespace std;
int main (){
string line;
string fileName = "Read.txt";
ifstream file;
string str;
file.open(fileName.c_str());
cout << "Hello, world!\n";
vector<string> fileLines;
fileLines.clear();
while (getline(file, str))
{
fileLines.push_back(line);
}
cout << "Total Line count:"<<fileLines.size()<<endl;
fileLines.clear();
cout << "Total Line count:"<<fileLines.size()<<endl;
boost::filesystem::path p("/tmp/foo.txt");
return 0;
}
I will be glad if you help me to fix this.
P.S. I'm compiling my application in Centos 4.7 and It contains Boost version 1.32 according to /usr/include/boost/version.hpp
Update:
I also commented boost instruction, but there is some problem with includes: boost/filesystem.hpp: No such file or directory.
It sounds like you have not yet installed the boost header files that you need for includes. Since you are on CentOS, you need to:
yum install boost-devel
That will place the header file you want in:
/usr/include/boost/filesystem/path.hpp
Since you are using boost::filesystem::path, you should change your #include <boost/filesystem.hpp> to #include <boost/filesystem/path.hpp>. Since -I /usr/include is passed to gcc by default, you do not need the -I /usr/include/boost/filesystem, unless you changed the include to path.hpp. However, this would be dangerous because another library may have the same header file name and then you may include the wrong header.
You can try:
g++ -std=c++11 -Os -Wall -pedantic test.cpp -lboost_system -lboost_filesystem -o test
I had the same problem
Let me know if is works
best regards,
Accodring to header files in my Centos Linux, I changed
#include <boost/filesystem.hpp>
to
#include <boost/filesystem/path.hpp>
And also compiled my program with special link options:
g++ test.cpp -o test.out -lboost_filesystem

Using a third party library (sbpl)

I've built and installed a library called sbpl on linux\ubuntu. After installing i have the following files:
usr/local/include/sbpl (have a bunch of files here including a headers.h file)
usr/local/lib has a libsbpl.so file
Now I'm having some trouble compiling a simple program:
yus.cpp
#include <iostream>
#include <sbpl/headers.h>
int main()
{
EnvironmentType type;
return 0;
}
Using these commands to compile i get errors:
$ g++ yus.cpp -Iusr/local/include/sbpl gives the following error
"error: 'EnvironmentType' was not declared in this scope"
$ g++ yus.cpp -L.-lsbpl gives the same error as above
How does one go about compiling and linking the library correctly?
Move the definition of enum EnvironmentType from main.cpp to headers.h and then run g++ yus.cpp -I/usr/local/include -L/usr/local/lib -lsbpl

Undefined Reference Compiler Error

I think I'm getting close, but I'm having this error I've been banging my head against the wall on for hours. I'm missing something stupid, and I've gone character by character but I can't find it.
The compiler is giving me
main.cpp:16: undefined reference to `translator::translator(std::istream&)'
collect2: error: ld returned 1 exit status
when I try to compile my program. The command I'm using to compile is:
clear && g++ -g -Wall main.cpp -o Pear
The three sections of use are as follows:
main.cpp
int main(int argc, char* argv[])
{
std::ifstream myFile;
myFile.open(argv[1]);
translator example(myFile);
myFile.close();
return 0;
}
translator.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "translator.h"
translator::translator(std::istream& in)
{
table1(in);
table2(in);
}
translator.h
#ifndef TRANSLATOR
#define TRANSLATOR
#include <fstream>
#include <iostream>
#include <string>
#include "translationTable.h"
class translator
{
private:
translationTable<std::string, int> table1;
translationTable<int, std::string> table2;
translator();
public:
translator(std::istream& in);
};
#endif
Any ideas? I've tried so much, and I've looked up similar problems, but they all have different sources. Thanks in advance!
The command line for g++ needs to include both source files, like this:
g++ -g -Wall main.cpp translator.cpp -o Pear
Otherwise, the compiler has no idea from where to get the implementation of the translator::translator(std::istream&) member function.
*EDIT: * (from the comment)
I thought that basically the use of header files was so that it would know where to get each implementation of the file?
This part is grossly oversimplified, but it should help you get the picture. Recall that the process of producing an executable from C++ sources consists of two major steps - compilation and linking. The g++ program performs them both (it can do just one if you specify -c flags, or pass only .o files).
The compiler and the linker stages of g++ do not "talk" to each other directly. The compiler produces the inputs for the linker, and that's where the communication ends.
Header files are for the compiler. Specifically, they are for the first stage of compilation - the preprocessing. Once the preprocessor has finished, there is no knowledge of where the definitions came from. Even the compiler does not know it, let alone the linker. That is why you need to "help" the linker by supplying all the relevant sources to g++.
You aren't linking translator.o with your application.
g++ -g -c translator.cpp
followed by
g++ -g -Wall main.cpp translator.o -o Pear