strerror with MinGW-w64 - c

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() .

Related

Compiling with gcc in macOS Mojave

Before updating to Mojave I was compiling C programs just fine. I used an older version of gcc, 7.3, that I installed using the instructions found here.
Then I updated to Mojave and tried compiling the simple program that follows with gcc main.c:
#include <stdio.h>
int main(){
printf("Hello World\n");
return 0;}
This results in the following error:
/usr/local/lib/gcc/x86_64-apple-darwin17.5.0/7.3.0/include-fixed/stdio.h:78:10: fatal error: _stdio.h: No such file or directory
#include <_stdio.h>
^~~~~~~~~~
compilation terminated.
If I remove the include it will compile with implicit declaration warnings for printf, but will still compile and run properly, printing Hello World. Does anyone know the issue and how I can fix it?
I figured out how to fix it. I went to
/Library/Developer/CommandLineTools/Packages/
then opened and installed macOS_SDK_headers_for_macOS_10.14.pkg.

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.

Using gcc to compile userspace application using linux kernel headers

I have a really simple c program that I want to compile using gcc, importing from linux kernel headers.
#include <stdio.h>
#include <stdlib.h>
#include <linux/random.h>
int main(){
int rand;
get_random_bytes(&rand,sizeof(rand));
printf("%d",rand);
return 0;
}
I have tried to compile this program using the following command:
gcc rand.c -D__KERNEL__ -isystem /lib/modules/`uname -r`/build/include
But I get a bunch of errors (below). What am I missing?:
/usr/src/kernels/4.9.8-201.fc25.x86_64/include/linux/linkage.h:7:25: fatal error: asm/linkage.h: No such file or directory
#include <asm/linkage.h>
From some quick Google searches, it seems like get_random_bytes might be a private function only usable from within the kernel.
How about you try using getrandom instead? Here is the documentation of getrandom:
http://man7.org/linux/man-pages/man2/getrandom.2.html

F_SEAL_SEAL undeclared, even when headers are included

I'm trying to use file sealing on Linux. Here's an example C program.
#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
int main(void) {
(void)F_SEAL_SEAL;
}
You can build it using gcc -Wall -o ./linux_file_sealing linux_file_sealing.c or similar.
When I build it, I get an error about F_SEAL_SEAL.
gcc -Wall -o ./linux_file_sealing linux_file_sealing.c
linux_file_sealing.c: In function ‘main’:
linux_file_sealing.c:7:19: error: ‘F_SEAL_SEAL’ undeclared (first use in this function)
printf("%d\n",F_SEAL_SEAL);
^
linux_file_sealing.c:7:19: note: each undeclared identifier is reported only once for each function it appears in
I'm including unistd.h and fcntl.h, as per the man page... so what else should I be doing, and where is that described?
(The man pages just say that sealing is "Linux-specific", but give no further details. This is the reason for including the GNU_SOURCE define, which is how you get the other Linux-specific stuff, but for F_SEAL_SEAL it seems to make no difference.)
(Ubuntu 16.04 LTS, Linux 4.4.0-36)
You want
#include <linux/fcntl.h>
instead of
#include <fcntl.h>

Is there a command that tells the compiler to print its version?

I need to upload my assignments to an online compiler, I was told it's GCC but I'm getting segfault on the online compiler but not when compiling with VS or on linux's GCC.
Is there a way to make compiler print what compiler is it and its version?
usually there isn't a single command.
you can try and check compiler defined macros.
cmake does this, it has a wide array of checks to detect compiler versions.
It compiles code and prints a "vendor string" based on preprocessor symbols.
here is for instance the code for gcc: https://github.com/Kitware/CMake/blob/master/Modules/Compiler/GNU-DetermineCompiler.cmake
since clang is drop in replacement for gcc you might also want to check the macros used here:
https://github.com/Kitware/CMake/blob/master/Modules/Compiler/Clang-C-FeatureTests.cmake
Edit:
So a running C example would do the following:
#include <stdio.h>
int main(int argc, char **argv) {
#ifdef __clang_major__
printf ("clang detected version %d.%d\n", __clang_major__, __clang_minor__);
#endif
#ifdef __GNUC__
// note that clang 3.7 declares itself as a gcc 4.2"
printf ("gcc detected version %d.%d\n", __GNUC__, __GNUC_MINOR__);
#endif
}
output for clang:
$ clang main.cc
$ ./a.out
clang detected version 3.7
gcc detected version 4.2
output for gcc:
$ gcc main.cc
$ ./a.out
gcc detected version 4.8

Resources