How can I create a string constant in gcc 4.9.2? - c

I'm running on Arch Linux with GCC 4.9.2, and I've been having trouble compiling the following code:
#ifndef WORLD_H
#define WORLD_H
#include <string.h>
#include <stdio.h>
//#include "removeBuffering.h"
//World dimensions
#define WORLD_WIDTH 80
#define WORLD_HEIGHT 20
//World block types
#define FLAT_LAND '-'
//Instructions
#define MOVE_UP 'w'
#define MOVE_DOWN 's'
#define MOVE_RIGHT 'd'
#define MOVE_LEFT 'a'
#ifndef WIN32
#define COMMAND "clear" //Clears a linux console screen
#else
#define COMMAND "cls" //Clears a windows console screen
#endif
#define wipe() system( COMMAND )
It works on my koding.com VM, which uses GCC 4.8.2 but on my local machine, it generates the following error:
include/world.h:17:17: error: expected declaration specifiers or ‘...’ before string constant
#define COMMAND "clear" //Clears a linux console screen
I'm thinking it's due to some sort of change in GCC 4.9, but I can't seem to find any good information about it, so any help would be greatly appreciated

Run this through gcc -E -- this will expand the results, at which point all should become clear.

Before I give my own answer, I want to give you an overview of how my code looked while it was generating the error message above. Here's world.h:
#ifndef WORLD_H
#define WORLD_H
#include <string.h>
#include <stdio.h>
//#include "removeBuffering.h"
//World dimensions
#define WORLD_WIDTH 80
#define WORLD_HEIGHT 20
//World block types
#define FLAT_LAND '-'
//Instructions
#define MOVE_UP 'w'
#define MOVE_DOWN 's'
#define MOVE_RIGHT 'd'
#define MOVE_LEFT 'a'
#ifndef WIN32
#define COMMAND "clear" //Clears a linux console screen
#else
#define COMMAND "cls" //Clears a windows console screen
#endif
int cursorXPos;
int cursorYPos;
char world[WORLD_HEIGHT][WORLD_WIDTH+1]; //Space for null terminator
void initializeWorld();
void printWorld();
void getInput();
//void printHelp();
#endif
Here's world.c (I've emptied the functions to save space)
#include "world.h"
void initializeWorld()
{
}
void printWorld()
{
}
void getInput()
{
}
system(COMMAND);
printWorld();
And here is the FULL error list provided by GCC:
In file included from src/world.c:1:0:
include/world.h:17:17: error: expected declaration specifiers or ‘...’ before string constant
#define COMMAND "clear" //Clears a linux console screen
^
src/world.c:78:10: note: in expansion of macro ‘COMMAND’
system(COMMAND);
^
src/world.c:79:3: warning: data definition has no type or storage class
printWorld();
^
src/world.c:79:3: error: conflicting types for ‘printWorld’
src/world.c:13:6: note: previous definition of ‘printWorld’ was here
void printWorld()
In my experience, it was always a good idea to deal with the very first error on the list, so I didn't pay much attention to anything besides the first error, which is why I asked the question in the first place. I eventually tried to resolve the later errors as Carey Gregory and immibis suggested.
The important ones were:
src/world.c:79:3: warning: data definition has no type or storage class
printWorld();
^
src/world.c:79:3: error: conflicting types for ‘printWorld’
src/world.c:13:6: note: previous definition of ‘printWorld’ was here
void printWorld()
As soon as I moved the misplaced function call for printWorld() (and system()), the errors disappeared.

Related

Why is #define throwing "error: expected declaration specifiers"?

