Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: LiK on May 19, 2009, 07:13:27 pm

Title: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 19, 2009, 07:13:27 pm
Hello everyone :)

I think that my C::B is compiling in K&R and not in ASNI C..

ie. with this code
Code
#include <stdlib.h>

int main (void) {

    char *pfilename;
    pfilename = malloc( 1 );

return 0;
}

returns
Quote
error: invalid conversion from `void*' to `char*'

Of course this
Code
pfilename = (char*)malloc( 1 );
compiles without errors.. But this is not ANSI C right?


I tried selecting an other compiler from C::B option but not luck.
Even tried the devC++ compiler but nothing..

Any ideas? what is wrong?


Thank you very much!
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: rcoll on May 19, 2009, 07:46:39 pm
The "flavor" of C being compiled is a function of the compiler, not the IDE.  Depending upon the compiler you choose and the options you select (specific to that compiler) you will get different modes of compiling.  Note that (AFAIK) the Dev-C++ IDE uses only the GCC compiler.

Ringo
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 19, 2009, 08:03:00 pm
Yes i am aware of that :)

But i have GNU GCC Compiler selected at C::B compiler options and i tried the same flags..

So i guess there is also something else i need to change in the IDE's option.

Thx for the reply
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: AlexN on May 19, 2009, 08:31:20 pm
Quote
error: invalid conversion from `void*' to `char*'

Of course this
Code
pfilename = (char*)malloc( 1 );
compiles without errors.. But this is not ANSI C right?


Thank you very much!

I my opinion this is behavior is more ANSI C then K&R. ANSI C checks the type, K&R C assigns all to all.  :)

Alex  ;)
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 19, 2009, 08:37:48 pm
well ANSI C does not need any casting to mallocs
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: rcoll on May 19, 2009, 08:51:48 pm
By default the GCC C-compiler operates in "C99" mode, which does require a cast of one pointer type to another (in this case a "void *" to a "char *").  C99 is neither K&R nor ANSI, but has elements of both.

However, at this point we are no longer talking about C::B, but the multitude of C standards out there.  You could get better help by asking the guys on USENET on the "comp.lang.c" list.

Ringo
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 19, 2009, 09:04:57 pm
r u sure about this?

Anyway.. Even so the problem still remains..

Not solved even when i put the -ansi flag at the compiler options.

So i still think it has something to do with C::B. I hadn't that problem with DevC++ or Pelles C.


Thanks
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: rcoll on May 19, 2009, 09:39:36 pm
Actually, I copied your original code into a brand new C::B "console" project, and it compiled and ran just fine.  No problems at all.  Apparentely the GCC optimizer is smart enough to translate a "void *" into a "char *" with no problems.

Are you sure you are compiling a "C" project, and not a "C++" project?  Is your main program named "main.c" or "main.cpp" ?

Ringo
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 19, 2009, 09:48:18 pm
(!) (!)
Yes i am.. I posted that only this small part for illustration reasons.

the whole program is much bigger with not other errors.. Just 2 errors of this kind..

What is more i reinstalled C::B and same happened.. :(
any ideas?


thx 4 helping :)
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: rcoll on May 19, 2009, 10:02:22 pm
When I create a default C-language project, the only compiler options I have checked is optimization "O2", and your code snippet compiled good.

What options do you have checked?  Also, what version of GCC are you using?  My system is using GCC 3.4.4

Ringo
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 19, 2009, 10:17:21 pm
not sure.. in the options its GNU GCC Compiler

i have tried with various flags checked. and i just tried only with checking O2 but no difference
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: rcoll on May 19, 2009, 10:38:20 pm
Even with -pedantic warnings turned on, I cannot get it to fail:


mingw32-gcc.exe -pedantic -W -Wall -ansi  -O2     -c C:\temp\test1\main.c -o obj\Release\main.o
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
 
Can you show us your full compiler log output?

Ringo
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 19, 2009, 10:57:42 pm
sure, but its actually just that
Code
\f.c        In function `int main()':
\f.c   6   error: invalid conversion from `void*' to `char*'
             === Build finished: 1 errors, 0 warnings ===
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: rcoll on May 19, 2009, 11:18:26 pm
Please turn on full-command-line logging (Settings -> Compiler and Debugger -> Other Settings then "Compiler Logging") then try to compile again.  Copy that log output into here, so we can see what all (hidden) settings are being used.

Also, can you open a command window (for example, a DOS window or a MSys xterm) and type "gcc -v" to see what version of GCC you are using.

Ringo
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 19, 2009, 11:35:44 pm
ok i enabled full-command-line logging but the message is exactly the same as above..

