gammasoft71/Examples_wxWidgets

Some tips to help compile in Windows

tttony opened this issue · 1 comments

Hi, my system is:

Windows 7 x64
wxWidgets 3.1.4 (build=release shared=0 monolithic=0)
GCC MinGW 10.2.0
Visual Studio 2017
cmake 3.18.1

I just wanted to know if my wxWidgets compile is good so I downloaded this project and compiled with VS 2017 and GCC

With VS2017

Configuring with cmake it was a PITA, because you have to either create environmente variables or just pass the variables to cmake to configure wxWidgets, here are the variables, the importants are: wxWidgets_ROOT_DIR, wxWidgets_LIB_DIR, wxWidgets_CONFIGURATION, but after creating those variables cmake was complaining about the variable: wxWidgets_LIBRARIES, and the docs says that this variable will be setted by cmake, don't know why was complaining sure I did something wrong but I don't know, at the end just either create environment variables or just pass the variables to cmake, I created all env vars and just:

cmake -G "Visual Studio 15 2017 Win64" ..

Not tested all projects but the ApplicationAndMain ApplicationAndMain2 gives undefined reference to main, this happens because VS uses its own main function for GUIs that is WinMain, the only way to solve this is to change subsystem to console but this shows the console window, even using the FreeConsole() function to hide the window it still appears for less than 2 seconds

With MinGW GCC

I also have GCC 7.1 and it is my default compiler but this version does not fully support C++17 so I had to configure cmake to select the GCC 10.2 like this:

set mingw64_path=c:/mingw-w64/x86_64-10.2.0-posix-seh/mingw64/bin

set CC=%mingw64_path%/gcc.exe
set CPP=%mingw64_path%/g++.exe
set MAKE=%mingw64_path%/mingw32-make.exe
set RC=%mingw64_path%/windres.exe

cmake -DCMAKE_MAKE_PROGRAM=%MAKE% -DCMAKE_C_COMPILER=%CC% -DCMAKE_CXX_COMPILER=%CPP% -DCMAKE_RC_COMPILER=%RC% -DCMAKE_VERBOSE_MAKEFILE=ON -G "MinGW Makefiles" ..

This generated the Makefiles with no problems

Also there was undefined reference to many functions with the RichTextCtrl project , after digging in internet I found that the libs should be in a specific order and since there are many libs I found this and I changed the file link.txt this part:

... --minor-image-version,0, @CMakeFiles\RichTextCtrl.dir\linklibs.rsp

to

... --minor-image-version,0,--start-group @CMakeFiles\RichTextCtrl.dir\linklibs.rsp

and that solved the libs and the project compiled fine

cmake

Now some tips to get better experience with both MSVC compiler and GCC

  • Check if wxWidgets_ROOT_DIR, wxWidgets_LIB_DIR:PATH, wxWidgets_CONFIGURATION vars are set if not show a message that these vars must be set
  • Check for minimun compiler version to run C++17 I think GCC is 7 or 8, dont know about MSVC
  • Detect wxWidgets compilation options like static (passing -DwxWidgets_USE_STATIC:BOOL=ON) in GCC add -static option and MSVC add option /MT for release and /MTd for debug (passing -DwxWidgets_USE_DEBUG:BOOL=ON) here more info
  • Add to the linker the --start-group to solves the libs order

Thank you for your interest.

For the compilation of the ApplicationAndMain and ApplicationAndMain2 projects I lost the following lines in CMakeLists.txt during a refactoring:

if (MSVC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:mainCRTStartup")
endif ()

These lines allow to use int main(int argc, char* argv[]) as an entry point for a Win32 GUI.

Fixed ;-)

On the other hand for CMake. If wxWidgets is correctly installed on your system there is nothing more to do.
The next line of the CMakeLists.txt file initializes all the necessary variables. Also if wxWidgets is not correctly installed the CMake configuration stops with an error :

find_package(wxWidgets REQUIRED)

Follow this link to install wxWidgets correctly on your system: https://www.wxwidgets.org/
Here is a simple example to install wxWidgets 3.14 on windows (admin console) :

git clone https://github.com/wxwidgets/wxwidgets.git -b v3.1.4 --depth 1
cd wxwidgets
git submodule update --init
mkdir build_cmake
cd build_cmake
cmake . -G "Visual Studio 15 2017 Win64" -DwxBUILD_SHARED=OFF
cmake --build . --config Debug --target install

For mingw, install g++8 minimum. Follow this link to install wxWidgets correctly on your system : https://www.wxwidgets.org/
Here is a simple example to install wxWidgets 3.14 on mingw :

git clone https://github.com/wxwidgets/wxwidgets.git -b v3.1.4 --depth 1
cd wxwidgets
git submodule update --init
mkdir build_cmake && cd build_cmake
cmake . -DCMAKE_BUILD_TYPE=Debug -DwxBUILD_SHARED=OFF
cmake --build . --target install -- -j $(nproc)

To answer your request :

"Check if wxWidgets_ROOT_DIR, wxWidgets_LIB_DIR:PATH, wxWidgets_CONFIGURATION vars are set if not show a message that these vars must be set"
This operation is done with :

find_package(wxWidgets REQUIRED)

"Check for minimun compiler version to run C++17 I think GCC is 7 or 8, dont know about MSVC"
For the minimum compiler versions to be installed for C++17 support see: https://en.cppreference.com/w/cpp/17
This operation is done with :

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

"Detect wxWidgets compilation options like static (passing -DwxWidgets_USE_STATIC:BOOL=ON) in GCC add -static option and MSVC add option /MT for release and /MTd for debug (passing -DwxWidgets_USE_DEBUG:BOOL=ON) here more info
Add to the linker the --start-group to solves the libs order"
This operation is done with the following line which fills all the necessary variables correctly:

find_package(wxWidgets REQUIRED)

you can also apply the following command to specify the components:

find_package(wxWidgets COMPONENTS base core html xml richtext RIQURED)

So nothing to add in the CMakeLists.txt. Now you can add more controls and checks in your projects. But this is not the purpose of this project.
The purpose of this project is to show the use of the different wxwidget components in the simplest way. Both in the code and in the build tool.