I'm new so apologies if my formatting is a little off.
I'm writing a simple OpenMP program in order to get the hang of it, and I've been stopped completely dead in my tracks by a strange compilation error. My serial implementation compiles just fine (with gnu11), but my parallel compilation seems to fail for some reason I can't locate.
The entire code up to the point of failure is as follows (and the error I receive from make follows thereafter)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define N_THR 1
#define MIN_SIZE 3 //NOTE: min_size is inclusive here
#ifdef _OPENMP
#include <omp.h>
#undef N_THR
#define N_THR 4
#undef MIN_SIZE
#define MIN_SIZE N_THR
//omp_set_dynamic(false); //we need to explicitly disable dynamic teams to force 4 threads
omp_set_num_threads(N_THR);
#endif
gcc maze.c -o maze
gcc maze.c -fopenmp -o mazep
maze.c:11:16: error: expected declaration specifiers or ‘...’ before numeric constant
#define N_THR 4
^
maze.c:15:22: note: in expansion of macro ‘N_THR’
omp_set_num_threads(N_THR);
^~~~~
make: *** [Makefile:5: parallel] Error 1
Is there some deep C language syntax hint I'm missing or is it something a little more obvious?
It's because you are calling omp_set_num_threads() outside of a function.
I got this error in this file canCom2.h
in this line
#define INST_CANCOM2 (2U)
because I had an extra closing curly braces in this file
eeeprom.c
} /* extra curly braces*/
#if defined(USING_CAN2) && defined(INST_CANCOM2)
After removing the extra curly brace, the error vanished

#include <windows.h> causes errors

