在C ++中绑定C函数(未定义的引用)

时间:2021-04-21 20:23:03

I have a regular C++ class like PardisoSolver.h:

我有像PardisoSolver.h这样的常规C ++类:

#ifndef PARDISOSOLVER_H_
#define PARDISOSOLVER_H_

class PardisoSolver {
public:
    /* Initializes a new Solver with the given matrix in CSR */
    PardisoSolver(
            int* ia,
            int* ja,
            double* a,
            int n,
            int nja);
    virtual ~PardisoSolver();
    void setMatrixType(int mtype);
    void pardisoSymFactorize();

private:
    static int get_nproc(void);
    void handle_error(int ierror, int line);

    int* ia;        // row indices
    int* ja;        // column pointer
    double* a;      // non zero values
    int n_eq;       // size of matrix
    int nja;        // number of non zero elements

    bool factorized;

    /* PARDISO SETTINGS */
    void *pt[64];
    int maxfct;
    int mnum;
    int mtype;
    int perm;
    int nrhs;
    int iparm[64];
    int msglvl;
    int error;
    double   dparm[64];

    double dzero;
    int izero;

};

#endif /* PARDISOSOLVER_H_ */

And on the other hand I have the implementations in PardisoSolver.cpp. Here I have an additional declaration of a C function just along with the implementations of the class:

另一方面,我在PardisoSolver.cpp中有实现。在这里,我还有一个C函数的附加声明以及该类的实现:

extern "C" void pardiso     (void   *, int    *,   int *, int *,    int *, int *,
              double *, int    *,    int *, int *,   int *, int *,
                 int *, double *, double *, int *, double *);

Now when I try to call that function like

现在当我尝试调用那个函数时

pardiso (pt, &maxfct, &mnum, &mtype, &phase,
       &n_eq, a, ia, ja, &izero, &nrhs,
       iparm, &msglvl, &dzero, &dzero, &error,  dparm);

I get the compile error

我得到编译错误

PardisoSolver.cpp:94: undefined reference to `pardiso'

where the call is in line 94. Did I declare the C function in the wrong place? As I got it, it cannot be a member of a class so this should be the way to call it. Thanks for the help.

调用在第94行。我是否在错误的地方声明了C函数?当我得到它时,它不能成为一个类的成员,所以这应该是调用它的方式。谢谢您的帮助。

1 个解决方案

#1


0  

All of this suggest that your program design needs improvment. First of all, there should only be one interface to your "module" (class + other stuff). You should not have "a class, and then some", that doesn't make any sense. All functions related to the functionality of the class should be included in that class. All that aren't related, should be moved to another part of the program.

所有这些都表明您的程序设计需要改进。首先,你的“模块”(类+其他东西)应该只有一个接口。你不应该有“一个阶级,然后一些”,这没有任何意义。与该类功能相关的所有功能都应包含在该类中。所有不相关的内容都应该移到程序的另一部分。

Once you have straightened that out, you need to clarify the programming language. Is this C or C++? Is it C++ with a caller in C? It seems to be the latter. If so, you need to separate the two languages somehow. As one example: in Windows you could place the C++ class in a DLL, with an interface that can be called from C.

一旦你理顺了这一点,就需要澄清编程语言。这是C还是C ++? C中是否有C调用者?它似乎是后者。如果是这样,你需要以某种方式分离这两种语言。作为一个例子:在Windows中,您可以将C ++类放在DLL中,其接口可以从C调用。

And finally, you should consider whether it makes sense to have a function taking 17 pointers as parameters. Are these parameters not related at all? Could they be grouped in structs/classes? Or perhaps this is simply a needlessly complex monster function, that could be split in several different ones.

最后,您应该考虑使用17个指针作为参数的函数是否有意义。这些参数根本没有关系吗?它们可以分组在结构/类中吗?或许这只是一个不必要的复杂怪物功能,可以分成几个不同的功能。

#1


0  

All of this suggest that your program design needs improvment. First of all, there should only be one interface to your "module" (class + other stuff). You should not have "a class, and then some", that doesn't make any sense. All functions related to the functionality of the class should be included in that class. All that aren't related, should be moved to another part of the program.

所有这些都表明您的程序设计需要改进。首先,你的“模块”(类+其他东西)应该只有一个接口。你不应该有“一个阶级,然后一些”,这没有任何意义。与该类功能相关的所有功能都应包含在该类中。所有不相关的内容都应该移到程序的另一部分。

Once you have straightened that out, you need to clarify the programming language. Is this C or C++? Is it C++ with a caller in C? It seems to be the latter. If so, you need to separate the two languages somehow. As one example: in Windows you could place the C++ class in a DLL, with an interface that can be called from C.

一旦你理顺了这一点,就需要澄清编程语言。这是C还是C ++? C中是否有C调用者?它似乎是后者。如果是这样,你需要以某种方式分离这两种语言。作为一个例子:在Windows中,您可以将C ++类放在DLL中,其接口可以从C调用。

And finally, you should consider whether it makes sense to have a function taking 17 pointers as parameters. Are these parameters not related at all? Could they be grouped in structs/classes? Or perhaps this is simply a needlessly complex monster function, that could be split in several different ones.

最后,您应该考虑使用17个指针作为参数的函数是否有意义。这些参数根本没有关系吗?它们可以分组在结构/类中吗?或许这只是一个不必要的复杂怪物功能,可以分成几个不同的功能。