Creating a Project

Project > New.  Give the project a name and choose where to save it.

File > New with template > main.c

Save it as main.c

As a good starting point copy the bcm2835 blink project sample code into your new main.c file and connect a LED positive pin to Pin 11 of your RPi P1 header:


#include <bcm2835.h>

// Blinks on RPi pin GPIO 11
#define PIN RPI_GPIO_P1_11

int main(int argc, char **argv)
{
    if (!bcm2835_init())
	return 1;

    // Set the pin to be an output
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);

    // Blink
    while (1)
    {
	// Turn it on
	bcm2835_gpio_write(PIN, HIGH);
	
	// wait a bit
	delay(500);
	
	// turn it off
	bcm2835_gpio_write(PIN, LOW);
	
	// wait a bit
	delay(500);
    }
    return 0;
}

Compiling A Simple Single File Project

Compile – Compile the currently selected source file into a binary object file.

Build – Link the currently selected source files object file into an executable (compile it first if the object files don't exist)

This can be all you need for simple projects or where you edit the Geany project build properties. However as you will see if you try and compile the sample blink project code above, changes are already needed to include the bc2835 library object file. Its worth getting straight into using a makefile so that using multiple files in a project is really easy.

Compiling A Multi File Project

Geany doesn't compile projects it compiles files. To solve this you need to use a makefile. Geany's "make" command will use the make file called "makefile" by default, so you can simply give your makefile that name and save it in the same folder as your project files.

Important note – there is a tab before the gcc or g++ command in the makefile. There must be a tab (not spaces) at the beginning of any command.  Make will not work properly if it's not there.

An example simple makefile for a project with a single file called main.c, to show you the basics of what a makefile does:


all: output_file_name

output_file_name: main.o
	gcc main.o -lbcm2835 -o output_file_name

main.o: main.c
	gcc -c main.c

clean:
	rm -rf *o output_file_name

The "-lbcm2835" assumes you are using the Mike McCauley bcm2835 library for IO control, if not just remove it.

A really useful makefile for geany C or C++ projects

This is a more powerful solution to use instead based on excellent examples here. Just copy the following into a new file and save it as "makefile" in your projects folder, then edit the settings for your project.


#This sample makefile has been setup for a project which contains the following files: main.h, ap-main.c, ap-main.h, ap-gen.c, ap-gen.h   Edit as necessary for your project

#Change output_file_name.a below to your desired executible filename

#Set all your object files (the object files of all the .c files in your project, e.g. main.o my_sub_functions.o )
OBJ = ap-main.o ap-gen.o

#Set any dependant header files so that if they are edited they cause a complete re-compile (e.g. main.h some_subfunctions.h some_definitions_file.h ), or leave blank
DEPS = main.h ap-main.h ap-gen.h

#Any special libraries you are using in your project (e.g. -lbcm2835 -lrt `pkg-config --libs gtk+-3.0` ), or leave blank
LIBS = -lbcm2835 -lrt

#Set any compiler flags you want to use (e.g. -I/usr/include/somefolder `pkg-config --cflags gtk+-3.0` ), or leave blank
CFLAGS = -lrt

#Set the compiler you are using ( gcc for C or g++ for C++ )
CC = g++

#Set the filename extensiton of your C files (e.g. .c or .cpp )
EXTENSION = .cpp

#define a rule that applies to all files ending in the .o suffix, which says that the .o file depends upon the .c version of the file and all the .h files included in the DEPS macro.  Compile each object file
%.o: %$(EXTENSION) $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

#Combine them into the output file
#Set your desired exe output file name here
output_file_name.a: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

#Cleanup
.PHONY: clean

clean:
	rm -f *.o *~ core *~ 

The "-lbcm2835" assumes you are using the Mike McCauley bcm2835 library for IO control, if not just remove it. 'lrt assumes you are using the rt library.

As you add new .c and .h files to your project just add their names to the makefile OBJ and DEPS defines and they will be included when you make the project.

Now instead of compiling or building your project, use Menu > Build > Make (SHIFT+F9) to cause Geany to run the makefile and if no errors are found it will produce your exe ready to run.

Issues

Ensure your makefile is saved if you've made any changes!

Have you set the .c or .cpp define correctly.  If not Geany will build that source file instead using its default settings but it won't give you an error message to tell you its doing that.

There is a tab before the gcc or g++ command in the makefile. There must be a tab (not spaces) at the beginning of any command.  Make will not work properly if it's not there.

Need the rt library including in LIBS= ?

LIBS=-lbcm2835 -lrt

Lots of errors like this: "undefined reference to `std::allocator<char>::allocator()'​"

You have a C++ project and need to select g++ instead of gcc in two places in the makefile (or you can sometimes add this -lstdc++ to the makefile LIBS= if you want to use gcc)

Running Make from the command line instead of in Geany

Go to the projects directory and enter the command:


make

Running Your Exe

Using LXTerminal change to your project directory (e.g. "cd projects/myproject1")

Run the exe by typing "./output_file_name.a"

If you get a permissions denied error (i.e. your project uses the IO pins) then run it as the root user by using "sudo ./output_file_name"

If your project doesn't exit you can use CTRL+SHIFT+C to forcibly exit it in LXTerminal.

Makefile Resources

http://mrbook.org/tutorials/make/
http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/
http://www.gnu.org/software/make/manual/make.html

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. Bash

    4 years ago

    I was having the same problem as Janos, a quick solution is to add -l bcm2835 to both the compiler and build commands under Build->Set Build Commands if you’re using Geany. Also helps to add sudo to the execute line

  2. Janos

    9 years ago

    Hi I got somer problem after installing bcm2835-1.49 on my RPi2. Messages:
    undefined reference to bcm2835_init; bcm2835_gpio_fsel; and so on to bcm2835_delay.
    Could anyone give me help?

  3. fred flintstone

    12 years ago

    There is an error, the line LIBS should be:

    LIBS=-lbcm2835 -lrt

    Then it compiles OK without ‘undefined reference to clock_gettime’
    Thanks for the great tutorials – they have helped me a lot getting going compiling C on my Pi!

  4. Pingback: .Getting Your RPi Ready For C Programming « Raspberry Pi Projects

Comments

Your email address will not be published. Required fields are marked *