diff --git a/src/main.rs b/src/main.rs
index b7111d6..5951618 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -17,7 +17,7 @@ use {
     clap::Parser,
     walkdir::WalkDir,
     tiny_ansi::TinyAnsi,
-    std::{path::PathBuf, sync::{Arc, RwLock}},
+    std::{path::PathBuf, sync::{Arc, RwLock}, io::Write},
 };
 
 #[derive(Parser, Debug)]
@@ -27,7 +27,10 @@ struct Args {
     morphism_base: Vec<PathBuf>,
 
     #[arg(short='m', long="morph")]
-    morphisms: Vec<String>
+    morphisms: Vec<String>,
+
+    #[arg(short='o', long)]
+    output: Option<PathBuf>
 }
 
 fn main() {
@@ -103,7 +106,11 @@ fn main() {
         }
     }
 
-    println!("{}",
-        crate::c_gen::gen_lib::generate_lib(&mut type_dict, instances).expect("failed to generate main function")
-    );
+    let c_source = crate::c_gen::gen_lib::generate_lib(&mut type_dict, instances).expect("failed to generate library");
+    if let Some(out_path) = args.output {
+        let mut file = std::fs::File::create(out_path).expect("failed to open output file");
+        file.write_all(c_source.as_bytes()).expect("failed to write output file");
+    } else {
+        println!("{}", c_source);
+    }
 }