compile gcc for armv7l from debian in linux - c

I'm using GCC version 10.2.1 in Linux Debian (x86-64).
i write this c code:
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Now my question is how can compile and run this code for armv7l
?
Target system Is a evaluation board
thank you

you can use arm-linux-gnueabihf-gcc hello.c -o hello command . and befor you should install arm-linux-gnueabihf-gcc in your linux system .
here is full sample that work for me

Related

How can I fix exec format error on raspberry pi 4

I am trying to compile code on the raspberry pi 4 using ubuntu server 20.04.1 LTS. I am using gcc to compile it and every time I try and run the file after it gets compiled successfully it says
-bash: ./out: cannot execute binary file: Exec format error
When I do the file command on out I get, and I know that the ARM cpu is 64bit
out: ELF 64-bit LSB relocatable, ARM aarch64, version 1(SYSV), not stripped
This is the source that I am trying to run
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("Hello World!");
return 0;
}
This is the gcc command I am running
gcc -march=native -ctest.c -oout
It's a "LSB relocatable" file, which is not executable since it hasn't been linked because the -c in your command command gcc -march=native -ctest.c -oout stands for "compile and assemble only, do not link":
$ gcc --help
<...>
-c Compile and assemble, but do not link.
<...>
You should compile everything into an executable:
gcc -march=native test.c -o out

Coverity Scan fails to build <stdlib.h> with _GNU_SOURCE defined

The Coverity Scan Build Tool fails to compile any C file that includes <stdlib.h> on Ubuntu 18.04 when _GNU_SOURCE is defined:
$ cat > main.c
#include <stdlib.h>
int main() {
}
$
$ gcc -D_GNU_SOURCE=1 -o main main.c
$
$ /opt/cov-analysis/bin/cov-build --dir cov-int gcc -D_GNU_SOURCE=1 -o main main.c
Coverity Build Capture (64-bit) version 2017.07 on Linux 4.15.0-20-generic x86_64
...
[WARNING] Emitted 0 C/C++ compilation units (0%) successfully
...
$
The same build works perfectly on Ubuntu 16.04 or without _GNU_SOURCE defined:
$ /volatile/local/cov-analysis/bin/cov-build --dir cov-int gcc -o main main.c
Coverity Build Capture (64-bit) version 2017.07 on Linux 4.15.0-20-generic x86_64
...
Emitted 1 C/C++ compilation units (100%) successfully
...
$
How to get Coverity Scan to build C sources with _GNU_SOURCEdefined on Ubuntu 18.04?
For those interested file cov-int/build-log.txt can be found here:
https://gist.github.com/DimitriPapadopoulos/0dcd9018eed26401cc6095087d9cc1d5
After contacting Coverity support, it appears this is known bug. They suggested I work around it by switching from the default Ubuntu 18.04 compiler (GCC 7) to the previous version (GCC 6):
sudo apt install gcc-6
Indeed _Float32, _Float32x, _Float64, _Float64x and _Float128 were introduced in GCC 7.
Coverity is failing to define the types GCC would define, but then it's claiming to be GCC anyway. Here's a workaround: https://gist.github.com/vathpela/0cede6d6eb5b0ec0791c6afc4282c340#file-fix_coverity-h
Just be sure you do:
#include "fix_coverity.h"
before stdlib.h gets included, whether directly or indirectly.

Link libsndfile source code with gcc

I just cloned libsndfile and created program.c file with the following contents:
#include <stdio.h>
#include <sndfile.h>
main() {
printf("hello world");
}
My goal is to get this file to compile using gcc (if indeed sndfile.h is the right header to include), however, I am not competent with c code nor the gcc compiler.
So far I've been referencing the gcc docs and the libsndfile FAQ and haven't been able to come up with a solution. I'm not sure if I even have a 'library' yet to feed the gcc -l option. Do I need to go through some sort of build process with the libsndfile source code first or can I 'link' to .c files?
So far, with program.c and the libsndfile clone directory in the same directory, I've tried the following:
gcc 'pkg-config --cflags libsndfile' program.c -o hello
gcc 'pkg-config --cflags ./libsndfile/sndfile.pc.in' program.c -o hello
I'm coding on my raspberry pi model B and did not install libsndfile, so, nothing is on my path... maybe I should rename sndfile.pc.in to sndfile.pc and somehow put it on my path? (not a linux guru either!)
If there are some resources you think I should study please comment! I'm perfectly willing to accept answers that simply push me enough to figure things out. Any help would be much appreciated.
First make sure you have gcc installed:
sudo apt-get install gcc
Then, install libsndfile1-dev:
sudo apt-get install libsndfile1-dev
I slightly fixed you code to avoid some warnings:
#include <stdio.h>
#include <sndfile.h>
int main(void) {
printf("hello world\n");
return 0;
}
Now you can compile:
gcc -o hello program.c
Execution:
bebs#rasp:~$ ./hello
hello world
bebs#rasp:~$
A small supplement of #alpereira7's answer.
It would cause linking failure without its lib. gcc -lsndfile -o hello program.c should be a 100% solution.

Windows 10 can compile C programs with gcc but will not run

I installed gcc on my Windows 10 PC and can compile C code into an exe from the command prompt eg.
gcc HelloWorld.c -o HelloWorld
however when I try to run using
HelloWorld
the program will run however nothing will be output and it won't do anything
This is the C program that I compile and run that does nothing which doesn't have any errors that I can see
#include <stdio.h>
int main(void) {
printf("Hello from C!");
return 0;
}
Can someone point out whats happening?
Compile your source code by:
gcc HelloWorld.c -o ./HelloWorld.exe
and then run it by:
./HelloWorld.exe
If it still fails, you can run dir to see what's in your current directory.

strerror with MinGW-w64

Take this simple program
#include <stdio.h>
#include <string.h>
#include <errno.h>
int
main (void)
{
printf ("ERROR %d %s\n", ETIMEDOUT, strerror (ETIMEDOUT));
return 0;
}
If you compile it with Cygwin gcc it runs fine
$ gcc a.c
$ ./a
ERROR 116 Connection timed out
If you compile it with MinGW-w64 gcc it does not give proper error message
$ i686-w64-mingw32-gcc a.c
$ ./a
ERROR 138 Unknown error
How can I get MinGW-w64 to put correct error message?
ETIMEDOUT seems to be a POSIX extension to the ISO C standard errno.h. Cygwin has better support for POSIX than MinGW. A bug report about ETIMEDOUT for mingw32 was opened and closed in 2007.
One option is to use the GNU Portability Library (Gnulib). It provides a POSIX-like errno.h and strerror()/strerror_override() .

Resources