@@ -1553,6 +1553,7 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
1553
1553
importArgs := b .includeArgs ("-L" , allactions )
1554
1554
swigDirs := make (map [string ]bool )
1555
1555
swigArg := []string {}
1556
+ cxx := false
1556
1557
for _ , a := range allactions {
1557
1558
if a .p != nil && a .p .usesSwig () {
1558
1559
sd := a .p .swigDir (& buildContext )
@@ -1564,8 +1565,50 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
1564
1565
}
1565
1566
swigDirs [sd ] = true
1566
1567
}
1568
+ if a .p != nil && len (a .p .CXXFiles ) > 0 {
1569
+ cxx = true
1570
+ }
1571
+ }
1572
+ ldflags := buildLdflags
1573
+ if cxx {
1574
+ // The program includes C++ code. If the user has not
1575
+ // specified the -extld option, then default to
1576
+ // linking with the compiler named by the CXX
1577
+ // environment variable, or g++ if CXX is not set.
1578
+ extld := false
1579
+ for _ , f := range ldflags {
1580
+ if f == "-extld" || strings .HasPrefix (f , "-extld=" ) {
1581
+ extld = true
1582
+ break
1583
+ }
1584
+ }
1585
+ if ! extld {
1586
+ compiler := strings .Fields (os .Getenv ("CXX" ))
1587
+ if len (compiler ) == 0 {
1588
+ compiler = []string {"g++" }
1589
+ }
1590
+ ldflags = append (ldflags , "-extld=" + compiler [0 ])
1591
+ if len (compiler ) > 1 {
1592
+ extldflags := false
1593
+ add := strings .Join (compiler [1 :], " " )
1594
+ for i , f := range ldflags {
1595
+ if f == "-extldflags" && i + 1 < len (ldflags ) {
1596
+ ldflags [i + 1 ] = add + " " + ldflags [i + 1 ]
1597
+ extldflags = true
1598
+ break
1599
+ } else if strings .HasPrefix (f , "-extldflags=" ) {
1600
+ ldflags [i ] = "-extldflags=" + add + " " + ldflags [i ][len ("-extldflags=" ):]
1601
+ extldflags = true
1602
+ break
1603
+ }
1604
+ }
1605
+ if ! extldflags {
1606
+ ldflags = append (ldflags , "-extldflags=" + add )
1607
+ }
1608
+ }
1609
+ }
1567
1610
}
1568
- return b .run ("." , p .ImportPath , nil , tool (archChar + "l" ), "-o" , out , importArgs , swigArg , buildLdflags , mainpkg )
1611
+ return b .run ("." , p .ImportPath , nil , tool (archChar + "l" ), "-o" , out , importArgs , swigArg , ldflags , mainpkg )
1569
1612
}
1570
1613
1571
1614
func (gcToolchain ) cc (b * builder , p * Package , objdir , ofile , cfile string ) error {
@@ -1641,6 +1684,7 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
1641
1684
ldflags := b .gccArchArgs ()
1642
1685
cgoldflags := []string {}
1643
1686
usesCgo := false
1687
+ cxx := false
1644
1688
for _ , a := range allactions {
1645
1689
if a .p != nil {
1646
1690
if ! a .p .Standard {
@@ -1660,6 +1704,9 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
1660
1704
}
1661
1705
usesCgo = true
1662
1706
}
1707
+ if len (a .p .CXXFiles ) > 0 {
1708
+ cxx = true
1709
+ }
1663
1710
}
1664
1711
}
1665
1712
for _ , afile := range afiles {
@@ -1672,6 +1719,9 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
1672
1719
if usesCgo && goos == "linux" {
1673
1720
ldflags = append (ldflags , "-Wl,-E" )
1674
1721
}
1722
+ if cxx {
1723
+ ldflags = append (ldflags , "-lstdc++" )
1724
+ }
1675
1725
return b .run ("." , p .ImportPath , nil , "gccgo" , "-o" , out , ofiles , "-Wl,-(" , ldflags , "-Wl,-)" , buildGccgoflags )
1676
1726
}
1677
1727
0 commit comments