gcc -Wextra warning comparison between signed and unsigned - gcc

I have started making use of the compiler options -Wall and -Wextra
Compiling my one .c file with -Wall I get no warnings.
for -Wextra I get just 3 warnings and they are for 123, 552, and 881 . At each of those lines the code is the same and is exactly this:
for ( index = 0; index < strlen(str); index++ )
with the warning issued by -Wextra being
mycode.c:123: warning: comparison between signed and unsigned
mycode.c:552: warning: comparison between signed and unsigned
mycode.c:881: warning: comparison between signed and unsigned
the variable index in the given functions is defined/declared locally as an int and I know strlen() provided by string.h returns type size_t
what is the best way to write that line of code so there is no warning message generated?

Related

Undefined reference to sequence when trying to input a vector

This seems a simple but somehow the compile sends this error message which I'm not able understand thus correct my code.
This is a simplified version of what I did, just so it can appear the error for you:
Main.cpp
include "myfunction.h"
int main(){
std::vector<int> myVet = {1,4,3};
sequence(1,2,1,myVet);
}
myfunction.h
#include <vector>
/*funtion creates a sequence*/
void sequence(int start, int end,
int step, std::vector<int> skip);
myfunction.cpp
#include "myfunction.h"
void sequence(int start, int end,
int step, std::vector<int> skip){
auto x = 0;
};
This gives me an error message which says
In function 'main':
/home/machina/Documents/Grafos&Redes/Implementação/main.cpp:18: undefined reference to 'sequence(int, int, int, std::vector <int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status
Could you please explain me why it appears?
This is the following command which I've been using for compiling
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp -o main.out
You are only passing main.cpp to g++.
g++ needs to know about myfunction.cpp where your function is defined so as to compile and link it to your program.
The command to use should be:
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp myfunction.cpp -o main.out

Cannot convert ‘timeval*’ to ‘PTIMEVAL {aka __ms_timeval*}’

Getting this error during wxWidgets compilation for FileZilla on Windows10/MSYS2/Cygwin.
$ ./configure --prefix="$HOME/prefix" --enable-static --disable-shared
--build=x86_64-w64-mingw32 --enable-unicode --without-libtiff --without-libjpeg --with-expat=builtin --with-libpng=builtin --disable-fsvolume
$ make
/home/alex_buz/wx3/bk-deps g++ -c -o netlib_socket.o -I./.pch/wxprec_netlib -D__WXMSW__ -DWXBUILDING -I./src/png -I./src/zlib -I./src/regex -I./src/expat/lib -DwxUSE_GUI=0 -Wall -Wundef -Wunused-parameter -Wno-ctor-dtor-privacy -Woverloaded-virtual -D_FILE_OFFSET_BITS=64 -I/home/alex_buz/wx3/lib/wx/include/msw-unicode-static-3.0 -I./include -DWX_PRECOMP -O2 -fno-strict-aliasing ./src/common/socket.cpp
./src/common/socket.cpp: In member function ‘wxSocketEventFlags
wxSocketImpl::Select(wxSocketEventFlags, const timeval*)’:
./src/common/socket.cpp:1343:73: error: cannot convert ‘timeval*’ to ‘PTIMEVAL {aka __ms_timeval*}’ for argument ‘5’ to ‘int select(int, fd_set*, fd_set*, fd_set*, PTIMEVAL)’
const int rc = select(m_fd + 1, preadfds, pwritefds, &exceptfds, &tv);
^
make: *** [Makefile:29796: netlib_socket.o] Error 1
Google search gave some hints that it's related to boost.
Any way to resolve it?
Both values are different structures, because the members are of different type. Look at the definition code of them.
You cant convert them => make an instance and set the members.

Unable to run "check-all" target when building LLVM-Clang 3.6.1

I have successfully compiled the 3.6.1 version of LLVM-Clang-LLDB-Compiler-RT-LibCxx-LibCxxABI-TestSuite bundle. However, when I tried ninja check-all, the check failed right at the first target [1/150] Generating sanitizer_bitvector_test.cc.x86_64.o. I think the reason is that LLVM is trying to use the system GCC header files, but I compiled LLVM with another GCC toolchain (GCC 4.8.4) that I compiled and installed in a non-root directory. However, I can't figure out how to let the CMake avoid using the system GCC headers (I have modified the PATH and LD_LIBRARY_PATH, but they didn't help at all).
Here is the relevant information:
CentOS 6.6
x86_64 architecture (Intel(R) Xeon(R) CPU W3550 # 3.07GHz)
Tools used for installation:
1) gcc/4.8.4
2) ninja
3) zlib/1.2.8
4) python-miniconda
Linux Kernel version
2.6.32-504.16.2.el6.x86_64
CMake build command
cmake -G "Ninja" -DCMAKE_C_COMPILER=/Scr/scr-test-steven/install/gcc/4.8.4/bin/gcc -DCMAKE_CXX_COMPILER=/Scr/scr-test-steven/install/gcc/4.8.4/bin/c++ -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/Scr/scr-test-steven/install/llvm/3.6.1 /Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src -DLLDB_DISABLE_PYTHON=1 -DCMAKE_CXX_FLAGS:STRING="-I/Scr/scr-test-steven/install/gcc/4.8.4/include -I/Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/tools/clang/include -I/Scr/scr-test-steven/install/libedit/3.1/include -I/Scr/scr-test-steven/install/miniconda/include/python2.7 -L/Scr/scr-test-steven/install/gcc/4.8.4/lib64 -L/Scr/scr-test-steven/install/libedit/3.1/lib -L/Scr/scr-test-steven/install/miniconda/lib -L/Scr/scr-test-steven/install/miniconda/lib/python2.7" -DPYTHON_HOME=/Scr/scr-test-steven/install/miniconda -DLLVM_LIB_SEARCH_PATH=/Scr/scr-test-steven/Programs/LLVM/build_llvm-3.6.1/lib
ninja check results are okay, which means LLVM was compiled successfully:
[1/1] Running the LLVM regression tests
lit.py: lit.cfg:271: note: Did not find llvm-go in /Scr/scr-test-steven/Programs/LLVM/build_llvm-3.6.1/./bin
-- Testing: 12316 tests, 8 threads --
Testing: 0 .. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
Testing Time: 105.65s
* Expected Passes : 12056
* Expected Failures : 79
* Unsupported Tests : 181
The first few lines of errors for ninja check-all (see details in Log.check.log ):
[1/150] Generating sanitizer_bitvector_test.cc.x86_64.o
FAILED: cd /Scr/scr-test-steven/Programs/LLVM/build_llvm-3.6.1/projects/compiler-rt/lib/sanitizer_common/tests && /Scr/scr-test-steven/Programs/LLVM/build_llvm-3.6.1/./bin/clang -I/Scr/scr-test-steven/install/gcc/4.8.4/include -I/Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/tools/clang/include -I/Scr/scr-test-steven/install/libedit/3.1/include -I/Scr/scr-test-steven/install/miniconda/include/python2.7 -L/Scr/scr-test-steven/install/gcc/4.8.4/lib64 -L/Scr/scr-test-steven/install/libedit/3.1/lib -L/Scr/scr-test-steven/install/miniconda/lib -L/Scr/scr-test-steven/install/miniconda/lib/python2.7 -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -ffunction-sections -fdata-sections -Wall -std=c++11 -Wno-unknown-warning-option -DGTEST_NO_LLVM_RAW_OSTREAM=1 -DGTEST_HAS_RTTI=0 -I/Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/utils/unittest/googletest/include -I/Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/utils/unittest/googletest -I/Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/projects/compiler-rt/include -I/Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/projects/compiler-rt/lib -I/Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/projects/compiler-rt/lib/sanitizer_common -fno-rtti -O2 -Werror=sign-compare -Wno-non-virtual-dtor -m64 -c -o sanitizer_bitvector_test.cc.x86_64.o /Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/projects/compiler-rt/lib/sanitizer_common/tests/sanitizer_bitvector_test.cc
clang-3.6: warning: argument unused during compilation: '-L/Scr/scr-test-steven/install/gcc/4.8.4/lib64'
clang-3.6: warning: argument unused during compilation: '-L/Scr/scr-test-steven/install/libedit/3.1/lib'
clang-3.6: warning: argument unused during compilation: '-L/Scr/scr-test-steven/install/miniconda/lib'
clang-3.6: warning: argument unused during compilation: '-L/Scr/scr-test-steven/install/miniconda/lib/python2.7'
In file included from /Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/projects/compiler-rt/lib/sanitizer_common/tests/sanitizer_bitvector_test.cc:16:
In file included from /Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/projects/compiler-rt/lib/sanitizer_common/tests/sanitizer_test_utils.h:28:
In file included from /Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/projects/compiler-rt/lib/sanitizer_common/tests/sanitizer_test_config.h:20:
In file included from **/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7** /vector:61:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:66:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:89:10: error: no matching function for call to 'forward'
: first(std::forward<_U1>(__x)),
^~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:1177:13: note: in instantiation of function template specialization 'std::pair<std::_Rb_tree_iterator<const char *>, bool>::pair<std::_Rb_tree_iterator<const char *>, bool>' requested here
return pair<iterator, bool>(_M_insert_(__x, __y, __v), true);
^
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_set.h:411:9: note: in instantiation of member function 'std::_Rb_tree<const char *, const char *, std::_Identity<const char *>, std::less<const char *>, std::allocator<const char *> >::_M_insert_unique' requested here
_M_t._M_insert_unique(__x);
^
/Scr/scr-test-steven/Programs/LLVM/llvm-3.6.1.src/utils/unittest/googletest/include/gtest/internal/gtest-internal.h:628:25: note: in instantiation of member function 'std::set<const char *, std::less<const char *>, std::allocator<const char *> >::insert' requested here
defined_test_names_.insert(test_name);
^
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/move.h:51:5: note: candidate function [with _Tp = std::_Rb_tree_iterator<const char *>] not viable: no known conversion from 'std::_Rb_tree_iterator<const char *>' to 'typename std::identity<_Rb_tree_iterator<const char *> >::type &&' (aka 'std::_Rb_tree_iterator<const char *> &&') for 1st argument
forward(typename std::identity<_Tp>::type&& __t)
^
In particular you may notice that:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7 was used. However, 'ninja check-all' really should have used the header files for gcc-4.8.4, not gcc-4.4.7 (system bundled gcc).
I have filed a bug report, but no one seems to want to answer it.
If anyone knows how to fix this, please let me know. Thanks.

Building Crypto++ 5.6.2 with Cygwin x86_64

I am attempting to build CryptoPP with Cygwin x86_64, but I'm having some trouble with osrng.cpp.
I'm using gcc's C++ compiler, and I'm getting the following error at line 50 of osrng.cpp
$ make
g++ -DNDEBUG -g -O2 -march=native -pipe -c osrng.cpp
osrng.cpp: In constructor ‘CryptoPP::MicrosoftCryptoProvider::MicrosoftCryptoProvider()’:
osrng.cpp:50:80: error: invalid conversion from ‘CryptoPP::MicrosoftCryptoProvider::ProviderHandle* {aka long unsigned int*}’ to ‘HCRYPTPROV* {aka long long unsigned int*}’ [-fpermissive]
if(!CryptAcquireContext(&m_hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
^
In file included from /usr/include/w32api/windows.h:95:0,
from osrng.cpp:19:
/usr/include/w32api/wincrypt.h:646:26: error: initializing argument 1 of ‘WINBOOL CryptAcquireContextA(HCRYPTPROV*, LPCSTR, LPCSTR, DWORD, DWORD)’ [-fpermissive]
WINIMPM WINBOOL WINAPI CryptAcquireContextA(HCRYPTPROV *phProv,LPCSTR szContainer,LPCSTR szProvider,DWORD dwProvType,DWORD dwFlags);
^
GNUmakefile:208: recipe for target 'osrng.o' failed
make: *** [osrng.o] Error 1
I have successfully compiled the whole thing under the 32bit version of Cygwin before.
How can I fix this for the 64bit edition?
EDIT As suggested by #Yakov's answer, I replaced if defined(_WIN64)if defined(__x86_64__) by if in line 30 of osrng.h.
Sadly though, I immediately run into the next problem, indicated by the following error:
$ make
g++ -DNDEBUG -g -O2 -march=native -pipe -c fipstest.cpp
In file included from dll.h:30:0,
from fipstest.cpp:8:
osrng.h:34:19: error: expected ‘;’ at end of member declaration
typedef unsigned __int64 ProviderHandle; // type HCRYPTPROV, avoid #include <windows.h>
^
osrng.h:34:27: error: ‘ProviderHandle’ does not name a type
typedef unsigned __int64 ProviderHandle; // type HCRYPTPROV, avoid #include <windows.h>
^
osrng.h:38:2: error: ‘ProviderHandle’ does not name a type
ProviderHandle GetProviderHandle() const {return m_hProvider;}
^
osrng.h:40:2: error: ‘ProviderHandle’ does not name a type
ProviderHandle m_hProvider;
^
GNUmakefile:208: recipe for target 'fipstest.o' failed
make: *** [fipstest.o] Error 1
Thankfully, GCC error messages have improved significantly in recent releases. The {aka}s tell you that there is a mismatch between long and long long. Keep in mind that Win64 is LLP64: long is still only 32-bits, and you need a long long to match the size of a pointer. Most (all?) other 64-bit platforms are LP64, meaning that long matches the size of a pointer.
In this particular case, see the if defined(_WIN64) in osrng.h; you will need to change that condition to if defined(__x86_64__) instead.
You might run into problems with the __int64 type as well. Replace those with long long. (Occurs here, here and here.)
You might also find word64 useful. From Crypto++'s config.h:
#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef unsigned __int64 word64;
#define W64LIT(x) x##ui64
#else
typedef unsigned long long word64;
#define W64LIT(x) x##ULL
#endif
There's a couple of Cygwin defines in there too. But they don't look interesting. For example:
#ifndef TYPE_OF_SOCKLEN_T
# if defined(_WIN32) || defined(__CYGWIN__)
# define TYPE_OF_SOCKLEN_T int
# else
# define TYPE_OF_SOCKLEN_T ::socklen_t
# endif
#endif
#if defined(__CYGWIN__) && defined(PREFER_WINDOWS_STYLE_SOCKETS)
# define __USE_W32_SOCKETS
#endif
(Sorry about the comment here. Its too bing to fit in the little comment block).

Why is GCC complaining about missing identifiers before a 'sizeof' that doesn't exist?

I'm in the process of cross-compiling GTK+ 3.4.4 for Windows. I have already cross-compiled all of the build dependencies for GTK (ATK, Cairo, GDK Pixbuf, and Pango) and installed them to /usr/i686-w64-mingw32/.
Attempting to compile GTK itself, however, results in the following error:
In file included from gdkrgba.c:31:0:
fallback-c89.c:40:1: error: expected identifier or '(' before 'sizeof'
fallback-c89.c:40:1: error: expected ')' before '==' token
Line 34 - 44 of gdk/fallback-c89.c contains:
34. #ifndef HAVE_ISINF
35. /* Unfortunately MSVC does not have finite()
36. * but it does have _finite() which is the same
37. * as finite() except when x is a NaN
38. */
39. static inline gboolean
40. isinf (double x)
41. {
42. return (!_finite (x) && !_isnan (x));
43. }
44. #endif
I haven't the slightest idea where GCC is finding 'sizeof' or '=='. Why is the compiler throwing such a cryptic error message and how can I fix it?
Edit: here is the actual command line:
/usr/bin/i686-w64-mingw32-gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I..
-DG_LOG_DOMAIN="Gdk" -DGDK_COMPILATION -I.. -I../gdk -I..
-DG_DISABLE_CAST_CHECKS -mms-bitfields
-I/usr/i686-w64-mingw32/include/pango-1.0
-I/usr/i686-w64-mingw32/include/glib-2.0
-I/usr/i686-w64-mingw32/lib/glib-2.0/include
-I/usr/i686-w64-mingw32/include/cairo -I/usr/i686-w64-mingw32/include/pixman-1
-I/usr/i686-w64-mingw32/include -I/usr/i686-w64-mingw32/include/freetype2
-I/usr/i686-w64-mingw32/include/libpng15
-I/usr/i686-w64-mingw32/include/gdk-pixbuf-2.0 -O2 -Wall -mms-bitfields -MT
gdkrgba.lo -MD -MP -MF .deps/gdkrgba.Tpo -c gdkrgba.c -DDLL_EXPORT -DPIC -o
.libs/gdkrgba.o
Further edit: after compiling with the -E option, I captured the following pre-processed output... which explains the strange sizeof:
# 39 "fallback-c89.c"
static inline gboolean
((sizeof (double x) == sizeof (float) ? __fpclassifyf (double x) : sizeof double x)) == (0x0100 | 0x0400))
{
return (!_finite (x) && !_isnan (x));
}
I can only conclude that isinf is already a defined macro. It is merely being expanded when used in the function declaration above.
My question now becomes... why is HAVE_ISINF not defined? Is it a problem with the configure script?
Yet another edit: okay, so I decided to search for everything in the build tree that contained the string 'HAVE_ISINF' and came across the following instances:
autom4te.cache/traces.1
m4trace:configure.ac:740: -1- AH_OUTPUT([HAVE_ISINF], [/* Define to 1 if you
have the `isinf\' function. */
#%:#undef HAVE_ISINF])
config.h.in
/* Define to 1 if you have the `isinf' function. */
#undef HAVE_ISINF
config.h
/* Define to 1 if you have the `isinf' function. */
/* #undef HAVE_ISINF */
Surprisingly, there is nothing in config.log mentioning `HAVE_ISINF'.
(Possibly) final edit: I did some more investigation and found the string 'isinf' in autom4te.cache/output.0 here: http://paste.ubuntu.com/1154478/
This code made a reference to ac_fn_c_check_func, so I dug up the source for that function and compiled the .c sample that the script generates:
test.c:25:6: warning: conflicting types for built-in function ‘isinf’
[enabled by default]
/tmp/ccLYd1R8.o:test.c:(.text+0xc): undefined reference to `_isinf'
collect2: ld returned 1 exit status
This is odd since my explanation above would suggest that isinf is simply a macro.
I finally found this. In summary:
"The isnan() and isinf() are C99 macros not functions so use AC_CHECK_DECL instead of AC_CHECK_FUNCS for those."
So it looks like I'll be patching the source.

Resources