简体   繁体   中英

How to compile a C++ program on OS X 10.9 and be used on previous versions of OS X?

I've written a C++ program on OS X 10.9, and I'd like to distribute the executable file, but the program won't run on OS X 10.7. I get the error message Illegal instruction: 4 . Is there a way to compile my program on OS X 10.9 and have it work on previous versions of OS X, say 10.6 and greater? I'm not using Xcode, I'm using clang++ version 3.4 with OpenMP support ( http://clang-omp.github.io ). As a side note, I'd also like to distribute the OpenMP library with my program so that users don't have to install it themselves. How do I make the library work with older versions of OS X?

Note: When I use something like -mmacosx-version-min=10.6 during compilation I get several errors related to "Undefined symbols for architecture x86_64". For example, on this very simple program:

#include <iostream>

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

Compiled as clang++ -o hello hello.cc -mmacosx-version-min=10.6 , I get the following error:

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)

Based on the date of your question I guess this was some sort of bug in that specific clang version. I've tried compiling your example with Xcode 6.4 and it compiles and links fine:

$ 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

Don't have an older osx machine to test it though, just ran it on Yosemite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM