简体   繁体   English

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

[英]Bind C function in C++ (undefined reference)

I have a regular C++ class like PardisoSolver.h: 我有一个普通的C ++类,如PardisoSolver.h:

#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. 另一方面,我在PardisoSolver.cpp中有实现。 Here I have an additional declaration of a C function just along with the implementations of the class: 在这里,我还有一个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? 调用在第94行。我是否在错误的地方声明了C函数? 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. 谢谢您的帮助。

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++? 这是C还是C ++? Is it C++ with a caller in C? 在C中使用调用者是否使用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. 举一个例子:在Windows中,您可以将C ++类放置在DLL中,并带有可以从C调用的接口。

And finally, you should consider whether it makes sense to have a function taking 17 pointers as parameters. 最后,您应该考虑使用17个指针作为参数的函数是否有意义。 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. 又或者,这仅仅是一个不必要的复杂怪物函数,可以分解为几个不同的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM