重复符号链接器错误(C++ 帮助)

发布于 2024-09-03 15:04:15 字数 1107 浏览 3 评论 0原文

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

我的程序编译得很好,但是当它链接文件时,我收到 XMLParser_libxml2.hh 文件的重复符号错误。我的文件是这样分隔的:

包含上面 XMLParser 文件的类头文件
包含类头文件的类实现文件
包含类头文件的主文件

重复的符号出现在 main.o 和 classfile.o 中,但据我所知,我实际上并没有两次添加该 .hh 文件。

完整错误:

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`

将类的实现复制到主文件中并将类实现文件从编译目标中取出可以消除错误,但这样会造成混乱,我很快就会添加更多的类(并且它会最好将它们放在单独的文件中)。

据我了解,这是由文件(XMLParser_libxml2.hh)在一个文件中同时具有类和函数定义和实现引起的(并且由于在那个“头”文件)。关于如何将所有类文件粘贴到 main.cpp 中,有什么想法吗? (我已经尝试过#ifdefs,但它们不起作用)。

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.

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:

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

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.

Full error:

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).

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-09-10 15:04:15

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文