How I do compile a application against a static library compiled with libc++ in xcode/clang/macos?

不想你离开。 提交于 2019-12-07 11:19:48

问题


When I try to compile a test console application to test some functionality on a static library on the same workspace, i run into problems in the linking stage of the binary, it only happen when I choose to use libc++ standard library.

The missing symbols error is the follow :

    Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::empty() const", referenced from:
      libtorrent::torrent::replace_trackers(std::__1::vector<libtorrent::announce_entry, std::__1::allocator<libtorrent::announce_entry> > const&) in libLibOFFTorrent-xcode.a(torrent.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

When I choose stdlibc++ in both targets everything compiles ok and it runs OK.

my questions are :

  1. there are some restriction on using libc++ on static libraries?
  2. its a bug in apple/clang++ linker tool?
  3. how can I configure the project to use libc++ with my static libraries?
  4. why the linker tool does not find the symbols of a standard c++ libraries on a static lib?, (any other lib that depends on is compiled against libc++)
  5. should I forget the idea on using libc++?

notes:

  1. the static library depends on libboost_system, witch i had compiled with libc++ and libstdc++ with the same results
  2. when i run the test with the 'bjam' tool it runs OK, maybe the jam files chooses libstdc++ to compile the files.
  3. I know that changing the standard library fix the linking problem , I only want to know why is that.

UPDATE: when I remove the reference to string::empty in the static lib project, the project that depends on compiles with libc++ fine and runs, but it gets in a infinite loop.

UPDATE 2: removing the string::empty references causes no effect when I compile the whole thing with libstdc++ it runs fine. no loops, this makes me think that is a bug or something like that.

UPDATE 3: when it compiles this is the place where the programs loops indefinitely :


回答1:


It seems that one of your dependencies (libtorrent) has been built against libstdc++.

Check the namespace : std::__1::basic_string. It has the __1 prefix, usually indicating libstdc++).

I may be wrong but I think you need to rebuild your libtorrent against libc++ if you absolutely want to use this one.

Note that it is pretty common to use the stdlibc++.




回答2:


Did you by any chance compile libtorrent with a -D_LIBCPP_INLINE_VISIBILITY=""?

The reason I ask is that std::string::empty() isn't in libc++.dylib because it is marked up with "always_inline". And so it should have been inlined into libtorrent when it was used.



来源:https://stackoverflow.com/questions/14803622/how-i-do-compile-a-application-against-a-static-library-compiled-with-libc-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!