Skip to main content
Giac provides a powerful C++ API that allows you to integrate computer algebra capabilities directly into your C++ applications. This guide covers compilation, linking, and usage of the Giac library.

Prerequisites

Before building Giac for C++, you need:
  • C++ compiler with C++11 support (GCC, Clang, or MSVC)
  • CMake 3.10 or higher
  • GMP (GNU Multiple Precision Arithmetic Library)
  • MPFR (Multiple Precision Floating-Point Reliable Library)
sudo apt-get install build-essential cmake libgmp-dev libmpfr-dev

Building with CMake

Giac includes a complete CMake build system that works across all platforms.

Basic Build

mkdir build
cd build
cmake ..
make

Windows Build

For Windows with MSYS2 CLANG64:
mkdir build
cd build
CC=clang CXX=clang++ cmake -G "MinGW Makefiles" ..
mingw32-make
For 32-bit builds, use the CLANG32 flavor and replace x86_64 with i686 in package names.

C++ API Usage

Basic Example

Here’s a simple example showing how to evaluate expressions using Giac:
#include "giac.h"
#include <iostream>
#include <string>

using namespace std;
using namespace giac;

int main() {
    context ct;
    
    // Evaluate a simple expression
    gen g("expand((x+y)^3)", &ct);
    string result = giac::print(giac::eval(g, &ct), &ct);
    cout << "Result: " << result << endl;
    
    return 0;
}
Expected output:
Result: x^3+y^3+3*x*y^2+3*x^2*y

Creating a CAS Wrapper Library

You can create a clean API wrapper for your applications: GeoGebraCAS.h
#ifdef _WIN32
#ifdef GEOGEBRACAS_EXPORTS
#define GEOGEBRACAS_API __declspec(dllexport) 
#else
#define GEOGEBRACAS_API __declspec(dllimport) 
#endif
#else
#define GEOGEBRACAS_API
#endif

using namespace std;
#include <string>

extern "C" {
    GEOGEBRACAS_API void initializeCAS();
    GEOGEBRACAS_API string evaluateCAS(string command);
};
GeoGebraCAS.cc
#include "GeoGebraCAS.h"
#include <stdexcept>
#include "giac.h"

using namespace std;
using namespace giac;

#ifndef _WIN32
#define EXPORT __attribute__((visibility("default")))
__attribute__((constructor))
static void initializer(void) { }
__attribute__((destructor))
static void finalizer(void) { }
#else
#define EXPORT
#endif

context ct;

extern "C" {
    EXPORT void initializeCAS() { return; }
    
    EXPORT string evaluateCAS(string command) {
        gen e(string(command), &ct);
        try {
            return giac::print(giac::eval(e, &ct), &ct);
        } catch (std::runtime_error & err) {
            cerr << err.what() << endl;
        }
    }
}

Advanced Examples

context ct;

// Solve quadratic equation
gen g("solve(x^2-3*x+2=0)", &ct);
string result = giac::print(giac::eval(g, &ct), &ct);
cout << result << endl; // Output: [1,2]
context ct;

// Define a function and compute its derivative
gen g1("f(x):=sin(x^2)", &ct);
giac::eval(g1, &ct);

gen g2("f'(2)", &ct);
string result = giac::print(giac::eval(g2, &ct), &ct);
cout << "f'(2) = " << result << endl;
context ct;

// Compute definite integral
gen g("int(1/(x^4+1)^4,x,0,+infinity)", &ct);
string result = giac::print(giac::eval(g, &ct), &ct);
cout << result << endl;
context ct;

// Create matrix and compute eigenvalues
gen g1("A:=[[1,2],[3,4]]", &ct);
giac::eval(g1, &ct);

gen g2("eigenvalues(A)", &ct);
string result = giac::print(giac::eval(g2, &ct), &ct);
cout << "Eigenvalues: " << result << endl;

Linking Against Giac

CMake Integration

Add Giac to your CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(my_app)

set(CMAKE_CXX_STANDARD 11)

# Add Giac library
add_subdirectory(giac)

# Your executable
add_executable(my_app main.cpp)

# Link against Giac
target_link_libraries(my_app giac mpfr gmp)
target_include_directories(my_app PRIVATE giac/src/giac/headers)

Manual Compilation

g++ -std=c++11 -I/path/to/giac/headers main.cpp \
    -L/path/to/giac/lib -lgiac -lmpfr -lgmp -o my_app

Build Definitions

Giac uses several preprocessor definitions for platform-specific behavior:
#define GIAC_GGB          // GeoGebra mode
#define IN_GIAC           // Building inside Giac
#define HAVE_MPFR         // MPFR support
#define HAVE_LIBMPFR      // Link with MPFR
#define TIMEOUT           // Enable timeouts
#define VERSION="1.2.3"   // Version string

Error Handling

Giac uses C++ exceptions for error reporting:
#include "giac.h"
#include <stdexcept>

using namespace giac;

context ct;

try {
    gen g("1/0", &ct);  // Division by zero
    string result = giac::print(giac::eval(g, &ct), &ct);
    cout << result << endl;
} catch (std::runtime_error & err) {
    cerr << "Error: " << err.what() << endl;
}

Static Linking

Static linking of MPFR may break in some cases. See the MPFR FAQ if you encounter issues.
The build system prefers static linking for portability:
# Use prebuilt static libraries
find_static_library(gmp GMP_STATIC ${PREBUILT_DIR}/linux/x86-64/)
find_static_library(mpfr MPFR_STATIC ${PREBUILT_DIR}/linux/x86-64/)

target_link_libraries(my_app ${MPFR_STATIC} ${GMP_STATIC})
MPFR must be linked before GMP due to dependency order.

Performance Considerations

  • Reuse context objects: Creating a giac::context is expensive; reuse it for multiple evaluations
  • Precompile expressions: Parse expressions once and evaluate multiple times
  • Thread safety: Each thread should have its own context object
  • Memory management: Giac manages memory automatically through reference counting

GeoGebra Mode

Giac includes a special GeoGebra mode with enhanced features:
context ct;

// Enable GeoGebra mode
gen g1("caseval(\"init geogebra\")", &ct);
cout << giac::print(giac::eval(g1, &ct), &ct) << endl;
// Output: "geogebra mode on"

// Lists become sets in GeoGebra mode
gen g2("[1]", &ct);
cout << giac::print(giac::eval(g2, &ct), &ct) << endl;
// Output: {1}

// Disable GeoGebra mode
gen g3("caseval(\"close geogebra\")", &ct);
cout << giac::print(giac::eval(g3, &ct), &ct) << endl;
// Output: "geogebra mode off"

Next Steps

API Reference

Explore the complete Giac function reference

Core API

Learn about the gen class and core types

Node.js Integration

Integrate Giac with Node.js applications

Android Integration

Use Giac in Android apps

Build docs developers (and LLMs) love