重复符号链接器错误(C ++帮助) [英] Duplicate Symbol Linker Error (C++ help)

查看:178
本文介绍了重复符号链接器错误(C ++帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习一些CSP(约束满足)理论的东西,我使用这个库来解析XML文件。我使用Xcode作为IDE。

I'm learning some CSP (constraint satisfaction) theory stuff right now, and am using this library to parse XML files. I'm using Xcode as an IDE.

我的程序编译正常,但当它链接文件,我得到一个重复的符号错误与XMLParser_libxml2.hh文件。我的文件如此分隔:

My program compiles fine, but when it goes to link the files, I get a duplicate symbol error with the XMLParser_libxml2.hh file. My files are separated as such:

包含上述XMLParser文件的类头文件

包含类头文件的类实现文件< br>
包含类头文件的主文件

A class header file that includes the XMLParser file above
A class implementation file that include the class header file
A main file that includes the class header file

重复的符号出现在main.o和classfile.o中,告诉,我实际上没有添加.hh文件两次。

The duplicate symbol is occurring in main.o and classfile.o, but as far as I can tell, I'm not actually adding that .hh file twice.

完整错误:

ld: duplicate symbol bool CSPXMLParser::UTF8String::to<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&) constin
/Users/vash265/CSP/Untitled/build/Untitled.build/Debug/Untitled.build/Objects-
normal/x86_64/dStructFill.o and
/Users/vash265/CSP/Untitled/build/Untitled.build/Debug/Untitled.build/Objects-
normal/x86_64/main.o`

将类的实现复制到主文件,并将类实现文件从编译目标中移除错误,但是这是一个混乱的混乱,这种方式,我会很快添加更多的类(而且它是很好,它们在不同的文件)。

Copying the implementation of the class into the main file and taking the class implementation file out of the compilation target removes the error, but it's a disorganized mess this way, and I'll be adding more classes very soon (and it would be nice to have them in separate files).

正如我已经理解它,这是由文件(XMLParser_libxml2.hh)同时具有类和函数定义和实现在一个文件好像这可能是必要的,因为在'header'文件中使用模板)。任何想法如何绕过粘在我的main.cpp中的所有类文件? (我试过 #ifdefs ,他们不工作)。

As I've come to understand it, this is caused by the file (XMLParser_libxml2.hh) having both the class and function definition and implementation in one file (and it seems as though this might have been necessary due to the use of templates in that 'header' file). Any ideas on how to get around sticking all my class files in my main.cpp? (I've tried #ifdefs, they don't work).

推荐答案

詹姆斯是对的。头文件中的三个模板函数需要内联声明才能正确链接。谢谢!

James was correct. The three template functions inside the header file needed to be declared inline for it to link properly. Thanks!

这篇关于重复符号链接器错误(C ++帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