C3: Creating a project

In the previous post we looked how to interop with C. Now let’s look at how we create project and link our C library.

Generating a project #

c3c init myproject

Linking mylib #

Copy the static C library we created in the previous post to a directory of your choosing. I’ll put it in the lib directory of our project for now.

cp path-to/libmylib.a ./lib

You should now have the following project structure.

├── LICENSE
├── README.md
├── docs
├── lib
│   └── libmylib.a
├── project.json
├── resources
├── scripts
├── src
│   └── main.c3
└── test

Add the linked-libraries and linker-search-paths lines.

{
    "langrev": "1",
    "warnings": [ "no-unused" ],
    "dependency-search-paths": [ "lib" ],
    "dependencies": [ ],
    "authors": [ "John Doe <john.doe@example.com>" ],
    "version": "0.1.0",
    "sources": [ "src/**" ],
    "test-sources": [ "test/**" ],
    "output": "build",
    "targets": {
      "myproject": {
        "type": "executable",
      },
    },
    "cpu": "generic",
    "opt": "O0",
    "linked-libraries": [ "mylib" ],
    "linker-search-paths": [ "lib" ]
}

Declarations #

The main source file will look just like it did in the previous post

import std;

struct Point
{
	int x;
	int y;
}

extern fn void point_add(Point* a, Point* b, Point* dest);

fn int main()
{
	Point a = { 1, 1 };
	Point b = { 1, 2 };

	Point c = {};

	point_add(&a, &b, &c);

	io::printfn("x: %d, y: %d", c.x, c.y);

	return 0;
}

Compiling #

c3c build
./build/myproject

Alternatively you can use c3c run.