混合 C 和 C++ 代码时出现链接错误
linking error when mixing c and c++ code
我有来自这里示例的 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.cpp
和decInterface.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
}
};
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误