gpt4 book ai didi

c - 共享标志的重定位编译问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:34 26 4
gpt4 key购买 nike

我有一个 C 语言程序,它使用 GSL 集成 ODE 系统,并将结果数据存储在数组中。代码在问题的末尾,但正如您将看到的那样,这并不是真正的问题所在。

除了 C 代码之外,我还使用 Python 脚本使用 ctypes 将数组指针传递到 C 代码中,因为我稍后想要绘制和操作数组数据。所有这些都很好,它适用于小型测试程序。但为此,我需要使用 gcc 创建一个共享库。这就是问题所在。

当我用

编译时
    gcc ctest.c -o ctest.o  -std=c11 -Wall -g -lgsl -lgslcblas -lm

代码运行良好。我有一个主要功能,可以复制用于测试的 Python 脚本,并且没有任何中断。仅供引用,-lgsl-lgslcblas 标志用于使链接器停止提示缺少 GSL 声明。但是当我尝试使用

创建共享库时
    gcc ctest.c -o ctest.o  -std=c11 -Wall -g -lgsl -lgslcblas -lm -fPIC -Wl,-shared,-soname,ctest.so

gcc 吐出这个错误:

    /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0xe): undefined reference to `__init_array_start'
/usr/bin/x86_64-linux-gnu-ld: /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS): relocation R_X86_64_PC32 against undefined hidden symbol `__init_array_start' can not be used when making a shared object
/usr/bin/x86_64-linux-gnu-ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

这让我很困惑。我在网上四处看看,这似乎是对象初始化的问题,这对我来说仍然很奇怪,因为我只使用内置类型来创建数组,除非它是 GSL 搞砸了。

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

#define pi M_PI

//#define HAVE_INLINE

typedef struct funcParams {
double I;
double J;
double s;
} funcParams;

int diff (double t, const double y[], double dy[], void * params) {

#define I() IJs.I
#define J() IJs.J
#define s() IJs.s

#define w1 y[0]
#define w2 y[1]
#define w3 y[2]
#define e1 y[3]
#define e2 y[4]
#define e3 y[5]
#define e4 y[6]

t = 0;

funcParams IJs = *(funcParams*) params;

dy[0] = 2*pi * ( (1-J() /I() )*( 6*(1-2*e1*e1-2*e2*e2)*(e2*e3+e1*e4) - w2*w3) + w3*s() );
dy[1] = 0;
dy[2] = 2*pi * ( (J() /I() -1)*(12*(e1*e3-e2*e4)*(e2*e3+e1*e4) - w1*w2) - w1*s() );
dy[3] = pi * (w3*e2 + w1*e4 - e3*(w2-s()+1));
dy[4] = pi * (w1*e3 - w3*e1 + e4*(w2-s()-1));
dy[5] = pi * (-w1*e2 + w3*e4 + e1*(w2-s()+1));
dy[6] = pi * (-w1*e1 - w3*e3 - e2*(w2-s()-1));

return GSL_SUCCESS;
}

void quat2C( double* e, double C[3][3]) {

#undef e1
#undef e2
#undef e3
#undef e4

#define e1 e[0]
#define e2 e[1]
#define e3 e[2]
#define e4 e[3]

C[0][0] = 1 - 2*e2*e2 - 2*e3*e3;
C[0][1] = 2 * (e1*e2 - e3*e4);
C[0][2] = 2 * (e3*e1 + e2*e4);
C[1][0] = 2 * (e1*e2 + e3*e4);
C[1][1] = 1 - 2 * e3*e3 - 2 * e1*e1;
C[1][2] = 2 * (e2*e3 - e1*e4);
C[2][0] = 2 * (e3*e1 - e2*e4);
C[2][1] = 2 * (e2*e3 + e1*e4);
C[2][2] = 1 - 2 * e1*e1 - 2 * e2*e2;

return;
}



void timeHistories (double nut0, double x, double vf, int N, double I, double J, double s, double* states, double * nut, double* gamma, double* beta, double* dLambda) {

funcParams IJs = {I, J, s};
gsl_odeiv2_system sys = {diff, NULL, 7, &IJs};
gsl_odeiv2_driver* d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rkf45, 1e-6, 1e-6, 0.0);

double* states_i = (double*)malloc(7*sizeof(double));
states_i[0] = 0;
states_i[1] = x;
states_i[2] = 0;
states_i[3] = sin(nut0*pi/360);
states_i[4] = 0;
states_i[5] = 0;
states_i[6] = cos(nut0*pi/360);
double v = 0.0;

double C[3][3];

/* retrieve the states at each time. This allows us to modify each of the
"returned" arrays in the same loop */
for (int i = 0; i < N; ++i) {

double vi = i*vf/N; // current revolution value
printf("vi got\n");
gsl_odeiv2_driver_apply(d, &v, vi, states_i); // get states
printf("states got\n");
quat2C(&states_i[3],C);
printf("C got\n");

printf("%d: %f %f %f %f %f %f %f\n", i, states_i[0], states_i[1], states_i[2], states_i[3], states_i[4], states_i[5], states_i[6]);
}

return;
}

int main() {

double I = 450;
double J = 75;
int N = gsl_pow_int(2,14);

double* states = (double*)malloc(N*sizeof(double));
double* nut = (double*)malloc(N*sizeof(double));
double* gamma = (double*)malloc(N*sizeof(double));
double* beta = (double*)malloc(N*sizeof(double));
double* dLambda = (double*)malloc(sizeof(double));

double vf = 4;

double nut0 = 6;
double x = 20;
double s = 0;
timeHistories(nut0, x, vf, N, I, J, s, states, nut, gamma, beta, dLambda);

return 0;
}

最佳答案

感谢 Andrew Henle,问题已得到解决。使用

拆分编译和链接
gcc -c -std=c11 -fPIC -g -o ctest.o ctest.c && gcc -o libctest.so ctest.o -lgsl -lgslcblas -lm -shared

生成可由 Python 调用的共享可执行文件(无论如何通过 ctypes)。我不确定为什么,但是使用 -shared 与使用 -Wl,-shared 将命令传递给链接器相反。

关于c - 共享标志的重定位编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52203318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com