{"id":179,"date":"2012-10-10T11:44:28","date_gmt":"2012-10-10T11:44:28","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=179"},"modified":"2014-09-26T15:05:29","modified_gmt":"2014-09-26T15:05:29","slug":"creating-a-project","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/compilers-and-ides\/geany\/creating-a-project","title":{"rendered":"Creating A Geany Project"},"content":{"rendered":"<h4>\nCreating a Project<br \/>\n<\/h4>\n<p>\nProject &gt; New. &nbsp;Give the project a name and choose where to save it.\n<\/p>\n<p>\nFile &gt; New with template &gt; main.c\n<\/p>\n<p>\nSave it as main.c\n<\/p>\n<p>\nAs 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:\n<\/p>\n<pre>\r\n<code>\r\n#include &lt;bcm2835.h&gt;\r\n\r\n\/\/ Blinks on RPi pin GPIO 11\r\n#define PIN RPI_GPIO_P1_11\r\n\r\nint main(int argc, char **argv)\r\n{\r\n    if (!bcm2835_init())\r\n\treturn 1;\r\n\r\n    \/\/ Set the pin to be an output\r\n    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);\r\n\r\n    \/\/ Blink\r\n    while (1)\r\n    {\r\n\t\/\/ Turn it on\r\n\tbcm2835_gpio_write(PIN, HIGH);\r\n\t\r\n\t\/\/ wait a bit\r\n\tdelay(500);\r\n\t\r\n\t\/\/ turn it off\r\n\tbcm2835_gpio_write(PIN, LOW);\r\n\t\r\n\t\/\/ wait a bit\r\n\tdelay(500);\r\n    }\r\n    return 0;\r\n}\r\n<\/code><\/pre>\n<h4>\nCompiling A Simple Single File Project<br \/>\n<\/h4>\n<p style=\"padding-left: 30px;\">\nCompile &#8211; Compile the currently selected source file into a binary object file.\n<\/p>\n<p style=\"padding-left: 30px;\">\nBuild &#8211; Link the currently selected source files object file into an&nbsp;executable&nbsp;(compile it first if the object files don&#39;t exist)\n<\/p>\n<p>\nThis 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.\n<\/p>\n<h4>\nCompiling A Multi File Project<br \/>\n<\/h4>\n<p>\nGeany doesn&#39;t compile projects it compiles files. To solve this you need to use a makefile. Geany&#39;s &quot;make&quot; command will use the make file called &quot;makefile&quot; by default, so you can simply give your makefile that name and save it in the same folder as your project files.\n<\/p>\n<p>\n<em><strong>Important note &#8211;&nbsp;there is a tab before the gcc&nbsp;or g++ command in the makefile. There must be a tab (not spaces) at the beginning of any command. &nbsp;Make will not work properly if it&#39;s not there.<\/strong><\/em>\n<\/p>\n<p>\nAn example simple makefile for a project with a single file called main.c, to show you the basics of what a makefile does:\n<\/p>\n<pre>\r\n<code>\r\nall: output_file_name\r\n\r\noutput_file_name: main.o\r\n\tgcc main.o -lbcm2835 -o output_file_name\r\n\r\nmain.o: main.c\r\n\tgcc -c main.c\r\n\r\nclean:\r\n\trm -rf *o output_file_name\r\n<\/code><\/pre>\n<p>\nThe &quot;-lbcm2835&quot; assumes you are using the Mike McCauley bcm2835 library for IO control, if not just remove it.\n<\/p>\n<h5>\nA really useful makefile for geany&nbsp;C or C++ projects<br \/>\n<\/h5>\n<p>\nThis is a more powerful solution to use instead based on excellent examples <a href=\"http:\/\/www.cs.colby.edu\/maxwell\/courses\/tutorials\/maketutor\/\" target=\"_blank\">here<\/a>. Just copy the following into a new file and save it as &quot;makefile&quot; in your projects folder, then edit the settings for your project.\n<\/p>\n<pre>\r\n<code>\r\n#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\r\n\r\n#Change output_file_name.a below to your desired executible filename\r\n\r\n#Set all your object files (the object files of all the .c files in your project, e.g. main.o my_sub_functions.o )\r\nOBJ = ap-main.o ap-gen.o\r\n\r\n#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\r\nDEPS = main.h ap-main.h ap-gen.h\r\n\r\n#Any special libraries you are using in your project (e.g. -lbcm2835 -lrt `pkg-config --libs gtk+-3.0` ), or leave blank\r\nLIBS = -lbcm2835 -lrt\r\n\r\n#Set any compiler flags you want to use (e.g. -I\/usr\/include\/somefolder `pkg-config --cflags gtk+-3.0` ), or leave blank\r\nCFLAGS = -lrt\r\n\r\n#Set the compiler you are using ( gcc for C or g++ for C++ )\r\nCC = g++\r\n\r\n#Set the filename extensiton of your C files (e.g. .c or .cpp )\r\nEXTENSION = .cpp\r\n\r\n#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\r\n%.o: %$(EXTENSION) $(DEPS)\r\n\t$(CC) -c -o $@ $&lt; $(CFLAGS)\r\n\r\n#Combine them into the output file\r\n#Set your desired exe output file name here\r\noutput_file_name.a: $(OBJ)\r\n\t$(CC) -o $@ $^ $(CFLAGS) $(LIBS)\r\n\r\n#Cleanup\r\n.PHONY: clean\r\n\r\nclean:\r\n\trm -f *.o *~ core *~ \r\n<\/code><\/pre>\n<p>\nThe &quot;-lbcm2835&quot; assumes you are using the Mike McCauley bcm2835 library for IO control, if not just remove it. &#39;lrt assumes&nbsp;you are using the rt&nbsp;library.\n<\/p>\n<p>\nAs you add new .c and .h files to your project just add their names to the makefile&nbsp;OBJ and DEPS&nbsp;defines and they will be included when you make the project.\n<\/p>\n<p>\nNow instead of compiling or building your project, use Menu &gt; Build &gt; Make (SHIFT+F9) to cause Geany to run the makefile and if no errors are found it will produce your exe ready to run.\n<\/p>\n<h5>\nIssues<br \/>\n<\/h5>\n<p>\nEnsure your&nbsp;makefile&nbsp;is saved if you&#39;ve made any changes!\n<\/p>\n<p>\nHave you set the .c or .cpp define correctly. &nbsp;If not Geany will build that source&nbsp;file instead using its default settings but it won&#39;t give you an error message to tell you its doing that.\n<\/p>\n<p>\nThere 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. &nbsp;Make will not work properly if it&#39;s not there.\n<\/p>\n<p>\nNeed the rt library including in&nbsp;LIBS= ?\n<\/p>\n<p style=\"margin-left: 40px;\">\nLIBS=-lbcm2835 -lrt\n<\/p>\n<p>\nLots of errors like this: &quot;undefined reference to `std::allocator&lt;char&gt;::allocator()&#39;\u200b&quot;\n<\/p>\n<p style=\"margin-left: 40px;\">\nYou have a C++ project and need to select g++ instead of gcc&nbsp;in two places in the makefile (or you can sometimes add this -lstdc<span style=\"font-size: 10px;\">++&nbsp;<\/span>to the makefile&nbsp;LIBS= if you want to use gcc)\n<\/p>\n<h4>\nRunning Make from the command line instead of in&nbsp;Geany<br \/>\n<\/h4>\n<p>\nGo to the projects directory and enter the command:\n<\/p>\n<pre>\r\n<code>\r\nmake\r\n<\/code><\/pre>\n<h4>\nRunning Your Exe<br \/>\n<\/h4>\n<p>\nUsing LXTerminal change to your project directory (e.g. &quot;cd projects\/myproject1&quot;)\n<\/p>\n<p>\nRun the exe by typing &quot;.\/output_file_name.a&quot;\n<\/p>\n<p>\nIf you get a permissions denied error (i.e. your project uses the IO pins) then run it as the root user by using &quot;sudo .\/output_file_name&quot;\n<\/p>\n<p>\nIf your project doesn&#39;t exit you can use CTRL+SHIFT+C to forcibly exit it in LXTerminal.\n<\/p>\n<h4>\nMakefile Resources<br \/>\n<\/h4>\n<p>\n<a href=\"http:\/\/mrbook.org\/tutorials\/make\/\" target=\"_blank\">http:\/\/mrbook.org\/tutorials\/make\/<\/a><br \/>\n<a href=\"http:\/\/www.cs.colby.edu\/maxwell\/courses\/tutorials\/maketutor\/\" target=\"_blank\">http:\/\/www.cs.colby.edu\/maxwell\/courses\/tutorials\/maketutor\/<\/a><br \/>\n<a href=\"http:\/\/www.gnu.org\/software\/make\/manual\/make.html\" target=\"_blank\">http:\/\/www.gnu.org\/software\/make\/manual\/make.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a Project Project &gt; New. &nbsp;Give the project a name and choose where to save it. File &gt; New with template &gt; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-geany"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":41,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":1576,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/179\/revisions\/1576"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}