【问题标题】:C++ with Extern "C" causing Duplicate Symbols Error带有外部“C”的 C++ 导致重复符号错误
【发布时间】:2012-09-23 04:39:59
【问题描述】:

我正在尝试在我的头文件中为 c++ 类使用外部“C”函数。

当我编译时,我不断收到错误

duplicate symbol _currentInstance in:
main.o
GLHandler.o

我以为我有合适的守卫,但似乎无法弄清楚为什么会发生这种情况。任何帮助将不胜感激。

这是头文件。

#ifndef GLHANDLER_H
#define GLHANDLER_H

#include "LoadedObject.h"


#ifdef __cplusplus
extern "C" {
void displayCallback();
}
#endif



class GLHandler {

private:
    LoadedObject *object;

public:
    GLHandler(LoadedObject *);
    void initializeVBO(LoadedObject *);
    void renderObject(struct model *);
    void displayFunction(void);
    model *createModel(void);
    void setupDisplayCallback();


};

GLHandler *currentInstance;

#ifdef __cplusplus
}

#endif

#endif

编辑:David 很快指出,extern GLHandler *currentInstance 修复了错误。

【问题讨论】:

  • 也许应该是extern GLHandler *currentInstance;
  • 你肯定在 main.c 和 GLHandler.c 中实现了这个函数。或者你把它放到一个头文件中而不是内联它。

标签: c++ extern include-guards


【解决方案1】:

这个问题与extern "C" 声明无关——你在头文件中定义了一个全局变量,所以它在每个编译单元中都被定义:

GLHandler *currentInstance;

在标题中,您应该改用:

extern GLHandler *currentInstance;

那么在正好一个.cpp文件中有:

GLHandler *currentInstance;

作为旁注,就目前而言,标头仅对 C++ 有效,因为它具有类定义。 #ifdef __cplusplus 指令是毫无意义的混乱(尽管无害)。

【讨论】:

    猜你喜欢
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多