编译一些简单的 C++ 代码时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7533321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:05:12  来源:igfitidea点击:

Error when compiling some simple c++ code

c++macoslinker-errors

提问by Jean-Philippe Leclerc

I try to compile this cpp code on osx lion but I get an error.

我尝试在 osx lion 上编译此 cpp 代码,但出现错误。

#include <iostream> 

using namespace std; 

int main (int argc, char *argv[]) 
{ 
    for(int i = 0; i < 10; i++) 
    { 
        cout << "hi"; 
        cout << endl; 
    } 

    return 0; 
}

To compile:

编译:

cc main.cpp

Error:

错误:

Undefined symbols for architecture x86_64:
  "std::cout", referenced from:
      _main in ccBdbc76.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in ccBdbc76.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in ccBdbc76.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
      _main in ccBdbc76.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccBdbc76.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in ccBdbc76.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

回答by Carl Norum

Normally this sort of failure happens when compiling your C++ code by invoking the C front-end. The gccyou execute understands and compiles the file as C++, but doesn't link it with the C++ libraries. Example:

通常在通过调用 C 前端编译 C++ 代码时会发生这种失败。在gcc你执行理解和编译文件作为C ++,但不与C ++库链接。例子:

$ gcc example.cpp 
Undefined symbols for architecture x86_64:
  "std::cout", referenced from:
      _main in ccLTUBHJ.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in ccLTUBHJ.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in ccLTUBHJ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
      _main in ccLTUBHJ.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccLTUBHJ.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in ccLTUBHJ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
$ g++ example.cpp 
$ 

As you can see, using g++makes the problems go away. The same behaviour (with slightly different messages) occurs if you use clang(which I'd recommend):

如您所见,使用g++会使问题消失。如果您使用clang(我建议这样做),则会发生相同的行为(消息略有不同):

$ clang example.cpp 
Undefined symbols for architecture x86_64:
  "std::ios_base::Init::~Init()", referenced from:
      ___cxx_global_var_init in cc-IeV9O1.o
  "std::ios_base::Init::Init()", referenced from:
      ___cxx_global_var_init in cc-IeV9O1.o
  "std::cout", referenced from:
      _main in cc-IeV9O1.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in cc-IeV9O1.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in cc-IeV9O1.o
  "std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:
      _main in cc-IeV9O1.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++ example.cpp 
$

As you can see in the clangerror message, you could use -vto see the linker invocation to see what's going wrong. It would show you this link line:

正如您在clang错误消息中看到的那样,您可以使用-v查看链接器调用来查看出了什么问题。它会向您显示此链接行:

"/usr/bin/ld" -demangle -dynamic -arch x86_64 
    -macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o
    /var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-hdOL8Z.o
    -lSystem /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a

Or something like it - as you can see, it's linking the C runtime, not C++, and also doesn't have the C++ libraries. Using clang++the link line is:

或者类似的东西——如你所见,它链接的是 C 运行时,而不是 C++,也没有 C++ 库。使用clang++链接行是:

"/usr/bin/ld" -demangle -dynamic -arch x86_64
     -macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o 
     /var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-wJwxjP.o 
     /usr/lib/libstdc++.6.dylib -lSystem
     /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a

As you can see, libstdc++is included, and presto - no link errors.

如您所见,libstdc++已包含在内,并且很快 - 没有链接错误。

回答by Allisone

Try

尝试

g++ main.cpp

This way it should work, at least using OS X

这样它应该可以工作,至少使用 OS X

回答by S.C. Madsen

I'm not familiar with OSX LION. However, in the strictest sense, the errors described are not caused by the compiler, but by the linker. It seems as if the standard library is not being linked.

我不熟悉 OSX LION。但是,严格来说,所描述的错误不是由编译器引起的,而是由链接器引起的。似乎标准库没有被链接。

回答by AndrzejJ

Use CC command (uppercase) to compile C++ and link to standard C++ library.

使用 CC 命令(大写)编译 C++ 并链接到标准 C++ 库。

回答by Alistair A. Israel

As of Yosemite (10.10.1), I've found that gccwith the -lc++flag also works:

从优胜美地(10.10.1)开始,我发现gcc使用-lc++标志也可以:

gcc -lc++ main.cpp

回答by jkhosla

If using clang on OS X , try :

如果在 OS X 上使用 clang,请尝试:

clang++ simple_cpp_program_file.cpp -o simple_cpp_program_file.out

回答by user1031613

Here's the solution that works on macOs Sierra:

这是适用于 macOs Sierra 的解决方案:

There are two implementations of the standard C++ library available on OS X: libstdc++ and libc++. They are not binary compatible and libMLi3 requires libstdc++.

OS X 上有两种标准 C++ 库的实现:libstdc++ 和 libc++。它们不是二进制兼容的,并且 libMLi3 需要 libstdc++。

On 10.8 and earlier libstdc++ is chosen by default, on 10.9 libc++ is chosen by default. To ensure compatibility with libMLi3, we need to choose libstdc++ manually.

在 10.8 及更早版本上默认选择 libstdc++,在 10.9 上默认选择 libc++。为了保证与libMLi3的兼容性,我们需要手动选择libstdc++。

To do this, add -stdlib=libstdc++ to the linking command.

为此,请将 -stdlib=libstdc++ 添加到链接命令中。

回答by wangqingtao

So the error ld: library not found for -lstdc++ is where the actual error lies.

所以错误 ld: library not found for -lstdc++ 是实际错误所在。

To fix this, open the folder

要解决此问题,请打开文件夹

open /Library/Developer/CommandLineTools/Packages/

打开 /Library/Developer/CommandLineTools/Packages/

Run the package macOS_SDK_headers_for_macOS_10.14.pkg

运行包 macOS_SDK_headers_for_macOS_10.14.pkg

Then gem install mini_racer works!

然后 gem install mini_racer 工作!

This issue may not be only related to mini_racer as it could affect any gem that compiles an extension.。

这个问题可能不仅仅与 mini_racer 相关,因为它可能会影响任何编译扩展的 gem。

回答by Frigo

Is this GCC on Windows (MinGW) or Linux? On MinGW you need the parameters -lmingw32 -enable-auto-import. Linux might needs something similar, -enable-auto-importis most likely needed.

这是 Windows (MinGW) 还是 Linux 上的 GCC?在 MinGW 上,您需要参数-lmingw32 -enable-auto-import。Linux 可能需要类似的东西,-enable-auto-import很可能需要。