我有此处示例中的 5 个文件 - Dec.h、Dec.cpp、decInterface.h、decInterface.cpp 和 temp.c
How to use c++ objects in c? 和 Developing C wrapper API for Object-Oriented C++ code main 在一个名为 temp.c 的文件中,它在接口(interface)文件的帮助下调用在 Dec.cpp 中实现的 cpp 代码。
它们都驻留在vs2008中的一个项目中,我将compile as选项设置为默认而不是compile as c或compile as 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.cpp
和 decInterface.cpp
中或在两个源文件包含的 header 中都定义了多个函数。通常,您只能在一个程序中定义一个函数;这是一个定义规则的(一个方面)。
如果定义在两个源文件中,则从其中一个文件中删除它们。
如果它们在标题中,那么您可以选择。您可以将定义移动到一个(并且只有一个)源文件中,只在 header 中保留声明:
// header
class Node {
public:
Node(std::vector<float>); // declaration
};
// source
Node::Node(std::vector<float>) {
// definition
}
或者您可以在 header 中定义它们 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
}
};
关于c++ - 混合 C 和 C++ 代码时出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12001423/