Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for compiling c++ source file. #2210

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

learnforpractice
Copy link
Contributor

@learnforpractice learnforpractice commented Oct 29, 2021

This pull request adds support for compiling C++ source files.

See example at "src/examples/callcpp"

Developers can specify cxxflags in target json file like this:

{
	"cflags": [
...
	],
	"cxxflags": [
		"-std=c++17"
	],
...
}

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

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

Can you add the example to the Makefile so it gets tested? You can add it here for example:

 ifneq ($(OS),Windows_NT)
 	$(TINYGO) build -o test.elf -gc=leaking -scheduler=none examples/serial
+	$(TINYGO) build -o test.elf                             examples/callcpp
 endif

loader/loader.go Outdated
@@ -51,6 +51,7 @@ type PackageJSON struct {
GoFiles []string
CgoFiles []string
CFiles []string
CXXFiles []string
Copy link
Member

Choose a reason for hiding this comment

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

Please run go fmt loader/loader.go to correctly format this file.

@@ -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'

builder/build.go Outdated
copy(flags, pkg.CFlags)
copy(flags[len(pkg.CFlags):], pkg.CXXFlags)
job := &compileJob{
description: "compile CGo CPP file " + abspath,
Copy link
Member

Choose a reason for hiding this comment

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

CPP is usually used for the C preprocessor.

Suggested change
description: "compile CGo CPP file " + abspath,
description: "compile CGo C++ file " + abspath,

@learnforpractice learnforpractice force-pushed the support-for-compiling-cpp branch from 4a554da to de69e7a Compare October 31, 2021 01:53
@learnforpractice
Copy link
Contributor Author

learnforpractice commented Oct 31, 2021 via email

@learnforpractice
Copy link
Contributor Author

learnforpractice commented Oct 31, 2021 via email

@learnforpractice
Copy link
Contributor Author

learnforpractice commented Oct 31, 2021 via email

@aykevl
Copy link
Member

aykevl commented Nov 1, 2021

Actually, I think there is a much better way to do that: using #cgo CXXFLAGS. See: https://pkg.go.dev/cmd/cgo

In fact, I think some of the logic in this PR is not correct:

All the cgo CPPFLAGS and CFLAGS directives in a package are concatenated and used to compile C files in that package. All the CPPFLAGS and CXXFLAGS directives in a package are concatenated and used to compile C++ files in that package. All the CPPFLAGS and FFLAGS directives in a package are concatenated and used to compile Fortran files in that package. All the LDFLAGS directives in any package in the program are concatenated and used at link time. All the pkg-config directives are concatenated and sent to pkg-config simultaneously to add to each appropriate set of command-line flags.

In other words, CGo normally doesn't mix CFLAGS and CXXFLAGS like is done in this PR.

I would suggest the following:

  • Use #cgo CFLAGS, #cgo CPPFLAGS, and the cflags key from the target spec to compile C files.
  • Use #cgo CXXFLAGS, #cgo CPPFLAGS, and (again) the cflags key from the target spec to compile C++ files.

This means that you can use a particular version of C++ like this:

// #cgo CXXFLAGS: -std=c++17
import "C"

To avoid duplication I think it makes sense to use the cflags field in the TargetSpec for both C and C++. The cflags field is mostly used for low level details such as the architecture and ABI, which apply to both C and C++.

@learnforpractice
Copy link
Contributor Author

learnforpractice commented Nov 3, 2021 via email

@deadprogram
Copy link
Member

Hello @learnforpractice is this PR something that you are still working on? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants