Qt4: QMap causing "strict-aliasing rules" compilation error - c++

I'm trying to create the following data structure in Qt 4.5 in C++:
QMap<int, QMap<QString, QVector<QPointF> > > animation;
However, the inclusion of this line in my code results in the following error:
cc1plus: warnings being treated as errors
In file included from XXX/XXX/XXX/MainWindow.qt.C.tmp.C:113:
/usr/lib/qt4/include/QtCore/qmap.h: In member function ‘void MainWindow::exportAnn()’:
/usr/lib/qt4/include/QtCore/qmap.h:588: error: dereferencing pointer ‘y’ does break strict-aliasing rules
/usr/lib/qt4/include/QtCore/qmap.h:586: note: initialized from here
Command exited with non-zero status 1
My organization requires me to treat all warnings as errors, so I can't simply ignore this. Is this a bug in Qt, or is there something I'm doing wrong?

The code you mention works fine with Qt 4.6.1 and GCC 4.4.1 on Intel/Mac OS X.
$ cat a.cpp #include <QtCore>
void foo (QMap<int, QMap<QString, QVector<QPointF> > > &map)
{ map.clear(); }
$ g++-4.4-fsf -c -DQT_CORE_LIB -DQT_SHARED -I/Library/Frameworks/QtCore.framework/Versions/4/Headers a.cpp -W -Wall -Werror -g -O3 -fPIC
$
So your issue is probably fixed by upgrading to Qt 4.6.1. (Unless it's a compiler target-specific bug, which seems unlikely; plus, you haven't told us what platform you're compiling on.)

Related

Unexpected result when initializing variable in C++ using curly braces

I'm using atom to practice C++ (I'm very new). I just learned to initialize variables like the following:
#include <iostream>
using namespace std;
int main() {
int myInt {};
return 0;
}
When I build and run the previous code in codelite I receive no errors. However, if I compile my atom file dailyPractice10.cpp using my MacBook terminal (zsh) I get the following error:
dailyPractice10.cpp:7:12: error: expected ';' at end of declaration
int myInt {};
^
;
1 error generated.
I'm using the following command to compile it on terminal:
g++ -o dailyPractice10 dailyPractice10.cpp (compiles)
./dailyPractice10 (runs program)
Does anyone have any feedback why this code runs in codelite but doesn't compile in terminal?
Because this feature is added from c++11.
if you will like to try below command.it will work.
$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp
The key to fixing this issue is to set the C++11 (or above) standards while building your code.
In the console tab of the IDE, the following output is generated before the error. Notice that no standard is being defined while building the code:
make all
Building file: ../1.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"1.d" -MT"1.o" -o "1.o" "../1.cpp"
We need to add the --std=c++1x flag to the g++ command. The following solution is for the ones using the Eclipse IDE and the MacOSX C++ compiler:
Right click on the project from the "Project Explorer".
Go to Properties > C/C++ Build > Settings.
Under the "Tool Settings" tab, find "GCC C++ Compiler" > "Miscellaneous"
In the "Other Flags" text box, edit the text such that it looks like:
-std=c++17 -c -fmessage-length=0
If you intend to use any other c++ standard, replace "c++17" with the standard of your choice ( eg. c++20).
Apply Changes.
Run Clean, and the Build again.
you should try this to compile the Code
g++ -std=c++20 -o dailyPractice10 dailyPractice10.cpp

Issue after GCC upgrade C++

