Using GSL (GNU scientific Library) in the Visual Stdio environment

Source: Internet
Author: User
Tags deprecated hypot

Using GSL (GNU scientific Library) in the Visual Stdio environment

The GNU Scientific Library (GSL) is a library of open-source scientific computing functions that are very powerful. There are a lot of articles on the Internet, and GSL's documentation is very good, which is a very easy-to-get-started function library. There's not much to GSL about.

Today's talk about how to use this library in a Visual stdio environment. In fact, this aspect of the content of the internet there are some. However, most of the methods used are not very good. Some are directly downloaded GSL for widows to use, but this GSL for widows is 2006 GSL 1.8, antique-grade version, this version lacks too many features.

Another approach is to transform the generated DLLs that are compiled by MinGW. In fact, different C compiler compiler generated DLL as long as adhere to the same C calling Conven, in principle, can be universal. However, there are differences in the library functions that are available from different compilers. For example, the code in the GSL library often uses the Hypot function, which is a function in the standard math library in the MinGW toolchain. Can be used directly, but this function is not available in the Visual stdio math library. Therefore, the direct use of MinGW GSL is not possible, will be encountered in the link phase of the lack of definitions of various functions of the problem.

Therefore, if you want to use GSL in a Visual Stdio environment, the best way is to recompile the GSL code. In short, this is divided into two different roads.

    • Compiling GSL using the C compiler in Visual Stdio
    • Compiling GSL using the C compiler in MinGW

The first of these methods is relatively troublesome. Need to build a Visual Stdio project, add the relevant code to the project, but also need to modify the individual GSL code (mainly because the Visual Stdio C compiler does not support the inline feature in the C99 standard, the GSL code is not serious enough to consider this Problem). In addition, the compiler generated DLL for Visual Stdio needs to specify the export function through Def file, this def file needs to be generated by ourselves, not given in the source code of GSL. Because this method is cumbersome, the likelihood of failure is very high for the novice, so I am going to introduce another method today.

Compiling GSL using the C compiler in MinGW

Familiar with the Linux software compiled by the students must know that this source code compilation installation is the standard three steps:

    • ./configure
    • Make
    • Make install

We compile GSL naturally also can not get rid of this process.

Of course we need to have MinGW tool chain and MSYS environment before this. Here I suggest you use MSYS2, about the installation of MSYS2, you can refer to a short article I wrote:
http://blog.csdn.net/liyuanbhu/article/details/39397931

After the toolchain is complete, unzip the gsl-1.16.tar.gz into the MSYS2 home directory, followed by the./configure. The process will be longer and may take 3 or 5 minutes.

After that, there will be config.h files in the root directory of the gsl-1.16. We need to revamp this file. The reason for this transformation is that this file is prepared for the MinGW gcc toolchain and is not suitable for Visual Stdio development tools.

The contents of this config.h file are described in detail below.

The beginning of the Config.h 25 line is this.

/* config.h.  Generated from config.h.in by configure.  *//* config.h.in.  Generated from configure.ac by autoheader.  *//* Disable deprecated functions and enums while building */#define GSL_DISABLE_DEPRECATED 1/* Define if you have inline with C99 behavior */#define HAVE_C99_INLINE 1/* Define to 1 if you have the declaration of `acosh‘, and to 0 if you don‘t.   */#define HAVE_DECL_ACOSH 1/* Define to 1 if you have the declaration of `asinh‘, and to 0 if you don‘t.   */#define HAVE_DECL_ASINH 1/* Define to 1 if you have the declaration of `atanh‘, and to 0 if you don‘t.   */#define HAVE_DECL_ATANH 1/* Define to 1 if you have the declaration of `expm1‘, and to 0 if you don‘t.   */#define HAVE_DECL_EXPM1 1

#define Have_c99_inline 1 indicates that the compiler supports C99, although the VS compiler does not support C99, but we can still keep this sentence. Because GSL actually uses only the C99 inline, the other features are useless. The VS compiler actually supports inline. We'll talk about this inline support in detail later.

Acosh, Asinh, Atanh, EXPM1 are not supported in the standard library of four function VS. So change these four 1 to 0. Students who like to ask questions may wonder if the lack of these functions in VS will affect the functionality of GSL. The answer is, of course, negative. These functions, which are not part of the C language standard, are implemented by themselves, some in the SYS subdirectory, some in the Utils directory, and some in other directories, GSL.