I'm trying to use GetTickCount() from the windows.h header file. Right now my code looks like this:
#include <stdio.h>
#include <string.h>
#include "declarations.h"
#define INPUTBUFFER 400 * 6
#define START_POS "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
#include <windows.h>
void interface() {
.....
.....
}
The compiler returns 2 errors:
Expected '{' before '(' token (pointing to the '(' after "interface"
2 or more data types in declaration specifiers (pointing to "interface")
When I comment out #include < windows.h >, the interface function works just fine. I don't even have GetTickCount() in my code yet. I checked the gcc path to make sure windows.h is there, so I'm not sure why I'm getting this error.
As RbMm points out in a comment, the windows headers contain "#define interface struct", so the compiler sees your code as "void struct() { ....}" which is not valid C++
You need to either rename your function (probablly the better option) or add a "#undef interface" before your function.

Redefined symbol in multiple c code with #ifndef directive

I have a stupid problem and I don't see where it comes from. I took care of using #ifndef directive to make sure all my #include are not redefined. Sadly for three of them that's happening. Here my multiple files arch :
t_include.h
#ifndef T_INCLUDE_H_
#define T_INCLUDE_H_
/* Project specific dependencies*/
#include "utilities.h"
#include "fsp_function.h"
#include "ti/csl/csl_tsc.h"
#include "ti/csl/csl_cache.h"
#include "ti/csl/csl_cacheAux.h"
#include "ti_sp_complex_convolution_A_input1.h"
#include "ti_sp_complex_convolution_A_input2.h"
#include "to_sp_complex_convolution_A_output.h"
#endif /* T_INCLUDE_H_ */
t_function.h
#ifndef T_FUNCTION_H_
#define T_FUNCTION_H_
#include "t_include.h"
/*output vector*/
#define INPUT1A_LENGTH 5000
#define INPUT2A_LENGTH 2800
#define OUTPUTA_LENGTH 2202
extern FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
/*misc parameter*/
#define CPU_CLOCK_KHZ 1400000
#define CPU_CLOCK_MS 1/CPU_CLOCK_KHZ
#define FIR_NB_MACS INPUT1A_LENGTH * OUTPUTA_LENGTH /* FIR algorithm complexity */
#define NB_OF_REP 10
#define UMA_L2CACHE_L1DCACHE 0
/* Project specific types */
typedef struct{
ect...
And now c file only include t_function.h :
t_function.c
/* Dependencies */
#include "t_function.h"
FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
/* API */
etc...
And t_main_function.c
/* dependencies */
#include "t_function.h"
void main(void) {
etc...
It should work but during linking here the errors comming :
<Linking>
error #10056: symbol "sp_complex_convolution_A_output" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
error #10056: symbol "sp_complex_convolution_A_input2" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
error #10056: symbol "sp_complex_convolution_A_input1" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
error #10056: symbol "sp_complex_convolution_A_output_thales" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
>> Compilation failure
error #10010: errors encountered during linking; "CONVOLUTION_COMPLEX.out" not built
So the error only com from three symbol sp_complex_convolution_A_output, sp_complex_convolution_A_input1 and sp_complex_convolution_A_input2 Which are defined in their own .h which is also protected by #ifndef directives:
ti_sp_complex_convolution_A_input1.h
#ifndef __TI_SP_COMPLEX_CONVOLUTION_A_INPUT1_H_
#define __TI_SP_COMPLEX_CONVOLUTION_A_INPUT1_H_
FLOAT32 sp_complex_convolution_A_input1[2 * 2500] = {
etc...
And the same for the other two...
So I really don't know why it is happening.
Thx for helping
Definitions like:
FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
should go into a source file.
The header files should contain only declarations like:
extern FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
As a rule of thumb, do no put anything that allocates memory into header files.

An interesting thing about overriding linux kernel macro

In the following code, it is compiled success and print 1024
#include <stdio.h>
#define FD_SETSIZE 512
#include <sys/types.h>
int main()
{
printf("%d\n", FD_SETSIZE);
}
But in the following code, it is compiled failed and print
test.c:4:1: warning: "FD_SETSIZE" redefined
In file included from /usr/include/sys/types.h:220,
from test_fd.c:3:
/usr/include/sys/select.h:81:1: warning: this is the location of the previous definition
the code is
#include <stdio.h>
#include <sys/types.h>
#define FD_SETSIZE 512
int main()
{
printf("%d\n", FD_SETSIZE);
}
Can anbody explain this? Thanks!
But in the following code, it is compiled failed and print
In the question, both the programs were compiled, but while compiling first program you got warnings in preprocessor stage.
Preprocessor stage is responsible for the replacement of macros.
In this example the preprocessor is using the last defined macro and replacing it.
#include <stdio.h>
#define FD_SETSIZE 512
#include <sys/types.h>
Here the definition of FD_SETSIZE is there in both the .c file and also in header file sys/types.h.
After the file inclusion, then the replacing of macros will be done,so the latest defined macro is replaced.
So the final replacement FD_SETSIZE of will be same as defined in the sys/types.h file and vice-versa.
Hope this is helpful.
you can use the #undef directive to remove the defined macro and replace it later
#ifdef MACRO
#undef MACRO
#endif
#define MACRO

Conflicting types in flex on Linux - Windows ok

Some lines of my flex file:
%{
#include <stdlib.h>
#include <string.h>
#include "types.h"
#define NO_YY_UNPUT
/* #define YY_NEVER_INTERACTIVE */
extern char *strdup(const char *);
short unsigned int yylineno = 1;
%}
{ID} {
yylval.txt = strdup(yytext);
return ID;
};
\n { ++yylineno; }
My code looks good but I have problem when i want to compile on Ubuntu. In windows everything is okay but on linux I have errors like:
lex.l:10:14: error: expected identifier or ‘(’ before ‘__extension__’
lex.l:12:20: error: conflicting types for ‘yylineno’
lex.c:355:5: note: previous definition of ‘yylineno’ was here
Line 10: extern char *strdup(const char *);
Line 12: short unsigned int yylineno = 1;
strdup is declared in string.h, but it is a Posix interface and you should define an appropriate feature test macro before including any system header:
%top {
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include "types.h"
}
(Note: Using %top forces the enclosed code to be inserted at the top of the generated C file, in order to provide the guarantee that the feature test macro is defined before any system header.)
I don't know if that works on Windows (and it certainly depends on your compiler and toolchain), so you might need to declare strdup on that platform. If so, make sure you surround the declaration with a preprocessor test for the build environment.)
The error at line 10 is probably the result of strdup being defined as a macro. I'm not sure under what conditions that will happen -- it will be some GNU extension mode -- but defining the Posix feature test macro should prevent it.
As for the error with the type of yylineno, there is a simple solution: don't declare yylineno. It is declared in the code flex generates (and it may be declared as a macro if you ask for a re-entrant -- "pure" -- lexer).

Resources