我在使用 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/