gam2046
Posts: 2
Joined: Fri Jan 22, 2016 7:05 am

How to cross-compile and debug a C program?

Mon Jan 25, 2016 8:32 am

Hello everyone,

I'm new for C/C++, but I have a mission have to do with C. So is there any way to develop and debug a small program with eclipse (or any other IDE)? My purpose is to develop / debug on my computer, and do not keep the operation on Raspberry.

Thanks !

User avatar
joan
Posts: 14566
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to corss-compile and debug a C program?

Mon Jan 25, 2016 9:03 am

Does your program use any Raspberry Pi specific features?

Which operating system are you going to use to host the cross-compile?

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

Re: How to corss-compile and debug a C program?

Mon Jan 25, 2016 9:45 am

My purpose is to develop / debug on my computer, and do not keep the operation on Raspberry.
Is there any particular reason why you want to develop the code on you PC?
The Raspberry Pi is itself a highly capable "computer" easily able to develop / debug a small C program.

Because the Pi is credit card sized and very cheap, I think some people automatically disregard the Pi as a development environment and want to cross compile (or they are used to Arduino development).
For a small project you will likely spend more frustrating time setting up a cross compiling IDE than doing the actual development on the Pi itself.

Make sure you are using a Pi2 and use "make" effectively and you can develop some very large programs on the Pi.

A case for using the PC is if your program is portable - most likely meaning it doesn't use the GPIO (which PC's don't have), and, better, your PC runs Linux, not Windows. Then you can debug on the PC and occasionally build it on the Pi for testing.

Anything less than a few thousand lines of code you might as well do on the Pi.
The Pi comes with the development tools and compilers pre-installed (use Raspbian Jessie).
Simple IDE's such as geany are just an apt-get away.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: How to corss-compile and debug a C program?

Mon Jan 25, 2016 10:23 am

I agree, at least on a Raspi 2B I'm also fine with Geany for on-board editing and compiling.
Having said this I must admit that on a Raspi B+ it was a torture though and that I had wished to have a crosscompiler, too...
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

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

Re: How to corss-compile and debug a C program?

Mon Jan 25, 2016 11:02 am

Time to compile a large single source file (not recommended - use make -j4 with multiple small files).
15,400 lines of code.

4GHz PC 1.63 sec
1GHz Pi2 20.08sec
1GHz Pi0 52.05 sec

Of course the quad core Pi2 would do better with make -j which can compile multiple source files at once using all the cores.
And again with multiple source files, make will only re-compile those files that were changed since the last build (unless you change a header file with lots of dependencies).
Last edited by jahboater on Mon Jan 25, 2016 11:12 am, edited 2 times in total.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: How to corss-compile and debug a C program?

Mon Jan 25, 2016 11:08 am

the compiling itself was not a problem to me, but editing and debugging code by the Geany Editor!
Really a torture was scrolling through the code and re-editing in case of compiler errors (and warnings) and improving the source code.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

gam2046
Posts: 2
Joined: Fri Jan 22, 2016 7:05 am

Re: How to corss-compile and debug a C program?

Mon Jan 25, 2016 11:59 am

Thanks everyone.

I was a Java developer, but now there is a function due to the efficiency of I/O (frequent control GPIO), had to use C to achieve.
In fact, I just want to be able to debug programs on Raspberry (such as setting breakpoints) with a simple method. I don't know for what reason, I reaction in the GUI is always very slow (HDMI output and VNC remote control are the same). So I hope to cross compile and remote debugging (in fact, cross compilation is not necessary, because the program is very small)

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

Re: How to corss-compile and debug a C program?

Mon Jan 25, 2016 12:19 pm

For debugging use "gdb" which is included with raspbian.

If there is no documentation (man pages) for gdb or gcc do:-

Code: Select all

sudo apt-get update
sudo apt-get install gcc-4.9-doc gdb-doc
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

HankB
Posts: 125
Joined: Fri Jan 01, 2016 2:45 pm

Re: How to cross-compile and debug a C program?

Fri Jan 29, 2016 4:06 am

I did some development for Arduino which I tested on Linux where I had a much richer development environment. When it came to arduino specific features, I provided stubs that would provide the appropriate feedback to the code I was developing. There are usually macros provided by the compiler that can identify the target (or you can provide them in your build environment) that will select the appropriate code for the test vs. production environment.

Of course as others have said, do not rule out development hosted on the Pi itself. That was not an option for the Arduino.

One other option to explore could be to host Eclipse on a PC and debug remotely on the Pi. I think that Eclipse supports that but have not explored that myself.

My gut feeling is that you can come up to speed a lot faster hosting development right on the Pi but if you're really good with Eclipse, it might pay to put the extra effort into getting it to hosted on the PC and debugging remotely on the Pi. I see that git is already installed on the Pi so sharing code between a PC and the Pi would be easy using that.

HTH,
hank

Return to “C/C++”