我在同一条路径上写了两个文件。第一个名为“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中运行以上命令。
看起来你混淆了dso.h和dso.cpp。假设应该是dso.h ...
在顶部,包含防守是错误的。
#ifdef DSO_H
#define DSO_H
......应该是
#ifndef DSO_H
#define DSO_H
或者标题不会被解析!