The GSL implementation, however, adds a GSL prefix to the function name. For example, the EXPM1 function, implemented in GSL, is:

double gsl_expm1 (const double x)

If the CONFIG.H is configured as:

#define HAVE_DECL_EXPM1 0

The GSL_EXPM1 function will be called where the EXPM1 function is useful in GSL.

Other areas that need to be changed include:

#define HAVE_DECL_FINITE 0#define HAVE_DECL_FREXP 0#define HAVE_DECL_HYPOT 0#define HAVE_DECL_ISFINITE 0#define HAVE_DECL_ISINF 0#define HAVE_DECL_ISNAN 0#define HAVE_DECL_LDEXP 0#define HAVE_DECL_LOG1P 0#define HAVE_IEEEFP_H 0#define HAVE_IEEE_DENORMALS 0#define HAVE_STRDUP 0#define HAVE_STRINGS_H 0#define HAVE_UNISTD_H 0

Where the Hypot function is actually there, but according to Microsoft's saying:

This POSIX function was deprecated beginning in Visual C + + 2005.

So we should still not use it.

With the Config.h file we can proceed to the next step.

    • Make

After a long wait. We have all the files we need after the compilation is complete.

The following files can be found in the Gsl-1.16\cblas.libs directory:

    • Libgslcblas.a
    • Libgslcblas.dll.a
    • Libgslcblas-0.dll

The following files can be found in the Gsl-1.16.libs directory:

    • Libgsl.a
    • Libgsl.dll.a
    • Libgsl-0.dll

Copy all these files and rename the. A file to. lib.

Header files in the GSL-1.16\GSL directory, this directory can be fully copied out.

We can create a directory named Gsl-1.16-vs.
Then create three subdirectories, respectively:

    • Include
    • Lib
    • Bin

Copy the. lib file from the Include directory where the header file resides in the GSL directory to the Lib directory where the. dll file is copied to the Bin directory.

At this point, our work has been completed in half. You can try our generated Lib file and DLL file, specifically how to use the third-party library in VS, online introduction of the article there are many, I will not say much.

The first example in the GSL Help file:

#include <stdio.h>#include <gsl/gsl_sf_bessel.h>int main(int argc, char *argv[]){    double x = 5.0;    double y = gsl_sf_bessel_J0 (x);    printf ("J0(%g) = %.18e\n", x, y);    return 0;}

This example can now be compiled and executed smoothly, and the results are also correct.

But then we couldn't be happy too early. Visual Stdio gave us a big hole waiting for us to jump in. This big pit is the VS compiler does not support C99, and of course includes inline that does not support C99.
As a result, many errors are reported when compiling the code below.

#define Have_inline 1#include "stdafx.h"#include <stdio.h>#include <gsl/gsl_vector.h>    int_tmain (intARGC, _tchar* argv[]) {intI Gsl_vector * v = gsl_vector_alloc (Ten); for(i =0; I <Ten; i++) {Gsl_vector_set (V, I,1.23+ i); } for(i =0; I <Ten; i++) {printf ("v_%d =%g\n", I, Gsl_vector_get (V, i));        } gsl_vector_free (v); GetChar ();return 0; }

The main errors of the report are:

Error C2054: After "inline" you should enter "("

To solve this problem, we need to modify gsl_inline.h this file. This file has the following lines.

#ifdef HAVE_INLINE#  if defined(__GNUC_STDC_INLINE__) || defined(GSL_C99_INLINE) || defined(HAVE_C99_INLINE)#    define INLINE_DECL inline  /* use C99 inline */#    define INLINE_FUN inline#  else#    define INLINE_DECL         /* use GNU extern inline */#    define INLINE_FUN extern inline#  endif#else#  define INLINE_DECL /* */#endif

Modify it to:

#ifdef HAVE_INLINE#  if defined(__GNUC_STDC_INLINE__) || defined(GSL_C99_INLINE) || defined(HAVE_C99_INLINE)#    define INLINE_DECL inline /* use C99 inline */#    define INLINE_FUN inline#  else#    define INLINE_DECL         /* use GNU extern inline */#    define INLINE_FUN __inline#  endif#else#  define INLINE_DECL /* */#endif

There is no error in compiling this way.

I will compile the GSL into the following link, there is a need to download directly.
http://download.csdn.net/detail/liyuanbhu/9009755

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using GSL (GNU scientific Library) in the Visual Stdio environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.