为什么在这种情况下编译动态库错误?

时间:2019-03-14 14:45:36

标签: c++ linux .so

我在同一路径上写了两个文件。 第一个文件名为“ dso.h”。

//dso.cpp
#ifdef DSO_H
#define DSO_H

#include <string>
#include <map>

#include "spider.h"

using namespace std;

#define MODULE_OK  0
#define MODULE_ERR 1

#define MAGIC_MAJOR_NUMBER 1
#define MAGIC_MINOR_NUMBER 0

#define STANDARD_MODULE_STUFF MAGIC_MAJOR_NUMBER, MAGIC_MINOR_NUMBER,__FILE__


typedef struct Module{
        int version;
        int minor_version;
        const char * name;
        void (*init)(Module *);
        int (*handle)(void *);
} MODULE;


class ModuleManager
{
        public:
                ModuleManager();
                ~ModuleManager();

                int load(string path, string name);  
                MODULE * getModule(string name);

        private:
                map<string, MODULE*> m_modules; 
};


#endif

第二个文件名为“ testmodule.cpp”

#include "dso.h"
#include <stdio.h>

extern int handle(void * data){
        printf("%s", (char *)data);
        return MODULE_OK;
}

extern int init(Module * module){
        module = &module;
        return MODULE_OK;
}


Module mod
{
        MODULE_MAJOR_VERSION,
        MODULE_SUB_VERSION,
        __FILE__,
        init,
        handle,
};

我尝试运行此命令:

  

g ++ -shared -fPIC -o testmodule.so testmodule.cpp

运行该命令后,出现一些错误,请参见下文

testmodule.cpp: In function ‘int handle(void*)’:
testmodule.cpp:6:10: error: ‘MODULE_OK’ was not declared in this scope
   return MODULE_OK;
          ^~~~~~~~~
testmodule.cpp: At global scope:
testmodule.cpp:9:17: warning: ‘init’ initialized and declared ‘extern’
 extern int init(Module * module){
                 ^~~~~~
testmodule.cpp:9:17: error: ‘Module’ was not declared in this scope
testmodule.cpp:9:17: note: suggested alternative: ‘double’
 extern int init(Module * module){
                 ^~~~~~
                 double
testmodule.cpp:9:26: error: ‘module’ was not declared in this scope
 extern int init(Module * module){
                          ^~~~~~
testmodule.cpp:9:26: note: suggested alternative: ‘double’
 extern int init(Module * module){
                          ^~~~~~
                          double
testmodule.cpp:15:1: error: ‘Module’ does not name a type; did you mean ‘double’?
 Module mod
 ^~~~~~
 double

我无法获得动态库,如何解决该问题?

在ubuntu18.04TLS linux中运行以上命令。

1 个答案:

答案 0 :(得分:0)

您似乎混淆了dso.h和dso.cpp。 假设应该是dso.h ...

顶部的include-guard错误。

#ifdef DSO_H
#define DSO_H

...应该是

#ifndef DSO_H
#define DSO_H

否则标题将不会被解析!

相关问题