Unexpected output from Clang - c++

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.

Related

In Visual Studio Code: "no template named 'initializer_list' in namespace 'std'"? [duplicate]

I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).
Which option should I use to turn on C++11 support?
By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.
As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.
xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.
For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.
XCode uses clang and clang++ when compiling, not g++ (assuming you haven't customized things). Instead, try:
$ cat t.cpp
#include <iostream>
int main()
{
int* p = nullptr;
std::cout << p << std::endl;
}
$ clang++ -std=c++11 -stdlib=libc++ t.cpp
$ ./a.out
0x0
Thanks to bames53's answer for pointing out that I had left out -stdlib=libc++.
If you want to use some GNU extensions (and also use C++11), you can use -std=gnu++11 instead of -std=c++11, which will turn on C++11 mode and also keep GNU extensions enabled.

Compiling ACE/TAO 1.2 libraries with CentOS 6

I am on a 64 bit CentOS 6.7 machine with g++ 4.4.7 and I am trying to compile the ACE TAO library 1.2 that was compiled with g++ 2.96.
When I try to compile using the g++ version 4.4.7 I get the following errors (more complete output here
g++ -W -Wall -Wpointer-arith -pipe -O3 -g -Wno-uninitialized -fno-implicit-templates -D_POSIX_THREADS -D_POSIX_THREAD_SAFE_FUNCTIONS -D_REENTRANT -I/home/oscarpelaoo/Downloads/ACE_wrappers -DACE_HAS_EXCEPTIONS -c -fPIC -o .shobj/URL_Addr.o URL_Addr.cpp
In file included from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/ACE.h:17,
from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/INET_Addr.h:17,
from URL_Addr.h:20,
from URL_Addr.cpp:3:
/home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS.h:3110:27: error: new.h: No such file or directory
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/backward/strstream:47,
from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/streams.h:70,
from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS.h:3197,
from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/ACE.h:17,
from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/INET_Addr.h:17,
from URL_Addr.h:20,
from URL_Addr.cpp:3:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/backward/backward_warning.h:28:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated.
In file included from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS_TLI.h:191,
from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS.h:38,
from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/ACE.h:17,
from /home/oscarpelaoo/Downloads/ACE_wrappers/ace/INET_Addr.h:17,
from URL_Addr.h:20,
from URL_Addr.cpp:3:
/home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS_TLI.inl: In static member function ‘static int ACE_OS_TLI::t_accept(ACE_HANDLE, ACE_HANDLE, t_call*)’:
/home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS_TLI.inl:12: warning: the address of ‘call’ will never be NULL
/home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS_TLI.inl:13: warning: the address of ‘reshandle’ will never be NULL
/home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS_TLI.inl:14: warning: the address of ‘handle’ will never be NULL
/home/oscarpelaoo/Downloads/ACE_wrappers/ace/OS_TLI.inl: In static member function ‘static char* ACE_OS_TLI::t_alloc(ACE_HANDLE, int, int)’:
After a bit of research, I found that Red Hat offers some old libstdc++ libraries for CentOS 6 here.
Is it possible to use an old version of g++ 2.96 which I don't believe has 64 bit support or can I somehow use the compatibility tools with g++ 4.4.7 to try to make something happen?
Answering my own post since I was able to install it.
After installing the relevant 32bit libraries, the -m32 flag needed to get added. Then we needed to add a -DACE_LACKS_NEW_H flag since my system does not contain the <new.h> file. After that I just needed add a forward declaration and the library compiled.

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.

minGW compilation warning for each g++

I tried googling woht no luck, maybe someone encountered this
I'm using MinGW on my windows 7 32-bit machine (not sure about the version of MinGW, but the g++ version is g++ (GCC) 4.6.1)
I get the following warning for EACH compilation line:
command-line:0:16: warning: missing whitespace after the macro name
example for a compilation line:
g++ -c "-D__XXX_WINDOWS__;" -g -Wall -o "XXX.o" -Isrc "src/YYY.cpp"
The problem is the ; at the end of your -D switch.
The command you give is essentially equivalent to having:
#define foo;
at the to of your source. This does define foo to ;, but that's not valid C99 (and could be a typo). §6.10.3/3 from C99 draft n1124:
There shall be white-space between the identifier and the replacement list in the definition of an object-like macro.
So the compiler warns you, or emits an error if you compile with -std=c99.
(You can probably drop the quotes too once you've removed the semicolon.)

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

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.)