Current Code Base
Os-Linux x64, have perl code, boost 1.42, GCC 4.4.7 and other dependencies as a part of project code.
The build works fine.
After Upgrade of GCC to 4.9.2
We made a copy of existing Linux ec2 instance (which works fine) and upgraded its GCC to 4.9.2.
When we are trying to build the project, getting the below error.
webql/rt/bootstrap: Compiling main.cxx
webql/rt/bootstrap: #/archive/webql/build/al/current/base/dev/general/gfilt -pthread -fPIC -DU_HAVE_NAMESPACE=1 -D_FILE_OFFSET_BITS=64 -D_unix -D_BSD -fexceptions -fmessage-length=0 -I/home/builder/archive/webql/build/al/current/base/dev/perl/core -DQL2_PLATFORM="al" -DJAVA_ARCH="amd64" -fvisibility=hidden -D_XOPEN_SOURCE=500 -I/archive/webql/build/al/current/base/dev/general -I/archive/webql/build/al/current/base/dev/general/include -I. -I/archive/webql/build/al/current/base/dev -I/archive/webql/build/al/current/base/dev/xml -I/archive/webql/build/al/current/base/dev/xml/libxml -I/archive/webql/build/al/current/base/dev/xml/libxslt -I/archive/webql/build/al/current/base/packages/al/inst/include -I/archive/webql/build/al/current/base/dev/zlib -I/archive/webql/build/al/current/base/packages/al/inst/jdk/include -I/archive/webql/build/al/current/base/packages/al/inst/jdk/include/linux -I/usr/java/include -I/usr/java/include/linux -I/mnt/usr/include -ggdb3 -O3 -DNDEBUG -c -o /archive/webql/build/al/current/base/dist/alr/obj/webql/rt/bootstrap/main.o main.cxx
In file included from ../perlapi/perl.h::,
from ../perlapi/Utils.h:,
from main.cxx::
/archive/webql/build/al/current/base/dev/perl/core/perl.h::49: error: declaration of int setresuid(uid_t, uid_t, uid_t) has a different exception specifier
int setresuid(uid_t ruid, uid_t euid, uid_t suid);
In file included from main.cxx::
/mnt/usr/include/unistd.h:: error: from previous declaration int setresuid(__uid_t, __uid_t, __uid_t) throw ()
extern int setresuid (__uid_t __ruid, __uid_t __euid, __uid_t __suid)
In file included from ../perlapi/perl.h::,
from ../perlapi/Utils.h:,
from main.cxx::
/archive/webql/build/al/current/base/dev/perl/core/perl.h: error: declaration of int setresgid(gid_t, gid_t, gid_t) has a different exception specifier
int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
In file included from main.cxx::
/mnt/usr/include/unistd.h:: error: from previous declaration int setresgid(__gid_t, __gid_t, __gid_t) throw ()
extern int setresgid (__gid_t __rgid, __gid_t __egid, __gid_t __sgid)
make: Leaving directory `/archive/webql/build/al/current/base/dev/webql'
Things tried
Re compiled the boost 1.42 and other dependencies
Expectation
Even after trying several things the error remains same. perl.h is part of project code while unistd.h is a system file. Obviously perl.h is C code hence it cannot THROW exception as present in unstd.h function declaration.
Any help would be appreciated.

Error "sigemptyset was not declared in this scope" when using C+11 and Newlib

We are catching compiler errors when using sigemptyset on Cygwin under Newlib. The error occurs with a C++ compiler, but only when -std=XXX is used. Without a standard option, the test program compiles and executes as expected.
The test program is below, and the Cygwin header of interest follows. I don't see anything suspicious in the Cygwin header.
I've tried tricks like #define _GNU_SOURCE and #define _XOPEN_SOURCE 700. I've also tried tricks like using the global and std namespaces. Related, see What does -D_XOPEN_SOURCE do/mean? and Namespace issues in c++11?.
What is causing the compile failure and how do I fix it?
$ cat ~/test.cxx
#include <signal.h>
int main(int argc, char* argv[])
{
struct sigaction new_handler;
return sigemptyset(&new_handler.sa_mask);
}
Without a -std=XXX, it results in:
$ g++ -c test.cxx
$
With a -std=XXX, it results in:
$ g++ -std=c++03 -c test.cxx
test.cxx: In function int main(int, char**):
test.cxx:6:44: error: sigemptyset was not declared in this scope
return sigemptyset(&new_handler.sa_mask);
And when trying to use sigemptyset in the global namespace:
$ g++ -std=c++03 -c test.cxx
test.cxx: In function ‘int main(int, char**)’:
test.cxx:6:12: error: ‘::sigemptyset’ has not been declared
return ::sigemptyset(&new_handler.sa_mask);
^
Things get worse when using -std=gnu++03 and friends.
The function is an extension over the ISO C standard.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigemptyset.html
as such is protected on /usr/include/sys/signal.h by
__XSI_VISIBLE >= 4
see /usr/include/sys/features.h for details.
As defaults the largest definition set is used, but -std=XXX reduces the definition scope
The issue was worked through at Botan 2.1.0 does not compile under Cygwin 2.8.0 with g++ 5.4.0. Here are the two comments of interest.
First, from noloader:
Cygwin uses Newlib, not GNU's libstdc++. When there's no
-std=c++XX, current GCC defaults to -std=gnu++11 (GCC 6 changes
to gnu++14 by default). I
believe GNU sources ensures expected functions, like sigaction, are
available.
You might consider trying -D_XOPEN_SOURCE=600 or
-D_XOPEN_SOURCE=700.
Also see C++ and feature guards Warning
Question on the
Newlib mailing list.
Second, from SideChannel:
Thanks to #noloader. Until now -std=c++11 was set in Makefile. The
important info is in above mentioned thread on the Newlib mailing
list. Yaakov Selkowitz wrote:
G++ defines _GNU_SOURCE on glibc targets, meaning that -std=c++NN is, contrary to the documentation, not strict ISO C++:
So, applying the patch #987
AND setting -std=gnu++11 works for me. I
did not try the other -D options (I think the other fact is more
fundamental). Summarizing, #randombit please apply the PR #987 and set
-std=gnu++11 for gcc under Cygwin.

GoogleTest 1.6 with Cygwin 1.7 compile error: 'fileno' was not declared in this scope

GoogleTest 1.6 with Cygwin 1.7: 'fileno' was not declared in this scope
Error message when building a simple test on Factorial() function in Eclipse CDT:
Invoking: Cygwin C++ Compiler
g++ -std=c++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
In file included from E:\source\gtest-1.6.0\include/gtest/internal/gtest-internal.h:40:0,
from E:\source\gtest-1.6.0\include/gtest/gtest.h:57,
from ../src/challenge.cpp:11:
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1589:51: error: 'fileno' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1595:57: error: 'strdup' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1627:71: error: 'fdopen' was not declared in this scope
Eclipse CDT 8.1 running gcc 4.7.3 on Cygwin 1.7.22
gTest 1.6 succesfully built including demo tests, with cmake 2.8.9 on Cygwin 1.7.22
I've linked the built lib with full path, E:\lib\gtest-1.6.0\Cygwin\libgtest.a
The following command option was added manually, got same error without it.
-DGTEST_OS_CYGWIN=1
Seems the errors have nothing to do with my code. Anyone using gTest with Eclipse and Cygwin?
Thank you,
unsigned long Factorial(unsigned n) {
return n==0? 0 : n*Factorial(n-1);
}
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}
Setting the C++ standard to -std=gnu++0x rather than -std=c++0x, worked for me. You can try the statement:
g++ -std=gnu++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
Setting symbol (-DGTEST_OS_CYGWIN=1) has got nothing to do with this error.
Some functions go beyond the ANSI standard.
These are disabled when you use std=c++11 (or std=c++0x).
Among them are fdopen, fileno and strdup.
There are two possibilities to use them:
Use the GNU dialect (std=gnu++11).
If you want to compile without dialect and make a local exception, you can include stdio.h with the __STRICT_ANSI__ undefined. (see: Error "'fdopen' was not declared" found with g++ 4 that compiled with g++3)
I have tested both on Suse Linux Enterprise 11, MinGW and Cygwin.
Addition: Another (possibly better) way to access non-ANSI symbols would be to add
#define _POSIX_C_SOURCE 200809L
before the first #include in your file. This will give you access to most of the non-standard routines.
Some functions (e.g. realpath(...)) require
#define _BSD_SOURCE
to be inserted on top of your file.

Unexpected output from Clang

I've been testing out clang-llvm to see if it is worth it to mention to my school's IT department to add it to the machines we students program on. For all of our assignments, we are required to compile using g++ -Wall -W -pedantic-errors *.cpp, so I just converted the command to clang++ -Wall -W -pedantic-errors. I got some output that I wasn't expecting:
Attempting to compile...
In file included from test_library.cpp:6:
In file included from ./test_library.h:64:
In file included from ./library.h:167:
./library.hpp:20:23: warning: unused variable 'e' [-Wunused-variable]
catch(Exception & e)
^
Whereas the GCC compiler does not give an error about unused variables in the catch block. Is there anything I can do so that Clang does not freak out about unused variables in try/catch blocks while keeping the command similar to the g++ one?
Clang-LLVM(v2.7) GNU GCC(v4.4.4) Fedora 13
I kinda agree with Mike, but for getting-off-the-ground's sake, try this:
clang++ -Wall -W -pedantic-errors -Wno-unused-variable
I haven't used llvm much but I think the point of the [-Wunused-variable] in the diagnostic is to tell you that you can shut that warning up with -Wno-unused-variable.
What's wrong with catching exception with "catch(Exception &)" if you are not using the variable? You compilers AND your code reviewers will be happier.