Status: New
Owner: liuj...@google.com
Labels: Type-Defect Priority-Medium

New issue 449 by yayj...@gmail.com: Link error while using clang++ with -stdlib=libc++ flag in OSX
http://code.google.com/p/protobuf/issues/detail?id=449

What steps will reproduce the problem?
1. To avoid the affection of my other code, I wrote down this short snippet:

packet.proto:

message packet {
    required uint32 var = 1;
}

And pb.cc:

#include "packet.pb.h"

int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}

2. If I try to link with libc++, clang++ will complain like this error:

$ clang++ -stdlib=libc++ -o pb pb.cc packet.pb.cc -lprotobuf
Undefined symbols for architecture x86_64:
"google::protobuf::MessageFactory::InternalRegisterGeneratedFile(char const*, void (*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&))", referenced from:
      protobuf_AddDesc_packet_2eproto() in packet-q8y2hq.o
"google::protobuf::DescriptorPool::FindFileByName(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:
      protobuf_AssignDesc_packet_2eproto() in packet-q8y2hq.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

3. But everything seems OK without libc++:

$ clang++ -o pb pb.cc packet.pb.cc -lprotobuf
$ otool -L pb
pb:
/usr/local/lib/libprotobuf.7.dylib (compatibility version 8.0.0, current version 8.0.0) /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

What is the expected output? What do you see instead?

Linking stage passed for sure.

What version of the product are you using? On what operating system?

OS: OS X 10.8.2

Here are the versions of clang and protobuf:

$ clang -v
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
$ brew info protobuf
protobuf: stable 2.4.1
http://code.google.com/p/protobuf/
/usr/local/Cellar/protobuf/2.4.1 (62 files, 5.9M) *
https://github.com/mxcl/homebrew/commits/master/Library/Formula/protobuf.rb

Please provide any additional information below.

And I also compiled the snippet on a Linux workstation on which clang and libc++ were installed. But no error was raised with libc++.

$ /opt/llvm/bin/clang++ -stdlib=libc++ -o pb pb.cc packet.pb.cc -lprotobuf
$ ldd pb
        linux-gate.so.1 =>  (0x005dd000)
        libprotobuf.so.7 => /usr/local/lib/libprotobuf.so.7 (0x0035c000)
        libc++.so.1 => /usr/lib/libc++.so.1 (0x00113000)
        libm.so.6 => /lib/libm.so.6 (0x0072d000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x006ea000)
        libc.so.6 => /lib/libc.so.6 (0x00a6c000)
        libpthread.so.0 => /lib/libpthread.so.0 (0x001b1000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x001cc000)
        librt.so.1 => /lib/librt.so.1 (0x002b7000)
        /lib/ld-linux.so.2 (0x008c3000)

As you can see above, I built libc++ on Linux by using libstdc++ as the ABI layer.

$ clang++ -v
clang version 3.1 (tags/RELEASE_31/final 170526)
Target: i386-pc-linux-gnu
Thread model: posix

libcxx$ svn info
Path: .
URL: http://llvm.org/svn/llvm-project/libcxx/trunk
Repository Root: http://llvm.org/svn/llvm-project
Repository UUID: 91177308-0d34-0410-b5e6-96231b3b80d8
Revision: 170542
Node Kind: directory
Schedule: normal
Last Changed Author: marshall
Last Changed Rev: 170435
Last Changed Date: 2012-12-19 00:46:30 +0800 (Wed, 19 Dec 2012)


Because I didn't know whether it is the root cause or not that my protobuf library is built by libstdc++ instead of libc++, I also tried to build it with libc++ instead of homebrew.

$ ./configure CC=clang CXX=clang++ CXXFLAGS='-stdlib=libc++'
$ make
...
/bin/sh ../libtool --tag=CXX --mode=compile clang++ -DHAVE_CONFIG_H -I. -I.. -D_THREAD_SAFE -pthread -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -pipe -stdlib=libc++ -MT message.lo -MD -MP -MF .deps/message.Tpo -c -o message.lo `test -f 'google/protobuf/message.cc' || echo './'`google/protobuf/message.cc libtool: compile: clang++ -DHAVE_CONFIG_H -I. -I.. -D_THREAD_SAFE -pthread -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -pipe -stdlib=libc++ -MT message.lo -MD -MP -MF .deps/message.Tpo -c google/protobuf/message.cc -fPIC -DPIC -o .libs/message.o google/protobuf/message.cc:130:60: error: implicit instantiation of undefined
      template 'std::__1::basic_istream<char, std::__1::char_traits<char> >'
  return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
                                                           ^
/usr/include/c++/v1/iosfwd:108:27: note: template is declared here
    class _LIBCPP_VISIBLE basic_istream;
                          ^
google/protobuf/message.cc:135:67: error: implicit instantiation of undefined
      template 'std::__1::basic_istream<char, std::__1::char_traits<char> >'
  return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
                                                                  ^
/usr/include/c++/v1/iosfwd:108:27: note: template is declared here
    class _LIBCPP_VISIBLE basic_istream;
                          ^
google/protobuf/message.cc:175:16: error: implicit instantiation of undefined
      template 'std::__1::basic_ostream<char, std::__1::char_traits<char> >'
  return output->good();
               ^
/usr/include/c++/v1/iosfwd:110:27: note: template is declared here
    class _LIBCPP_VISIBLE basic_ostream;
                          ^
3 errors generated.
gmake[2]: *** [message.lo] Error 1

Then, I followed this link(http://freebsd.1045724.n5.nabble.com/ports-166011-patch-devel-protobuf-unbreak-with-libc-td5561262.html) and patched it. These errors disappeared, but a lot of link errors came up. All of them are shown in attachment build.log.


Attachments:
        build.log  41.3 KB

--
You received this message because you are subscribed to the Google Groups "Protocol 
Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to