xcode链接器在调试模式下错误(重复符号)

xcode linker error in Debug mode (duplicate symbol)

本文关键字:符号 错误 链接 调试 模式 xcode      更新时间:2023-10-16

我在使用boost::error_info编译一段代码时遇到了问题。我有一个错误代码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没有给我任何问题。但是当我尝试在xcode中编译Debug模式时,我得到以下错误:Apple Mach-O link (old) 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)

发布模式也没有问题

通过试错,我找到了解决这个问题的方法。我需要将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;

但我不明白为什么它工作。有人能给我解释一下有什么不同吗?

相关文章: