簡體   English   中英

Windows 上的 GSL 鏈接

[英]GSL linkage on Windows

我正在嘗試從 GSL 文檔中編譯示例。 Windows,Cmake + MSVS + GSL 2.6 安裝在 conda 下。

基本示例被編譯並且工作得很好:

cmake_minimum_required(VERSION 3.15)
project(test)

find_package(GSL REQUIRED)

add_executable(test)
target_sources(test PRIVATE main.cpp)
target_include_directories(test PRIVATE "${GSL_INCLUDE_DIRS}")
target_link_libraries(test "${GSL_LIBRARIES}")
#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main (void)
{
    double x = 5.0;
    double y = gsl_sf_bessel_J0 (x);
    printf ("J0(%g) = %.18e\n", x, y);
    return 0;
}

但是這個失敗並出現鏈接錯誤:

error LNK2001: unresolved external symbol gsl_odeiv2_step_rk8pd

#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv2.h>

int
func (double t, const double y[], double f[],
      void *params)
{
    (void)(t); /* avoid unused parameter warning */
    double mu = *(double *)params;
    f[0] = y[1];
    f[1] = -y[0] - mu*y[1]*(y[0]*y[0] - 1);
    return GSL_SUCCESS;
}

int
jac (double t, const double y[], double *dfdy,
     double dfdt[], void *params)
{
    (void)(t); /* avoid unused parameter warning */
    double mu = *(double *)params;
    gsl_matrix_view dfdy_mat
            = gsl_matrix_view_array (dfdy, 2, 2);
    gsl_matrix * m = &dfdy_mat.matrix;
    gsl_matrix_set (m, 0, 0, 0.0);
    gsl_matrix_set (m, 0, 1, 1.0);
    gsl_matrix_set (m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0);
    gsl_matrix_set (m, 1, 1, -mu*(y[0]*y[0] - 1.0));
    dfdt[0] = 0.0;
    dfdt[1] = 0.0;
    return GSL_SUCCESS;
}

int
main (void)
{
    double mu = 10;
    gsl_odeiv2_system sys = {func, jac, 2, &mu};

    gsl_odeiv2_driver * d =
            gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd,
                                           1e-6, 1e-6, 0.0);
    int i;
    double t = 0.0, t1 = 100.0;
    double y[2] = { 1.0, 0.0 };

    for (i = 1; i <= 100; i++)
    {
        double ti = i * t1 / 100.0;
        int status = gsl_odeiv2_driver_apply (d, &t, ti, y);

        if (status != GSL_SUCCESS)
        {
            printf ("error, return value=%d\n", status);
            break;
        }

        printf ("%.5e %.5e %.5e\n", t, y[0], y[1]);
    }

    gsl_odeiv2_driver_free (d);
    return 0;
}

gsl_odeiv2_step_rk8pd在 GSL 中的某處定義如下: GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rk8pd; ,其中GSL_VAR表示外部。

我錯過了什么?

GSL 和您的應用程序的二進制兼容性必須相同:只有 Win64 buld GSL 庫項目可以與 Win64 應用程序一起使用(或 Win32 GSL 庫分別與 Win32 應用程序一起使用),也可以在一種模式下構建您的項目和 GSL(調試/發布)和一個編譯器。

您需要使用 vcpkg 重新編譯 gsl 源代碼。 vcpkg 可以使用 CMake 自動編譯 gsl。 請谷歌vcpkg。 這很容易,沒有錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM