errors as i use the restrict qualifier - c

When I compile the following program I get errors :
gcc tester.c -o tester
tester.c: In function ‘main’:
tester.c:7:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_X’
tester.c:7:17: error: ‘ptr_X’ undeclared (first use in this function)
tester.c:7:17: note: each undeclared identifier is reported only once for each function it appears in
tester.c:10:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_Y’
tester.c:10:17: error: ‘ptr_Y’ undeclared (first use in this function)
#include <stdio.h>
int main() {
int x = 10;
int y = 20;
int *restrict ptr_X;
ptr_X = &x;
int *restrict ptr_Y;
ptr_Y = &y;
printf("%d\n",*ptr_X);
printf("%d\n",*ptr_Y);
}
Why am I getting these errors ?

Not all compilers are compliant with the C99 standard. For example Microsoft's compiler, does not support the C99 standard at all. If you are using MSVC on a x86 platform you will not have access to this critical optimization option.
When using GCC, remember to enable the C99 standard by adding -std=c99 to your compilation flags. In code that cannot be compiled with C99, use either __restrict or __restrict__ to enable the keyword as a GCC extension.
From here.

Restrict is part of C99, and therefore you have to compile it as a C99 program by specifying -std=c99 flag to gcc.
gcc -std=c99 tester.c -o tester

Related

Why am i getting error in compiling c file in linux os?

I am new to linux environment and i am trying to compile c file in linux OS but i'm getting so many errors. I searched on internet but didn't get any solution.
here is my file "first.c" :
#include<stdio.h>
int main()
{
printf("Hello world");
return 0;
}
error i am getting :
┌──(dj㉿kali)-[~/Desktop/c]
└─$ gcc -o output first.c
In file included from /usr/local/include/corecrt.h:10,
from /usr/local/include/crtdefs.h:10,
from /usr/local/include/conio.h:9,
from first.c:1:
/usr/local/include/_mingw.h:272:2: error: #error Only Win32 target is supported!
272 | #error Only Win32 target is supported!
| ^~~~~
In file included from /usr/local/include/corecrt.h:10,
from /usr/local/include/crtdefs.h:10,
from /usr/local/include/conio.h:9,
from first.c:1:
/usr/local/include/_mingw.h:580:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__debugbreak’
580 | void __cdecl __debugbreak(void);
These are only few lines of errors i am getting. there are numerous lines of errors i am getting.I'm using linux terminal to compile the file. Please help me to solve this problem. Thanks in advance.

Undeclared variable error when compiling HPC Challenge Benchmark

