c++ - Debug模式下的 xcode 链接器错误(重复符号)

标签 c++ xcode boost

我在使用 boost::error_info 编译一段代码时遇到了问题。我有一个包含错误代码定义的 header errors.h

#ifndef ERRORS_H
#define ERRORS_H

#include <boost/exception/all.hpp>
#include <string>

enum error_num {
    ERR_IS_NOT_ZERO,
    ERR_IS_ZERO
};

typedef boost::error_info<struct tag_errno_code,error_num> errno_code;
typedef boost::error_info<struct tag_code_line,int> code_line;
typedef boost::error_info<struct tag_err_description,std::string> err_description;

struct exception_base: virtual std::exception, virtual boost::exception { };
struct integral_error: virtual exception_base { };
struct geometry_error: virtual exception_base { };

std::string error_to_str(error_num err);

#endif  /* ERRORS_H */

gcc 没有给我任何问题。但是当我尝试在 Debug 模式下在 xcode 中编译时,出现以下错误:Apple Mach-O Linker (ld) Error

duplicate symbol __ZTSP13tag_code_line in:
....
duplicate symbol __ZTSP19tag_err_description in:
....
duplicate symbol __ZTSP13tag_code_line in:
....
....
ld: 6 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

而且在Release模式下没有问题。

最佳答案

通过试错,我找到了解决这个问题的方法。我需要将 struct tag_... 带出 boost::error_info 模板参数:

struct tag_errno_code { };
struct tag_code_line { };
struct tag_err_description { };
typedef boost::error_info<tag_errno_code,error_num> errno_code;
typedef boost::error_info<tag_code_line,int> code_line;
typedef boost::error_info<tag_err_description,std::string> err_description;

但我不明白为什么会这样。有人可以向我解释这里有什么区别吗?

关于c++ - Debug模式下的 xcode 链接器错误(重复符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24044047/

相关文章:

c++ - 指针和左值引用

c++ - 删除边后保留图连通分量数的动态图算法的实现

c++ - Windows:如何获取完整用户的帐户

c++ - 为什么 boost::barrier wait 不是中断点?

c++ - 如何重载operator<<以与boost::program_options默认值输出一起使用?

c++ - 获取sqlite3中的列名

xcode - UISearchController 不会停留在表头 View 的顶部

ios - 启动时 ios 模拟器中的空白/黑屏

ios - 为什么观点没有改变? Xcode/swift 2

c++ - 将未命名节点添加到 boost::property_tree::ptree