Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ endif
@$(MD5SUM) test.hex
ifneq ($(OS),Windows_NT)
$(TINYGO) build -o test.elf -gc=leaking -scheduler=none examples/serial
$(TINYGO) build -o test.elf examples/callcpp
endif


Expand Down
15 changes: 15 additions & 0 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,21 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
}
linkerDependencies = append(linkerDependencies, job)
}
for _, filename := range pkg.CXXFiles {
abspath := filepath.Join(pkg.Dir, filename)
flags := make([]string, len(pkg.CFlags)+len(pkg.CXXFlags))
copy(flags, pkg.CFlags)
copy(flags[len(pkg.CFlags):], pkg.CXXFlags)
job := &compileJob{
description: "compile CGo C++ file " + abspath,
run: func(job *compileJob) error {
result, err := compileAndCacheCFile(abspath, dir, flags, config.Options.PrintCommands)
job.result = result
return err
},
}
linkerDependencies = append(linkerDependencies, job)
}
}

// Linker flags from CGo lines:
Expand Down
8 changes: 8 additions & 0 deletions compileopts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ func (c *Config) CFlags() []string {
return cflags
}

func (c *Config) CXXFlags() []string {
var cxxflags []string
for _, flag := range c.Target.CXXFlags {
cxxflags = append(cxxflags, strings.ReplaceAll(flag, "{root}", goenv.Get("TINYGOROOT")))
}
return cxxflags
}

// LDFlags returns the flags to pass to the linker. A few more flags are needed
// (like the one for the compiler runtime), but this represents the majority of
// the flags.
Expand Down
1 change: 1 addition & 0 deletions compileopts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type TargetSpec struct {
AutoStackSize *bool `json:"automatic-stack-size"` // Determine stack size automatically at compile time.
DefaultStackSize uint64 `json:"default-stack-size"` // Default stack size if the size couldn't be determined at compile time.
CFlags []string `json:"cflags"`
CXXFlags []string `json:"cxxflags"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why a cxxflags key is necessary?
C flags are important in the target file because they define things like the float ABI. But these flags are also used for C++. I can't think of a reason why you would want to configure C++ flags in a target file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cxxflags is needed for compiling C++ source code. If you move cxx flags such as -std=c++17 to cflags, you will get the following error when compiling the C source code.

error: invalid argument '-std=c++17' not allowed with 'C'

LDFlags []string `json:"ldflags"`
LinkerScript string `json:"linkerscript"`
ExtraFiles []string `json:"extra-files"`
Expand Down
3 changes: 3 additions & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type PackageJSON struct {
GoFiles []string
CgoFiles []string
CFiles []string
CXXFiles []string

// Dependency information
Imports []string
Expand All @@ -72,6 +73,7 @@ type Package struct {
Files []*ast.File
FileHashes map[string][]byte
CFlags []string // CFlags used during CGo preprocessing (only set if CGo is used)
CXXFlags []string
CGoHeaders []string // text above 'import "C"' lines
Pkg *types.Package
info types.Info
Expand Down Expand Up @@ -400,6 +402,7 @@ func (p *Package) parseFiles() ([]*ast.File, error) {
}
generated, headerCode, cflags, ldflags, accessedFiles, errs := cgo.Process(files, p.program.workingDir, p.program.fset, initialCFlags)
p.CFlags = append(initialCFlags, cflags...)
p.CXXFlags = append(p.CXXFlags, p.program.config.CXXFlags()...)
p.CGoHeaders = headerCode
for path, hash := range accessedFiles {
p.FileHashes[path] = hash
Expand Down
9 changes: 9 additions & 0 deletions src/examples/callcpp/add.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
template <typename T>
T add(T a, T b) {
return a + b;
}

extern "C" int add(int a, int b) {
return add<int>(a, b);
}

12 changes: 12 additions & 0 deletions src/examples/callcpp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

/*
int add(int a, int b);
*/
import "C"
import "fmt"

func main() {
a := C.add(C.int(1), C.int(2))
fmt.Println(a)
}