QT5 uses mingw64 and mingw32 to compile the math library GSL

QT5 uses mingw64 and mingw32 to compile the math library GSL

1. Tool preparation

? QT5.12.9 version, the gsl library of mingw64 compiled by myself before is used normally. In the process of upgrading and maintaining the subsequent version, it is necessary to use 32 bits. I wanted to compile it directly and use it directly, but the result was unexpected, and there were some unexpected things. outside the problem. Let’s record the correct compilation method to facilitate future learning and accumulation.

? The QT5.12.9 I downloaded has already installed the mingw64 and mingw32 editors during installation. If you have not installed it, you can install it yourself. The corresponding versions of mingw64 and mingw32 are both mingw7.3.0.


? The GSL version I am using is GSL2.7. You can go to the official website of GSL to download the source code. After downloading, unzip it to the working path, do not have a Chinese path, for subsequent compilation.

? You need to download msys before compiling. msys is a tool developed by the minGW open source project to simulate the Unix command window under windows. We can use mingw on the command line to quickly compile the program. The download URL of msys is: MinGW-builds – Browse /external-binary-packages at SourceForge.net. I am using version 2013-05-15.

2. Compilation environment configuration

2.1 mingw environment configuration

? Add the bin directory of mingw to the environment variable of the system. The mingw installation path that comes with QT is in the QT installation path. Mine are: C:\Qt\Qt5.12.9\5.12.9\mingw73_64\bin and C :\Qt\Qt5.12.9\5.12.9\mingw73_32\bin. At the same time, C:\Qt\Qt5.12.9\Tools\mingw730_64\bin and C:\Qt\Qt5.12.9\Tools\mingw730_32\bin need to be put into the environment variable together. Regarding the setting of environment variables, there are a lot of them on the Internet.

2.2 msys configuration

? Unzip the downloaded msys to C:\Qt\Qt5.12.9\Tools\mingw730_32 and C:\Qt\Qt5.12.9\Tools\mingw730_64, one copy for each compiler, so that there is no need to set it back and forth for future use. And create a new mingw empty folder under the folder, and modify the /etc/fstab file under msys, the contents are as follows: Note that the corresponding files are written according to different digits.

C:/Qt/Qt5.12.9/Tools/mingw730_64/mingw
C:/Qt/Qt5.12.9/Tools/mingw730_32/mingw

3. Compile the GSL math library

? Take mingw64 bit as an example, open the msys.bat in the corresponding directory, and enter the g + + -v command to check whether it is a compiler with the corresponding number of bits. The following output shows that the configuration is correct.

? Enter the downloaded GSL source code directory

? Enter ./configure to configure the source code

? After the configuration is complete, compile the source code with make -j16

? After the compilation is complete and there is no error, proceed directly to the installation with make install .

? After the installation is complete, the corresponding files will be generated under the msys/local folder. They are as follows:



? The appearance of the above files indicates that the compilation is successful and the corresponding library files are generated.

? Use the same compilation method as mingw32, only need to confirm whether the number of compiled bits corresponds.

? After all the compilation is completed, create two new folders under the working directory, namely gsl32 and gsl64 to store the files generated by compilation, which is convenient for subsequent use in the QT development process.

? In the corresponding file, put the corresponding file generated under the msys/local folder into the corresponding folder, and write a copy of gsl**.pri for reference in QT development. The directory is as follows:





? Same as in the gsl64 folder, except for gsl64.pri. The content of these two files is the same, but the name is different, and the content is shown in the figure below.

DEFINES += GSL_DLL
HEADERS +=
#Specify header file path
INCLUDEPATH + = $$PWD\include

#Indicate dependencies and dependent library paths
LIBS += -L$$PWD\lib -llibgsl
LIBS += -L$$PWD\lib -llibgslcblas
SOURCES + =

? After getting the above 2 folders, you can use them for development in QT. Create a new QT project and add the following code in the qt pro file:

contains(QT_ARCH, x86_64){<!-- -->
    message("64-bit")
    include($$PWD/gsl64/gsl64.pri)
}else{<!-- -->
    message("32-bit")
    include($$PWD/gsl32/gsl32.pri)
}


?
Then compile the project, right-click the project, and select Rebuild. Normally, the compilation should be successful, but I did not expect a compilation error. The error is as follows:

?At first I thought it was the compiler, or there was a problem with the compilation environment. The 32-bit and 64-bit were mixed up, which caused the library to not be linked. Finally, reconfiguring the environment and choosing an editor did not solve the problem. I also refer to the compilation methods of other netizens on the Internet, which are similar with minor differences. Theirs can run normally, why can’t I compile it, it feels very strange. Finally confirm that there is no error in the compilation, then look for the source code. Since the definition is not found, go to the source code to find the definitions related to _imp__gsl_rng_default and _imp__gsl_multifit_nlinear_trust, and find the definitions of these two variables in the header file, respectively in gsl_multifit_nliner and gsl_rng.h. Take gsl_rng_default in gsl_rng.h as an example: open the file, find the definition, and since the prompt cannot find it, give it extern, as shown below:

? Then rebuild and find that the errors are reduced. If there is a problem, continue to modify gsl_multifit_nlinear.h

The modified gsl_multifit_nlinear.h is as follows

?Right-click the project, rebuild, the compilation is correct, there is no red error message, and it is successfully completed. Similarly, both 32-bit and 64-bit need to be modified.

Conclusion:

?It is very strange why the one I compiled before can be used, but the one compiled again cannot be used? With doubts and the solutions that have been obtained, I opened the previous compiled library file, searched for the definitions of related variables in gsl_multifit_nliner and gsl_rng.h, and found that what the f**k is extern used, it turned out that there was already I have encountered it and solved it, and it took me another 2 days to find the problem. Make a record here, and also remind yourself that you must have the habit of taking notes, so that you can refer to it to solve the same type of problems later, which will save a lot of time.

Points download: generated mingw64 and mingw32 library files and pri files

refer to:
Windows system Qt5/mingw-64 configuration GSL scientific computing library
Configure GSL math library in QT5 environment