我已经在OS X 10.9上编写了一个C ++程序,我想分发可执行文件,但是该程序无法在OS X 10.7上运行。我收到错误消息Illegal instruction: 4。有没有一种方法可以在OS X 10.9上编译我的程序并使它在OS X的早期版本(例如10.6和更高版本)上运行?我没有使用Xcode,而是使用具有OpenMP支持(http://clang-omp.github.io)的clang ++ 3.4版。附带说明一下,我还想将OpenMP库与程序一起分发,以便用户不必自己安装它。如何使该库与旧版本的OS X一起使用?

注意:当我在编译过程中使用-mmacosx-version-min=10.6之类的东西时,会出现一些与“体系结构x86_64的未定义符号”有关的错误。例如,在这个非常简单的程序上:

#include <iostream>

int main()
{
    std::cout << "Hello, world!\n";
    return 0;
}


编译为clang++ -o hello hello.cc -mmacosx-version-min=10.6时,出现以下错误:

Undefined symbols for architecture x86_64:
  "__ZNKSt3__16locale9use_facetERNS0_2idE", referenced from:
      __ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m in hello-323147.o
  "__ZNKSt3__18ios_base6getlocEv", referenced from:
      __ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m in hello-323147.o
  "__ZNSt3__14coutE", referenced from:
      _main in hello-323147.o
  "__ZNSt3__15ctypeIcE2idE", referenced from:
      __ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m in hello-323147.o
  "__ZNSt3__16localeD1Ev", referenced from:
      __ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m in hello-323147.o
  "__ZNSt3__18ios_base33__set_badbit_and_consider_rethrowEv", referenced from:
      __ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m in hello-323147.o
      __ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE5flushEv in hello-323147.o
  "__ZNSt3__18ios_base5clearEj", referenced from:
      __ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m in hello-323147.o
      __ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE6sentryD2Ev in hello-323147.o
      __ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE5flushEv in hello-323147.o
ld: symbol(s) not found for architecture x86_64
clang-3.4: error: linker command failed with exit code 1 (use -v to see invocation)

最佳答案

根据您提出问题的日期,我想这是该特定clang版本中的某种错误。我试过用Xcode 6.4编译您的示例,它可以正常编译和链接:

$ clang++ -o hello hello.cc -mmacosx-version-min=10.6
$ ./hello
Hello, world!
$ clang++ -v
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix


不过,没有一台较旧的osx机器可以对其进行测试,只需在优胜美地上运行即可。

关于c++ - 如何在OS X 10.9上编译C++程序并在OS X的早期版本中使用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24707294/

10-11 22:42