tcc

package module
v0.0.0-...-512e5e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2019 License: MIT Imports: 2 Imported by: 2

README

tcc

Godoc

Go bindings for libtcc.

Usage

Here's how to compile a C program in memory and run it.

package main

import "github.com/sorribas/tcc"

func main () {
	program := `
	extern int printf(char* c,...);
	int main () {
		printf("Hello world!\n");
		return 0;
	}
	`
	tcc := NewTcc()
	defer tcc.Delete()
	tcc.SetOutputType(OUTPUT_MEMORY)

	// tcc must contain the libtcc1.a file that includes the TCC runtime.
	tcc.SetLibPath("/path/to/tcc")
	err := tcc.CompileString(program)
	if err != nil {
		// compilation error
	}
	// run the program
	i := tcc.Run([]string{})
}

You can use this library as a backend for a compiler or simply to compile and run dynamically generated code.

To build a project using this library, you need to have libtcc installed. You can download from the official tcc website

License

MIT License

Documentation

Overview

Package tcc offers go bindings to libtcc.

https://bellard.org/tcc/

The libtcc library allows you to call the tcc compiler programatically. It is available as a static C library. This package has go bindings to it.

It doesn't implement every method available in libtcc yet. If there's a libtcc method that you need and is not implemented in this package, feel free to open an issue or submit a pull request.

To use it you must have libtcc installed. You can download tcc from the website above. After that you can extract and run

./configure
make
make install

If everything works, you will have libtcc installed. Note that you need a C compiler like gcc for the above to work.

Index

Constants

View Source
const OUTPUT_DLL = 3

dynamic library

View Source
const OUTPUT_EXE = 2

executable file

View Source
const OUTPUT_MEMORY = 1

output will be run in memory (default)

View Source
const OUTPUT_OBJ = 4

object file

View Source
const OUTPUT_PREPROCESS = 5

only preprocess (used internally)

Variables

This section is empty.

Functions

This section is empty.

Types

type Tcc

type Tcc struct {
	// contains filtered or unexported fields
}

func NewTcc

func NewTcc() *Tcc

create a new TCC compilation context

func (*Tcc) AddIncludePath

func (tcc *Tcc) AddIncludePath(path string)

add include path

func (*Tcc) CompileString

func (tcc *Tcc) CompileString(src string) error

compile a string containing a C source.

func (*Tcc) Delete

func (tcc *Tcc) Delete()

free a TCC compilation context

func (*Tcc) OutputFile

func (tcc *Tcc) OutputFile(path string)

output an executable, library or object file.

func (*Tcc) Run

func (tcc *Tcc) Run(argv []string) (int, error)

link and run main() function and return its value.

func (*Tcc) SetLibPath

func (tcc *Tcc) SetLibPath(path string)

set CONFIG_TCCDIR at runtime

func (*Tcc) SetOptions

func (tcc *Tcc) SetOptions(opts string)

set options as from command line (multiple supported)

func (*Tcc) SetOutputType

func (tcc *Tcc) SetOutputType(outputType int)

set output type. MUST BE CALLED before any compilation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL