Luka147
Posts: 1
Joined: Tue Aug 13, 2019 11:24 am

Ways to compile C/C++ code for pi

Tue Aug 13, 2019 11:27 am

I want to buy a raspberry pi soon but before that, I need to know what are the ways to compile code for arm architecture.
If you guys know any ways to do so, thank you in advance.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12353
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Ways to compile C/C++ code for pi

Tue Aug 13, 2019 12:19 pm

yes of course you can.

here is how:
https://www.youtube.com/watch?v=Rv41Gtn ... lKNsidxfwX

jahboater
Posts: 4770
Joined: Wed Feb 04, 2015 6:38 pm

Re: Ways to compile C/C++ code for pi

Tue Aug 13, 2019 12:30 pm

An excellent C and C++ compiler is included, pre-installed, and ready to use.

Create the usual little test program with any editor in a file called "hello.c"

Code: Select all

#include <stdio.h>

int
main( void )
{
  puts( "hello world!" );
}
Compile it with:

Code: Select all

cc hello.c -o hello
and finally, you can run the program as many times as you like with:

Code: Select all

./hello

deepo
Posts: 199
Joined: Sun Dec 30, 2018 8:36 pm

Re: Ways to compile C/C++ code for pi

Tue Aug 13, 2019 12:38 pm

I'm not quire sure what it is you're asking for, but the easiest way it to use the Raspbian OS which comes with g++ installed.
If you choose the desktop (not Lite version) you will be able to easily edit and compile C/C++ code on you Raspberry.
It is of course also possible with the console only Lite version, but it is easier to have a window for compiling along with your editor.

The most basic syntax of g++ is:

Code: Select all

g++ -o example example.cpp
For C code you'll need the gcc compiler:

Code: Select all

gcc -o hello main.c
Sorry - I can't remember which compilers are included on the Raspbian OS images. But cc is installed by default, as jahboater writes above.

There are cross compiler options that allow you to do everything on e.g. Windows and then transfer the executable to the Raspberry Pi. But I haven't used them. I think JetBrains can do that with their IDE CLion.

/Mogens

jahboater
Posts: 4770
Joined: Wed Feb 04, 2015 6:38 pm

Re: Ways to compile C/C++ code for pi

Tue Aug 13, 2019 12:48 pm

The compiler installed by default is called GCC.
It is a very popular and capable compiler.
The letters GCC stand for Gnu Compiler Collection.
According to "gcc -v", it will compile:

C
Ada
C++
Go
Fortran
Objective C

and supports the latest standards, such as: C18, C++17, Fortran 2018

moirainesedai
Posts: 4
Joined: Mon Aug 26, 2019 6:35 pm

Re: Ways to compile C/C++ code for pi

Tue Aug 27, 2019 3:04 am

Also highly recommend clang/LLVM. In most cases they can be used as drop-in replacement for gcc.

User avatar
Gavinmc42
Posts: 3925
Joined: Wed Aug 28, 2013 3:31 am

Re: Ways to compile C/C++ code for pi

Tue Aug 27, 2019 3:24 am

It seems like almost all languages can compile on Pi's.
I have used C, C++, D, Go, Rust, Free Pascal, Pony, Luajit.....
Then there are ones that are interpreted, Python, Lua, TCL, ....

I normally use Geany as my default editor/IDE, it know most languages.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

cmrincon
Posts: 40
Joined: Thu May 24, 2018 7:39 pm

Re: Ways to compile C/C++ code for pi

Wed Aug 28, 2019 4:34 pm

Cross compilation is an option.

I use Visual Studio 2017, im sure that you can google how to set up your VS in case you decide to use it.

Snerler
Posts: 9
Joined: Wed Feb 10, 2016 2:20 pm

Re: Ways to compile C/C++ code for pi

Thu Aug 29, 2019 3:00 pm

cmrincon wrote:
Wed Aug 28, 2019 4:34 pm
Cross compilation is an option.

I use Visual Studio 2017, im sure that you can google how to set up your VS in case you decide to use it.
Do you have a good guide for this?

User avatar
DougieLawson
Posts: 36326
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Ways to compile C/C++ code for pi

Thu Aug 29, 2019 5:45 pm

You need to call the Emergency Services, your local Google is in need of medical attention.

https://docs.microsoft.com/en-us/cpp/li ... ew=vs-2019
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

cmrincon
Posts: 40
Joined: Thu May 24, 2018 7:39 pm

Re: Ways to compile C/C++ code for pi

Thu Aug 29, 2019 5:54 pm

Download Visual Studio Comunity 2019:
https://visualstudio.microsoft.com/es/downloads/

Instalation:
https://docs.microsoft.com/en-us/cpp/li ... ew=vs-2019

Create the project:
https://devblogs.microsoft.com/cppblog/ ... al-studio/

omit gdbserver instalation as in raspbian is installed by default.
The tutorial is targeted to linux machines but you can notice that in the create project window there is a "blink (raspberry)" option which is the one i recommend you as a hello world program.

blink (raspberry) option will configure VS to work with raspberry and you will only need to enter hostname, username and password of the raspberry account. Don't try to configure VS from scratch, it can be a headache for newbies because there are soo many options.

You can get the hostname of the raspberry by tiping in the console "hostname"
You can get the username of the raspberry by tiping "whoami"
Default port is 22.

Aditional resources:
https://docs.microsoft.com/en-us/visual ... ew=vs-2019

Return to “C/C++”