I want to compile and run HPC Challenge benchmark on my server.
When I want to make hpcc-1.5.0 with mpicc compiler I get this error:
../../../../STREAM/stream.c:178:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘a’
static double * restrict a, * restrict b, * restrict c;
^
../../../../STREAM/stream.c: In function ‘checkSTREAMresults’:
../../../../STREAM/stream.c:263:16: error: ‘a’ undeclared (first use in this function)
if (fabs(a[j]/aj-1.0) > epsilon) {
^
../../../../STREAM/stream.c:263:16: note: each undeclared identifier is reported only once for each function it appears in
../../../../STREAM/stream.c:280:16: error: ‘b’ undeclared (first use in this function)
if (fabs(b[j]/bj-1.0) > epsilon) {
^
../../../../STREAM/stream.c:297:16: error: ‘c’ undeclared (first use in this function)
if (fabs(c[j]/cj-1.0) > epsilon) {
^
../../../../STREAM/stream.c: In function ‘computeSTREAMerrors’:
../../../../STREAM/stream.c: In function ‘HPCC_Stream’:
../../../../STREAM/stream.c: In function ‘tuned_STREAM_Copy’:
../../../../STREAM/stream.c: In function ‘tuned_STREAM_Scale’:
../../../../STREAM/stream.c: In function ‘tuned_STREAM_Add’:
../../../../STREAM/stream.c: In function ‘tuned_STREAM_Triad’:
make[1]: *** [../../../../STREAM/stream.o] Error 1
make[1]: Leaving directory `/root/hpcc-1.5.0/hpl/lib/arch/build'
make: *** [all] Error 2
It seems STREAM uses some variables that is not declared.
This is because of using restrict keywords for the array. To compile stream, you have to use -std=c99 option.
Add -std=c99 flag in your Make.$(arch) file in CCFLAGS section. Like this:
CCFLAGS = $(HPL_DEFS) -fomit-frame-pointer -O3 -funroll-loops -std=c99
Source: https://anilmaurya.wordpress.com/2016/04/26/hpc-1-5-0-compile-time-error/

mex doesn't recognize inline

I'm trying to compile a matlab wrapper for libdc1394 which is a library for firewire cameras.
I get a strange error involving some inline functions in a header file.
I'm working on ubuntu 12.04 with gcc-4.6. I've compiled other mex files with success. This wrapper was also difficult to compile in mac.
/usr/local/MATLAB/R2012b//bin/mex dc1394mex.c `pkg-config --libs --cflags libdc1394-2` -o dc1394.mexa64
Warning: You are using gcc version "4.6.3-1ubuntu5)". The version
currently supported with MEX is "4.4.6".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
In file included from /usr/include/dc1394/dc1394.h:53:0,
from dc1394mex.c:23:
/usr/include/dc1394/register.h:47:1: error: unknown type name ‘inline’
/usr/include/dc1394/register.h:47:29: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘dc1394_get_register’
/usr/include/dc1394/register.h:62:1: error: unknown type name ‘inline’
/usr/include/dc1394/register.h:62:29: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘dc1394_set_register’
/usr/include/dc1394/register.h:82:1: error: unknown type name ‘inline’
/usr/include/dc1394/register.h:82:29: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘dc1394_get_control_register’
/usr/include/dc1394/register.h:97:1: error: unknown type name ‘inline’
/usr/include/dc1394/register.h:97:29: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘dc1394_set_control_register’
/usr/include/dc1394/register.h:117:1: error: unknown type name ‘inline’
/usr/include/dc1394/register.h:118:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘dc1394_get_adv_control_register’
/usr/include/dc1394/register.h:133:1: error: unknown type name ‘inline’
/usr/include/dc1394/register.h:134:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘dc1394_set_adv_control_register’
dc1394mex.c: In function ‘command_init_camera’:
dc1394mex.c:133:12: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
dc1394mex.c:152:12: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
dc1394mex.c: In function ‘dcframe_to_mxframe’:
dc1394mex.c:571:1: error: expected expression before ‘/’ token
dc1394mex.c:572:1: error: expected expression before ‘/’ token
dc1394mex.c:573:1: error: expected expression before ‘/’ token
mex: compile of ' "dc1394mex.c"' failed.
make: *** [dc1394.mexa64] Error 1
Any ideas?
Well Simons proposal was correct. Only catch I didn't know was that you can't give cflags directly to mex. You have to alter your mexopts.sh file.
Also I had to remove -ansi flag because it contradicts with c99.
How to specify C99 via CFLAGS modification on the command line:
mex -v -largeArrayDims CFLAGS="\$CFLAGS -std=C99" dc1394mex.c

C Making volatile sig_atomic_t Global in Header File

I'm trying to make a global variable, initialized in my msh.c file as:
volatile sig_atomic_t sig_int = 0;
This alone seems to be fine. However if I go to my proto.h file(which is included in all the c files for this project), and type:
extern volatile sig_atomic_t sig_int;
It throws a bunch of errors:
gcc -c -Wall msh.c arg_parse.c builtin.c expand.c
In file included from arg_parse.c:5:
proto.h:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘sig_int’
In file included from builtin.c:13:
proto.h:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘sig_int’
In file included from expand.c:11:
proto.h:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘sig_int’
make: *** [msh.o] Error 1
How exactly do I make this variable global? Thanks.
Your proto.h file needs to include <signal.h> so that the sig_atomic_t type is defined, that's all.

Still the "error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token"

This is a small pure C program illustrative of the problem. The program doesn't do anything; it's a stripped-down version of a larger program that exhibits the same problem.
Here is the scenario:
Mac OS X Lion;
gcc version i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00);
Sample code:
#include <stdlib.h>
#include <stdio.h>
char huge *pbA;
char huge *pbB;
int main(int argc,char *argv[])
{
pbA = (char huge *)farmalloc(2);
pbB = pbA;
*(pbB++) = 'A';
return( 0 );
}
Compile command:
gcc -c -Wall -O -g -pipe -D_SDL myTest.c
Errors messages:
myTest.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
myTest.c:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
myTest.c: In function ‘main’:
myTest.c:10: error: ‘pbA’ undeclared (first use in this function)
myTest.c:10: error: (Each undeclared identifier is reported only once
myTest.c:10: error: for each function it appears in.)
myTest.c:10: error: expected ‘)’ before ‘huge’
myTest.c:10: warning: implicit declaration of function ‘farmalloc’
myTest.c:11: error: ‘pbB’ undeclared (first use in this function)
So, what am I missing?
I'm not sure what/where huge is, but the compiler can't find it in what you've given it (i.e. you're missing a header probably). As for farmalloc, that looks to be in <alloc.h>. Now about using these, there is an answer on another site for huge:
Keywords like near/far/huge were once used as memory models in the old MSDOS
days when computers had a max of 640K memory.
Any machine built in the last 15 years does not have that restriction so
unless you have a real issue where you have to use really obsolete hardware,
I would not spend time with segmented memory model syntax.
huge, and probably farmalloc as well, seem to be deprecated by today's standards (just like far vs near pointers). Using just char * and malloc should be all you need; no strange, old headers.

Resources