gpt4 book ai didi

c++ - 我在 g++ 中的(简单)代码链接,在 clang 中没有链接

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:15:19 26 4
gpt4 key购买 nike

这是 Stroustrup 的 PPP 中的一个非常简单的练习,您创建 3 个文件:

我的.h:

void print_foo();
void print(int);

我的.cpp:

#include <iostream>
#include <string>
#include "my.h"

using namespace std;

void print(int i)
{
cout << i << '\n';
}

void print_foo()
{
cout << "Foo" << '\n';
}

并使用.cpp:

#include "my.h"

int main()
{
print_foo();
print(7);
}

我的 clang 版本是 Apple LLVM version 7.0.2 (clang-700.1.81)
目标:x86_64-apple-darwin15.2.0

这在 clang 和 g++ 中编译,但不在 clang 中链接。我明白了:

$ clang my.cpp use.cpp -std=c++14 -o drill8_1
Undefined symbols for architecture x86_64:

有人知道为什么会这样吗?我在 g++ 中输入了相同的命令,得到了正确的输出。

谢谢你:)

这是 clang 输出的其余部分:

    Undefined symbols for architecture x86_64:
"std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"std::__1::ios_base::getloc() const", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(unsigned long, char)", referenced from:
std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in my-82a25e.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in my-82a25e.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(int)", referenced from:
print(int) in my-82a25e.o
"std::__1::cout", referenced from:
print(int) in my-82a25e.o
print_foo() in my-82a25e.o
"std::__1::ctype<char>::id", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"std::__1::locale::~locale()", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"std::__1::ios_base::__set_badbit_and_consider_rethrow()", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"std::__1::ios_base::clear(unsigned int)", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"std::terminate()", referenced from:
___clang_call_terminate in my-82a25e.o
"___cxa_begin_catch", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
___clang_call_terminate in my-82a25e.o
"___cxa_end_catch", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
"___gxx_personality_v0", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in my-82a25e.o
std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in my-82a25e.o
Dwarf Exception Unwind Info (__eh_frame) in my-82a25e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

最佳答案

它无法与 clang 链接,因为 clang 旨在用作 C 编译器。这个传统——如果你可以这样调用它的话——来自 GCC,其命令行界面 clang 紧随其后。如果你调用GCC为g++或Clang为clang++,它会自动理解为编译C++代码,并自动链接到C++标准库中。

如果你使用普通的 gccclang 你仍然可以编译 C++ 代码,但是要链接它你通常需要显式链接标准库(例如 -lc++。在调用编译器时仅使用面向 C++ 的名称更简单、更传统。

关于c++ - 我在 g++ 中的(简单)代码链接,在 clang 中没有链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34376709/

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