i open a cmd window and type gcc -v but it is not found..(i guess because its not in the windows directory)
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: Jenna on May 20, 2009, 12:05:32 am
ok i enabled full-command-line logging but the message is exactly the same as above..

Please post the commandline !!!
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: rcoll on May 20, 2009, 12:07:42 am
Did you see my example above, where it showed the compiler being called?  Yours has it also (somewhere).  That is the command I need to see.

For the version number ... open the command window, then CD to the directory with the gcc compiler.  Then issue the command "gcc -v".  I need to know what version of gcc you  are using.

Ringo
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 20, 2009, 09:51:21 am
ok gcc version is 3.4.2 (mingw-special).

ok i see your line. its the build log. Here is mine
[ 50.0%] mingw32-g++.exe -pedantic -Wall -ansi -g    -c "C:\...\1.c" -o "C:\...\1.o"

it is using g++ instead of gcc.. why?
In compiler settings mingw32-gcc.exe is selected as default C compiler.
mingw32-g++.exe is selected for C++.

The file is blabla.c and when i create a new one i select C, not C++..


Thx
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: rcoll on May 20, 2009, 06:50:04 pm
The g++ compiler of course expects all input files to be be C++ files; I'm not sure how you got to this point.

Go to Settings->Compiler and Debugger and on the "Selected Compiler" list select the compiler you are using (probably called GNU GCC Compiler), then click the tab called "Toolchain executables".  There will be both a C compiler and a C++ compiler listed there.  Make sure that "mingw32-gcc.exe" is listed for the C compiler.

Ringo
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 20, 2009, 07:54:01 pm
Thing is that i did not change anything there..

yes i already checked there and its mingw32-gcc.exe as C compipler.

But for some reason it uses the mingw32-g++.exe compiler which is the selection for C++.

It will only work if i put mingw32-gcc.exe as C++ compiler(!)

Seems like it cannot see that my file is a C file (despite the fact that its .c)
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: MortenMacFly on May 21, 2009, 02:13:10 pm
Seems like it cannot see that my file is a C file (despite the fact that its .c)
Look at the file's (!) properties what compiler environment is selected there. Maybe you changed that by accident.
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 21, 2009, 02:43:20 pm
what do u mean? where do i find this?
file-> properties? (nothing strange there..)


Thank you
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: MortenMacFly on May 21, 2009, 07:03:53 pm
what do u mean? where do i find this?
File -> properties -> tab "advanced" -> compiler variable and compiler. Do *not* modify just post what's written there.
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 22, 2009, 12:22:01 am
erm.... nothing?!!

check the image attached

[attachment deleted by admin]
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: MortenMacFly on May 22, 2009, 06:47:06 am
erm.... nothing?!!
...but you*did* create a project? Or do you try to compile a single file? If so - create a project. ;-)
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 22, 2009, 06:13:51 pm
erm... no i didn't :shock:

i tried with project and the file compiles jsut fine! thank you!

But again.. why it did not compile as a single file? More specifically why it does compile but uses the wrong compiler?
Has to do with the project thing?

ie In ms visual C++ IDE u cannot (i think:P) compile a single file, *but* the compile etc buttons are non-clickable..
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: MortenMacFly on May 25, 2009, 07:15:49 am
why it does compile but uses the wrong compiler?
This is by design. Supporting single-file compilation has limitations (for several good reasons). Actually I would vote to disable it completely to avoid such misunderstandings.
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: LiK on May 25, 2009, 12:09:36 pm
hm.. ok now i understand :)

btw.. better disable it completely or warn that it only compiles C++ single files!
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: Ceniza on May 31, 2009, 03:46:23 pm
Quote from: BloodyCake
Actually I would vote to disable it completely to avoid such misunderstandings.

I would vote to improve it (that's what I use the most of C::B) :D
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: stahta01 on June 01, 2009, 01:16:05 am
Quote from: BloodyCake
Actually I would vote to disable it completely to avoid such misunderstandings.

I would vote to improve it (that's what I use the most of C::B) :D

I would vote to have an option to enable it; that by default is disabled.
Tim S
Title: Re: C::B compiles in K&R C and not ANSI C...?
Post by: MortenMacFly on June 02, 2009, 08:14:55 am
Quote from: BloodyCake
Actually I would vote to disable it completely to avoid such misunderstandings.
I would vote to improve it (that's what I use the most of C::B) :D
I would vote to have an option to enable it; that by default is disabled.
I change my vote to have an "all-in-one device suitable for every purpose" a.k.a. "Swiss Army knife". I am already developing the coffee maker plugin... stay tuned. :lol: