MinGW (Command Line)

From Crypto++ Wiki
Jump to navigation Jump to search

Building Crypto++ with MinGW is a topic that comes up often on the Crypto++ mailing list due to errors encountered during a build. This wiki page will provide instructions for building Crypto++ on the command line for MinGW, and explain some of the issues and their work-arounds.

In the past there was some trouble building the library on MinGW due to lack of MinGW development. MinGW was frozen in time dating back to around the early 2000's. The problems were solved by MinGW64 project, which moved forward and provided first class C++11 support. Users who still rely on the 32-bit MinGW may still encounter some trouble.

Most of the Windows related C++11 problems can be controlled though three defines: CRYPTOPP_CXX11_STATIC_INIT, CRYPTOPP_CXX11_SYNCHRONIZATION, and CRYPTOPP_CXX11_ATOMICS. We inspect WINVER and _WIN32_WINNT along with a compiler version, and we disable the C++11 defines accordingly. However, the defines are not available through MinGW and Cygwin, so we can't trun them off automatically.

The Crypto++ fixes below were provided around Crypto++ 6.0 and above. For example, the fix for MinGw's lack of C++11 for std::mutex was checked in at Commit e4cef84883b2. If you are using Crypto++ 5.6.3 through 5.6.5, then you should consider upgrading to Crypto++ 6.0 for the improved support. If you cannot upgrade, then see the referenced commit on how to fix things.

Related pages are Linux (Command Line) and Visual Studio.

Default _WIN32_WINNT

MinGW may not set _WIN32_WINNT or other macros like WINVER. The GNUmakefile will set _WIN32_WINNT if it detects the macro is not set:

ifeq ($(IS_MINGW32),1)
ifeq ($(findstring -D_WIN32_WINNT,$(CXXFLAGS)),)
ifeq ($(findstring -D_WIN32_WINDOWS,$(CXXFLAGS)),)
ifeq ($(findstring -DWINVER,$(CXXFLAGS)),)
ifeq ($(findstring -DNTDDI_VERSION,$(CXXFLAGS)),)
  CXXFLAGS += -D_WIN32_WINNT=0x0501
endif # NTDDI_VERSION
endif # WINVER
endif # _WIN32_WINDOWS
endif # _WIN32_WINNT
endif # IS_MINGW32

This was added at Commit 404316420529.

std::mutex compile error

Using std::mutex often results in a compile problem out of the box.

c:\devdir\>make
g++ -DNDEBUG -g2 -O2 -march=native -pipe -c cryptlib.cpp
In file included from cryptlib.cpp:19:0:
misc.h: In member function 'const T& CryptoPP::Singleton<T, F, instance>::Ref() const':
misc.h:287:14: error: 'mutex' in namespace 'std' does not name a type
  static std::mutex s_mutex;
              ^~~~~
misc.h:296:18: error: 'mutex' is not a member of 'std'
  std::lock_guard<std::mutex> lock(s_mutex);
                  ^~~
misc.h:296:18: error: 'mutex' is not a member of 'std'
misc.h:296:28: error: template argument 1 is invalid
  std::lock_guard<std::mutex> lock(s_mutex);
                            ^
misc.h:296:35: error: 's_mutex' was not declared in this scope
  std::lock_guard<std::mutex> lock(s_mutex);
                                   ^~~~~~~
make: *** [cryptlib.o] Error 1

If you experience this error, then open config.h, scroll down to around line 65, and uncomment CRYPTOPP_NO_CXX11:

// Define CRYPTOPP_NO_CXX11 to avoid C++11 related features shown at the
// end of this file. Some compilers and standard C++ headers advertise C++11
// but they are really just C++03 with some additional C++11 headers and
// non-conforming classes. You might also consider `-std=c++03` or
// `-std=gnu++03`, but they are required options when building the library
// and all programs. CRYPTOPP_NO_CXX11 is probably easier to manage but it may
// cause -Wterminate warnings under GCC. MSVC++ has a similar warning.
// Also see https://github.com/weidai11/cryptopp/issues/529
// #define CRYPTOPP_NO_CXX11 1

If you want to backport CRYPTOPP_NO_CXX11 to Crypto++ 5.6.5 or earlier, then the fix for this problem was checked in at Commit e4cef84883b2.

wspiapi.h compile error

Another compile error that often occurs wspiapi.h: No such file or directory. It is due to MinGW targeting Windows 2000 by default and not supplying wspiapi.h.

g++ -DNDEBUG -g2 -O3 -pthread -pipe -c socketft.cpp
socketft.cpp:36:23: fatal error: wspiapi.h: No such file or directory
 #  include <wspiapi.h>
                       ^
compilation terminated.
make: *** [socketft.o] Error 1

We believe the workaround is to set _WINNT_VER accordingly for MinGW in socketft.cpp:

// <wspiapi.h> is a compatibility header and it needs _WIN32_WINNT >= 0x501.
// The work-around should be OK since it won't cross-pollinate into header files.
#if defined(__MINGW32__) && (_WIN32_WINNT < 0x501)
# undef _WIN32_WINNT
# define _WIN32_WINNT 0x501
#endif

If you want to backport the header fix to Crypto++ 5.6.5 or earlier, then the fix for this problem was checked in at Commit 379e97cc1c0a .