gregoff
Posts: 31
Joined: Fri Aug 22, 2014 7:07 am

Getting started with C

Tue Jan 24, 2017 1:06 pm

Hi all,

I have a Python app which I want to rewrite into C. The problems I hope to solve by this is the following:
  • Portability. With C i can code/compile using my x86 server and then cross compile it for ARM and there fore making my app available to RPi as well as other ARM based systems. Is this possible?
  • Standalone binary. With gcc I can compile my C code into a standalone binary without any dependencies, correct? I want to make my app to be able to run regardless of what's preinstalled.
Am I on the right track with my hopes above?

Coming from a semi-programmer background, but not used C before, I need a little help to get started. Do anyone know any good tutorials to help me get started with the following:
  • Working with simple arrays.
  • Read text files from system and parse them (explode strings and put substrings to an array).
  • Working with http post (can be done with libcurl or should I use something else?)
  • Creating small functions files to be included with simple mathematical functions.
As you probably can tell, it's not super advanced stuff I want to do.

Any input is appreciated!

User avatar
PeterO
Posts: 5879
Joined: Sun Jul 22, 2012 4:14 pm

Re: Getting started with C

Tue Jan 24, 2017 1:19 pm

I would say start with the MagPi book for basics.

https://www.raspberrypi.org/magpi/issue ... ials-c-v1/

If you write your code with portability in mind, compiling and running on ARM or X86 is easily achived.

As for avoiding "dependencies", well there is a large set of libraries that are installed by default on all Linux machines. You can use features of cmake to test that libraries are
available before trying to compile with them. Unless you are doing something "off the beaten track" in terms of libraries you use it shouldn't be a problem.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gregoff
Posts: 31
Joined: Fri Aug 22, 2014 7:07 am

Re: Getting started with C

Tue Jan 24, 2017 1:56 pm

PeterO wrote:I would say start with the MagPi book for basics.

https://www.raspberrypi.org/magpi/issue ... ials-c-v1/

If you write your code with portability in mind, compiling and running on ARM or X86 is easily achived.

As for avoiding "dependencies", well there is a large set of libraries that are installed by default on all Linux machines. You can use features of cmake to test that libraries are
available before trying to compile with them. Unless you are doing something "off the beaten track" in terms of libraries you use it shouldn't be a problem.

PeterO
That link looks exactly what I'm looking for!

I don't mind having dependencies that are in fact standard parts of a linux system, but not more. In short, when making Python scripts I'll have to depend on that Python (in the correct version) is installed on the target system, which I cannot rely on in this case.

Basically, i want to develop on a debian based system on x86 and want it to be a able to run on a debian based system on a ARM (regardless of if it is a RPi or a Pine64, BananaPro etc). Can gcc handle that?

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: Getting started with C

Tue Jan 24, 2017 2:33 pm

I wouldn't do this.

And I'm speaking as someone who knows nothing about Python - and what little I do know, is mostly negative. Starting with having the indentation determine program structure. This is just wonky. I understand where it is coming from, but it is wonky.

And as someone who has used C for decades - and is entirely comfortable with it. But if I were starting out today, I don't think I'd learn C. When I started, it was all there was (pretty much). In terms of mainstream industry, both C and C++ are considered "legacy".

But, nowadays, Python is pretty advanced and portable. I'd be surprised if it was any less portable than C on "normal" hardware/platforms. I suppose you might have an issue if you want it to run on IBM mainframes.

And Python has various "compile" thingies that, to my limited understanding, make it almost equivalent to C in terms of speed/efficiency.

And that's all I have to say about that.
If this post appears in the wrong forums category, my apologies.

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Getting started with C

Tue Jan 24, 2017 2:47 pm

gregoff wrote: I don't mind having dependencies that are in fact standard parts of a linux system, but not more.
Yes. If you have to install a library or a tool on Linux, its an indication that its possibly an unwanted dependency that might, or might not, always be available.
gregoff wrote:Basically, i want to develop on a debian based system on x86 and want it to be a able to run on a debian based system on a ARM (regardless of if it is a RPi or a Pine64, BananaPro etc). Can gcc handle that?
GCC can of course. It supports the latest language standards and is widely available. Also many of its extensions are supported by other compilers (not that you should use them for strict portability). But in the end, its up to you! You have to learn to write portable code, avoid any implementation defined, or undefined behaviour, and so on. Be aware that the sizes of basic items can change (on your x86 system some things will probably be 64-bit whereas on the Pi (for now) they would be 32-bits, easy to deal with, but you need to be aware).
For your simple program it should be very easy.

You can develop the code on the fast x86 machine and regularly copy the C source files and the makefile to a Pi and build/test it there which should spot any problems early on.

User avatar
PeterO
Posts: 5879
Joined: Sun Jul 22, 2012 4:14 pm

Re: Getting started with C

Tue Jan 24, 2017 3:01 pm

jahboater wrote: Be aware that the sizes of basic items can change (on your x86 system some things will probably be 64-bit whereas on the Pi (for now) they would be 32-bits, easy to deal with, but you need to be aware).
For your simple program it should be very easy.
Use explicit integer size types where this might become an issue.

Code: Select all

#include <stdint.h>
gives you uint8_t,uint16_t,uint32_t and uint64_t for unsigned and int8_t,int16_t,int32_t and int64_t for signed values.
You can develop the code on the fast x86 machine and regularly copy the C source files and the makefile to a Pi and build/test it there which should spot any problems early on.
Exactly what I'm doing while updating some code to work with gtk3 from gtk2.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Getting started with C

Tue Jan 24, 2017 8:47 pm

And last, but not least, make sure it compiles cleanly on both platforms with at least:

Code: Select all

-Wall -Wextra -Wconversion
You could add a few to that for good luck ... ;)

Code: Select all

-Wall -Wextra -Wconversion -Wcast-align -Wcast-qual -Warray-bounds -Wstrict-aliasing=3 -Wstrict-overflow=1 -Wuninitialized -Wwrite-strings -Wlogical-op -Wunused -Wformat=2 -Wpointer-arith
Last edited by jahboater on Tue Jan 24, 2017 11:31 pm, edited 1 time in total.

gregoff
Posts: 31
Joined: Fri Aug 22, 2014 7:07 am

Re: Getting started with C

Tue Jan 24, 2017 10:27 pm

Not sure that I understood a lot of your compiling tips. I've managed to compile with gcc for the platform I'm currently on but thats it.

User avatar
PeterO
Posts: 5879
Joined: Sun Jul 22, 2012 4:14 pm

Re: Getting started with C

Tue Jan 24, 2017 10:29 pm

The point of using all those gcc warning options (-Wxxxxxx) is to catch some of things in your code that could cause portability problems.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “C/C++”