Skip to content

Commit 06bb8b3

Browse files
committed
Fix link order on gcc and add clang as default on Linux
1 parent bde8962 commit 06bb8b3

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

compiler_linux.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
type linuxCompiler struct {
16+
toolset string
1617
}
1718

1819
func (ci linuxCompiler) Compile(path, objDir string, options *CompilerOptions) error {
@@ -43,7 +44,7 @@ func (ci linuxCompiler) Compile(path, objDir string, options *CompilerOptions) e
4344

4445
args = append(args, path)
4546

46-
cmd := exec.Command("gcc", args...)
47+
cmd := exec.Command(ci.toolset, args...)
4748

4849
if options.Verbose {
4950
log.Trace("%s", strings.Join(cmd.Args, " "))
@@ -60,7 +61,7 @@ func (ci linuxCompiler) Compile(path, objDir string, options *CompilerOptions) e
6061
func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *CompilerOptions) (string, error) {
6162
args := make([]string, 0)
6263

63-
exeName := "gcc"
64+
exeName := ci.toolset
6465

6566
switch outType {
6667
case LinkExe:
@@ -82,8 +83,11 @@ func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *
8283
} else {
8384
args = append(args, "-o", outPath)
8485
args = append(args, "-std=c++17")
85-
args = append(args, "-static-libgcc")
86-
args = append(args, "-static-libstdc++")
86+
87+
if ci.toolset == "gcc" {
88+
args = append(args, "-static-libgcc")
89+
args = append(args, "-static-libstdc++")
90+
}
8791

8892
if options.Static {
8993
args = append(args, "-static")
@@ -93,16 +97,6 @@ func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *
9397
for _, dir := range options.LinkDirectories {
9498
args = append(args, "-L"+dir)
9599
}
96-
97-
// Add libraries to link
98-
for _, link := range options.LinkLibraries {
99-
args = append(args, "-l"+link)
100-
}
101-
102-
// Add additional linker flags
103-
for _, flag := range options.LinkerFlags {
104-
args = append(args, flag)
105-
}
106100
}
107101

108102
filepath.Walk(objDir, func(path string, info os.FileInfo, err error) error {
@@ -116,6 +110,21 @@ func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *
116110
return nil
117111
})
118112

113+
if outType != LinkLib {
114+
// Link to some common standard libraries
115+
args = append(args, "-lstdc++")
116+
117+
// Add libraries to link
118+
for _, link := range options.LinkLibraries {
119+
args = append(args, "-l"+link)
120+
}
121+
122+
// Add additional linker flags
123+
for _, flag := range options.LinkerFlags {
124+
args = append(args, flag)
125+
}
126+
}
127+
119128
cmd := exec.Command(exeName, args...)
120129

121130
if options.Verbose {

getcompiler_linux.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,26 @@
22

33
package main
44

5+
import (
6+
"errors"
7+
"os/exec"
8+
)
9+
510
func getCompiler() (Compiler, error) {
611
//TODO: Check if we have gcc and ld installed
712
//TODO: Add option for other flavors of gcc (eg. mingw)
813

9-
return linuxCompiler{}, nil
14+
toolset := ""
15+
16+
if _, err := exec.LookPath("clang"); err == nil {
17+
toolset = "clang"
18+
} else if _, err := exec.LookPath("gcc"); err == nil {
19+
toolset = "gcc"
20+
} else {
21+
return nil, errors.New("couldn't find clang or gcc in the PATH")
22+
}
23+
24+
return linuxCompiler{
25+
toolset: toolset,
26+
}, nil
1027
}

0 commit comments

Comments
 (0)