mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 19:43:44 +00:00
cgo: add support for printf
The C printf function is sometimes needed for C files included using CGo. This commit makes sure they're available on all systems where CGo is fully supported (that is, everywhere except on AVR). For baremetal systems using picolibc, I've picked the integer-only version of printf to save on flash size. We might want to consider providing a way to pick the floating point version instead, if needed.
This commit is contained in:
committed by
Ron Evans
parent
3021e16bbf
commit
2eb39785fe
Vendored
+5
@@ -1,5 +1,6 @@
|
||||
#include <math.h>
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int global = 3;
|
||||
bool globalBool = 1;
|
||||
@@ -72,3 +73,7 @@ void arraydecay(int buf1[5], int buf2[3][8], int buf3[4][7][2]) {
|
||||
double doSqrt(double x) {
|
||||
return sqrt(x);
|
||||
}
|
||||
|
||||
void printf_single_int(char *format, int arg) {
|
||||
printf(format, arg);
|
||||
}
|
||||
|
||||
Vendored
+4
@@ -179,6 +179,10 @@ func main() {
|
||||
// libc: test basic stdio functionality
|
||||
putsBuf := []byte("line written using C puts\x00")
|
||||
C.puts((*C.char)(unsafe.Pointer(&putsBuf[0])))
|
||||
|
||||
// libc: test whether printf works in C.
|
||||
printfBuf := []byte("line written using C printf with value=%d\n\x00")
|
||||
C.printf_single_int((*C.char)(unsafe.Pointer(&printfBuf[0])), -21)
|
||||
}
|
||||
|
||||
func printUnion(union C.joined_t) C.joined_t {
|
||||
|
||||
Vendored
+2
@@ -152,3 +152,5 @@ typedef int arraydecay_buf3[4][7][2];
|
||||
void arraydecay(int buf1[5], int buf2[3][8], arraydecay_buf3 buf3);
|
||||
|
||||
double doSqrt(double);
|
||||
|
||||
void printf_single_int(char *format, int arg);
|
||||
|
||||
Vendored
+1
@@ -77,3 +77,4 @@ copied string: foobar
|
||||
CGo sqrt(3): +1.732051e+000
|
||||
C sqrt(3): +1.732051e+000
|
||||
line written using C puts
|
||||
line written using C printf with value=-21
|
||||
|
||||
Reference in New Issue
Block a user