Minimal snapcraft.yaml for a C program

Hello,

I’m trying to build the simplest hello.c program:

#include <stdio.h>

int main() {
  printf("Hello, world!\n");
  return 0;
}

but my build parameters are suspect owing to incorrect use of autotools (and the compiler) presumably. The log has the following error message:

autoreconf: error: 'configure.ac' is required

The relevant snippet from the snapcraft.yaml file is: grade: devel confinement: devmode

parts:
  hello-snap:
    plugin: autotools
    source: .
    build-packages:
      - g++

apps:
  hello-snap:
    command: ./hello-snap

There are only two files in the working folder - hello.c and snapcraft.yaml. What corrections do I need to make to the yaml file to build the snap successfully? Thanks.

Regards. P.S. Previously, I tried several other variations for the plugin but most of the errors were related to these variations (e.g. build, make). Both build-essential and make are present on the dev computer.

Hi @matha,

There is some documentation here (The autotools plugin | Snapcraft documentation) that describes which files the autotools plugin requires.

A simple example can be found here, which has a Makefile.am and configure.ac in addition to a “hello world” C program.

The autotools plugin does indeed expect your project to already be set up for building it with autotools … likewise for other plugins you could use … i.e the make plugin would expect a Makefile in your project dir…

If you just want to build the binary without adding any build system you can use the nil plugin and override-build: | to simply do the same gcc call you would do when building it manually on cmdline

i.e.:

    override-build: |
      gcc -o $CRAFT_PART_INSTALL/hello-snap hello-snap.c 

Thanks, @mr_cal. It worked! Of course, now I’m busy trying to understand the “build” by reviewing the log.

Regards.

I think you want to use the make plugin, make your code work fine locally and then just package it in a snap with the make plugin. The only real requirement is support for $(DESTDIR) variable. There a ample examples but I can offer one myself if you need one.

Thanks for the offer @zyga! Would love to get an example or two from you. Tried to get some from Google Gemini before starting this thread (and failed miserably). No comment on Gemini because my query could have been better.

I would like to get these “teething steps” under my belt so that I can ramp up the learning curve at a faster pace. The more varied the example, the better for my learning experience.

Regards.

The main idea is to decouple the whole build system from snapcraft, so that snapcraft is just the part where it gets shipped as a snap.

The .gitignore file:

hello-world
*.snap

The hello-world.c file:

#include <stdlib.h>
#include <stdio.h>

int main() {
  printf("Hello World\n");
  return 0;
}

The Makefile file - note that all the indents are done with hard tabs, not spaces:

DESTDIR ?=
CFLAGS ?= -Wall
bindir = /usr/bin

.PHONY: all clean install

all: hello-world
install: $(DESTDIR)$(bindir)/hello-world
clean:
	rm -f hello-world

hello-world: hello-world.c
$(DESTDIR)$(bindir)/hello-world: hello-world | $(DESTDIR)$(bindir)
	install -m 755 $< $@
$(DESTDIR)$(bindir):
	mkdir -p $@

And lastly the snapcraft.yaml file:

name: hello-world
version: "1"
summary: A toy hello-world program
description: |
  This repository contains a toy hello world program
  packaged as a snap.
base: core22
confinement: strict
apps:
  hello-world:
    command: usr/bin/hello-world
parts:
  hello-world:
    source: .
    plugin: make

You can put this in a directory. Run snapcraft --verbose to build and sudo snap install --dangerous ./hello-world_1_amd64.snap to install it. The architecture will be different if you are not on an x86 system.

2 Likes

Cannot thank you enough for taking the time to provide the detailed guidance. Everything worked as you specified with my first pass - perhaps an achievement for me in a long time.

Since I can relate to Makefile, your example will be the foundation for teaching some high school kids.

Warmest regards!

2 Likes