gpt4 book ai didi

c++ - 使用 libc++ 在 Mac Yosemite 上访问 std::string 成员时出现链接错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:40 25 4
gpt4 key购买 nike

我正在尝试编译此类代码,但在尝试在 MacOSX Yosemite 上使用最新的 Xcode (6.0) 构建时出现链接错误:

#include <iostream>
#include <string>

int main()
{
auto x = &std::string::size;
std::string hello("hello");
std::cout << (hello.*x)() << std::endl;
return 0;
}

$ g++ --std=c++11 -stdlib=libc++ hello.cpp -o hello

KO:

Undefined symbols for architecture x86_64:

"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::size() const", referenced from:
_main in hello-a9a7cd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

请注意,代码使用 libstdc++ 编译文件:

$  g++ --std=c++11 -stdlib=libstdc++ hello.cpp -o hello 

OK

最佳答案

我遇到了同样的问题。参见 Taking pointer to member std::string::size fails to link with libc++ but works with libstdc++

基本上我想到的解决方案是强制实例化 std::string 模板类。

这是建议的解决方案:

#include <iostream>
#include <string>

template class std::basic_string<char>;

int main()
{
auto x = &std::string::size;
std::string hello("hello");
std::cout << (hello.*x)() << std::endl;
return 0;
}

关于c++ - 使用 libc++ 在 Mac Yosemite 上访问 std::string 成员时出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26780964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com