1

So far I've been working with g++ (on Windows), but for some reasons I want to use Code:Blocks to use sfml library (quite easy configuration, contrary to clean g++).

I use bison to create file.tab.c and file.tab.h (from file.y), I use flex for lex.yy.c (from file.l) and i have my own files containing functions needed for my project (file.cpp and file.h). So with g++ I compile it like that:

g++ lex.yy.c file.tab.c file.cpp -o file.exe

and it works fine. Now i want to compile these files with Code:Blocks and i get a strange error:

file.l:4:18: fatal error: cstdio: No such file or directory

Why on earth does it signal an error in file.l when this file isn't even included to my project (it just served to create lex.yy.c which i attached to project)? And I'm not sure about that but... main() is in file.tab.c so this is an entry point - can I specifically ask Code:Blocks to compile everything with g++ like I did in command line and there would be a chance for it to work? I have the latest version of Code:Blocks. Thanks in advance

//Edit: I can also add that i get the same error when i compile it from command line like that: gcc lex.yy.c.

user2443194
  • 67
  • 3
  • 11
  • 2
    `.c` files should be compiled with `gcc` not `g++`! Just compile separately and link the `.o` results in an extra step. – πάντα ῥεῖ May 28 '14 at 15:02
  • Well it worked in cmd perefectly - why shouldn't work now? And how can I compile files seperately and link them later in Code:Blocks? – user2443194 May 28 '14 at 15:05
  • I used a rock once to hammer a nail. Just because something _worked before_ does not mean it is the best way. If you want to compile your C files as C++ then you should set cmake such that your files belong to C++ language: `set_source_files_properties(file.c PROPERTIES LANGUAGE CXX )` – ryyker May 28 '14 at 15:12
  • So what about that: http://stackoverflow.com/questions/778431/how-do-i-use-c-in-flex-and-bison?rq=1 ? He sais that it's just enough to use g++ instead of gcc and it works... – user2443194 May 28 '14 at 15:19

2 Answers2

2

To make all the source files in a C::B project be compiled as C++, regardless of file extension and regardless of whether gcc or g++ is invoked, add the option -x c++ in YourProject -> Build options... -> Compiler settings -> Other options.

That forces C++ compilation and should solve your problem, but just in case it is not clear to you, the error:

fatal error: cstdio: No such file or directory

is occurring because the C++ header <cstdio> is being #include-ed in a compilation that is being compiled as C, and that header is not in the C compiler's include-search paths.

The C compiler is being chosen based on the file extension (.c) of the file being compiled when the error occurs. The -x c++ option overrides this, but you might look for a better solution by investigating how it happens that a C++ header is getting included in a C source file, and correcting that if you can.

The C header equivalent to <cstdio> is <stdio.h>. Can you fix the problem simply by changing <cstdio> to <stdio.h> somewhere? If you use the -x c++ solution then it stops you from ever adding a source file to the project that should be compiled as C.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
0

Check whether all the standard library files are all included in your project; For help on linking files, refer this.

It could be more better if you include more details.

Community
  • 1
  • 1
  • Well, my project works when i compile it with g++, like I said earlier. I just want to compile it in Code:Blocks the same way like in cmd - I honestly have no clue why these files compiled together with g++ work, and seperately they don't... (it also happens in cmd) – user2443194 May 28 '14 at 15:56