Using -std=c++11 with the macports gcc-4.7 compiler

Clemens Lang cal at macports.org
Fri Jan 18 08:40:01 PST 2013


On Thu, Jan 17, 2013 at 12:56:26PM -0800, Jeremy Huddleston Sequoia wrote:
> Interesting. I'll take a look, but if it's anything more than "apply
> these patches to gcc" I can't really touch it due to gcc being GPL3.

I've done some testing with a very simple C++ file and Makefile (see the
attachments): I can get my test program compiled with both gcc42 and
gcc47 (the latter from macports) against the libc++ headers in
/usr/lib/c++/v1, however linking fails:

Undefined symbols for architecture x86_64:
  "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 ccdFRT9N.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccdFRT9N.o
  "std::ios_base::Init::~Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccdFRT9N.o
  "std::cout", referenced from:
      _main in ccdFRT9N.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 ccdFRT9N.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 ccdFRT9N.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

Taking a closer look reveals what I think the problem is:
$ nm foo-gcc47.o | grep 'U '
                 U __ZNSolsEPFRSoS_E
                 U __ZNSt8ios_base4InitC1Ev
                 U __ZNSt8ios_base4InitD1Ev
                 U __ZSt4cout
                 U __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
                 U __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
                 U ___cxa_atexit
                 U ___dso_handle
$ nm /usr/lib/libc++.dylib | grep Init
0000000000014392 T __ZNSt3__18ios_base4InitC1Ev
00000000000143a6 T __ZNSt3__18ios_base4InitC2Ev
000000000001439c T __ZNSt3__18ios_base4InitD1Ev
0000000000014448 T __ZNSt3__18ios_base4InitD2Ev

So while the symbols for std::ios_base::Init::Init() and the
corresponding destructor are there, their name mangling is different. If
I could somehow change the name mangling, the linking might actually
work.

Also interesting: It works with the headers in /usr/lib/c++/v1, however
not with those installed by the clang-3.1 port in
/opt/local/libexec/llvm-3.1/lib/c++/v1.

On Thu, Jan 17, 2013 at 01:59:18PM -0800, Sterling Smith wrote:
> I was puzzled by this line:
> > Interesting.  I'll take a look, but if it's anything more than "apply these patches to gcc" I can't really touch it due to gcc being GPL3.
> Maybe I just need to read the GPL3 better.

The missing bit of information there is that Jeremy is an Apple employee
and as such can't work on GPLv3 code per company policy, AFAIK.

-- 
Clemens Lang

-------------- next part --------------
A non-text attachment was scrubbed...
Name: foo.cpp
Type: text/x-c++src
Size: 82 bytes
Desc: not available
URL: <http://lists.macosforge.org/pipermail/macports-users/attachments/20130118/169cebd6/attachment.bin>
-------------- next part --------------
.PHONY: clean

all: foo-clang foo-gcc47 foo-gcc42

clean:
	rm -f foo-clang.o foo-gcc42.o foo-gcc47.o
	rm -f foo-clang foo-gcc42 foo-gcc47

foo-clang: foo.cpp
	/usr/bin/clang++ -stdlib=libc++ -Wall -Werror -c -o $@.o $<
	/usr/bin/clang++ -stdlib=libc++ -Wall -Werror -o $@ $@.o

foo-gcc42: foo.cpp
	/usr/bin/g++ -c -Wall -Werror -nostdlib -nodefaultlibs -isystem/usr/include/c++/v1 $< -o $@.o
	/usr/bin/g++    -Wall -Werror -nostdlib -nodefaultlibs $@.o -lc++ -lc -gcc_s.1 -o $@

foo-gcc47: foo.cpp
	/opt/local/bin/g++-mp-4.7 -c -Wall -Werror -nostdlib -nodefaultlibs -isystem/usr/include/c++/v1 -o $@.o $<
	/opt/local/bin/g++-mp-4.7    -Wall -Werror -nostdlib -nodefaultlibs -lc++ -lc -L/opt/local/lib/gcc47 -lgcc_s.1 -o $@ $<


More information about the macports-users mailing list