混合 C 和 C++ 代码时出现链接错误

linking error when mixing c and c++ code

本文关键字:链接 错误 代码 C++ 混合      更新时间:2023-10-16

我有来自这里示例的 5 个文件 - Dec.h、Dec.cpp、decInterface.h、decInterface.cpp 和 temp.c

如何在 c 中使用 c++ 对象?和为面向对象C++代码开发 C 包装器 API在一个名为 temp.c 的文件中,该文件调用在接口文件的帮助下实现的 12 月实现的 CPP 代码.cpp。

它们都驻留在 vs2008 中的一个项目中,我将编译为选项设置为默认值,而不是编译为 c 或编译为 c++

我收到以下链接错误

    1>Compiling...
    1>decInterface.cpp
    1>Generating Code...
    1>Compiling...
    1>temp.c
    1>Generating Code...
    1>Compiling manifest to resources...
    1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
    1>Copyright (C) Microsoft Corporation.  All rights reserved.
    1>Linking...
        1>decInterface.obj : error LNK2005: "public: __thiscall Node::Node(class std::vector<float,class std::allocator<float> >)" (??0Node@@QAE@V?$vector@MV?$allocator@M@std@@@std@@@Z) already defined in Dec.obj
        1>decInterface.obj : error LNK2005: "public: __thiscall Node::Node(float,float)" (??0Node@@QAE@MM@Z) already defined in Dec.obj
        1>decInterface.obj : error LNK2005: "public: __thiscall Node::Node(class std::vector<float,class std::allocator<float> >,class std::vector<bool,class std::allocator<bool> >)" (??0Node@@QAE@V?$vector@MV?$allocator@M@std@@@std@@V?$vector@_NV?$allocator@_N@std@@@2@@Z) already defined in Dec.obj
        1>decInterface.obj : error LNK2005: "public: __thiscall reprVectorsTree::reprVectorsTree(class std::vector<class Node *,class std::allocator<class Node *> >,int)" (??0reprVectorsTree@@QAE@V?$vector@PAVNode@@V?$allocator@PAVNode@@@std@@@std@@H@Z) already defined in Dec.obj
        1>decInterface.obj : error LNK2005: "public: __thiscall reprVectorsT
ree::reprVectorsTree(float * *,int,int)" (??0reprVectorsTree@@QAE@PAPAMHH@Z) already defined in Dec.obj
    1>decInterface.obj : error LNK2005: "private: class std::vector<bool,class std::allocator<bool> > __thiscall reprVectorsTree::binaryForm(int,int)" (?binaryForm@reprVectorsTree@@AAE?AV?$vector@_NV?$allocator@_N@std@@@std@@HH@Z) already defined in Dec.obj
    1>decInterface.obj : error LNK2005: "public: class std::vector<float,class std::allocator<float> > __thiscall reprVectorsTree::decode(class std::vector<bool,class std::allocator<bool> >)" (?decode@reprVectorsTree@@QAE?AV?$vector@MV?$allocator@M@std@@@std@@V?$vector@_NV?$allocator@_N@std@@@3@@Z) already defined in Dec.obj
    1>decInterface.obj : error LNK2005: "public: float * __thiscall reprVectorsTree::decode(int *,int)" (?decode@reprVe
ctorsTree@@QAEPAMPAHH@Z) already defined in Dec.obj

我应该使用哪种类型的 Visual Studio 项目设置?我觉得问题出在这里?要链接哪些文件?如何链接?或者默认项目设置应该有效?

错误消息

告诉您在Dec.cppdecInterface.cpp中或在两个源文件都包含的标头中定义了多个函数。通常,只允许在程序中对函数进行单个定义;这是(一个定义规则的一个方面)。

如果定义在两个源文件中,则从其中一个源文件中删除它们。

如果它们在标题中,那么您可以选择。您可以将定义移动到一个(且仅一个)源文件中,只在标头中保留声明:

// header
class Node {
public:
    Node(std::vector<float>); // declaration
};
// source
Node::Node(std::vector<float>) {
    // definition
}

或者,您可以在标头中inline定义它们。这会放宽规则以允许多个相同的定义:

// header
class Node {
public:
    Node(std::vector<float>); // declaration
};
inline Node::Node(std::vector<float>) {
    // inline definition
}

或者你可以在类定义中定义它们;这也使它们内联:

// header
class Node {
public:
    Node(std::vector<float>) {
        // inline definition
    }
};