On Thu, Sep 29, 2011 at 1:27 PM, Nilesh Govindarajan
<contact@nileshgr.com> wrote:
> Default function arguments in C are specified like this:
>
> int func(int a = 10) {} // just a dummy function
>
> Now I save that in a file called foo.c
>
> The above piece of code is valid in C as well as C++
>
> Now see this:
>
> nilesh@Linux ~ $ cat /tmp/foo.c
> int func(int a = 1) {}
> nilesh@Linux ~ $ gcc /tmp/foo.c
> /tmp/foo.c:1:16: error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token
> nilesh@Linux ~ $ g++ /tmp/foo.c
> /usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/../../../../lib64/crt1.o: In
> function `_start':
> (.text+0x20): undefined reference to `main'
> collect2: ld returned 1 exit status
> nilesh@Linux ~ $ gcc -v
> Using built-in specs.
> COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3/gcc
> COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.5.3/lto-wrapper
> Target: x86_64-pc-linux-gnu
> Configured with:
> /media/500GB/gentoo_portage/tmp/portage/sys-devel/gcc-4.5.3-r1/work/gcc-4.5.3/configure
> --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3
> --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include
> --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3
> --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/man
> --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/info
> --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4
> --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec
> --disable-fixed-point --without-ppl --without-cloog --disable-lto
> --enable-nls --without-included-gettext --with-system-zlib
> --disable-werror --enable-secureplt --enable-multilib
> --enable-libmudflap --disable-libssp --enable-libgomp --enable-cld
> --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/python
> --enable-checking=release --disable-libgcj
> --enable-languages=c,c++,fortran --enable-shared --enable-threads=posix
> --enable-__cxa_atexit --enable-clocale=gnu
> --with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo 4.5.3-r1
> p1.0, pie-0.4.5'
> Thread model: posix
> gcc version 4.5.3 (Gentoo 4.5.3-r1 p1.0, pie-0.4.5)
> nilesh@Linux ~ $ g++ -v
> Using built-in specs.
> COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3/g++
> COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.5.3/lto-wrapper
> Target: x86_64-pc-linux-gnu
> Configured with:
> /media/500GB/gentoo_portage/tmp/portage/sys-devel/gcc-4.5.3-r1/work/gcc-4.5.3/configure
> --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3
> --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include
> --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3
> --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/man
> --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/info
> --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4
> --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec
> --disable-fixed-point --without-ppl --without-cloog --disable-lto
> --enable-nls --without-included-gettext --with-system-zlib
> --disable-werror --enable-secureplt --enable-multilib
> --enable-libmudflap --disable-libssp --enable-libgomp --enable-cld
> --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/python
> --enable-checking=release --disable-libgcj
> --enable-languages=c,c++,fortran --enable-shared --enable-threads=posix
> --enable-__cxa_atexit --enable-clocale=gnu
> --with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo 4.5.3-r1
> p1.0, pie-0.4.5'
> Thread model: posix
> gcc version 4.5.3 (Gentoo 4.5.3-r1 p1.0, pie-0.4.5)
>
> Why is this happening? O_o
First guess, you need an int main() function, or you need to tell gcc
not to look for one.
--
:wq
09-29-2011, 05:43 PM
Todd Goodman
Strange GCC behavior
* Nilesh Govindarajan <contact@nileshgr.com> [110929 13:33]:
> Default function arguments in C are specified like this:
>
> int func(int a = 10) {} // just a dummy function
No they're not. C doesn't have default function arguments.
>
> Now I save that in a file called foo.c
>
> The above piece of code is valid in C as well as C++
Uh, no it's not.
>
> Why is this happening? O_o
Because it's not correct C.
Todd
09-29-2011, 05:47 PM
Nilesh Govindarajan
Strange GCC behavior
On Thu 29 Sep 2011 11:10:00 PM IST, Michael Mol wrote:
> On Thu, Sep 29, 2011 at 1:27 PM, Nilesh Govindarajan
> <contact@nileshgr.com> wrote:
>> Default function arguments in C are specified like this:
>>
>> int func(int a = 10) {} // just a dummy function
>>
>> Now I save that in a file called foo.c
>>
>> The above piece of code is valid in C as well as C++
>>
>> Now see this:
>>
>> nilesh@Linux ~ $ cat /tmp/foo.c
>> int func(int a = 1) {}
>> nilesh@Linux ~ $ gcc /tmp/foo.c
>> /tmp/foo.c:1:16: error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token
>> nilesh@Linux ~ $ g++ /tmp/foo.c
>> /usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/../../../../lib64/crt1.o: In
>> function `_start':
>> (.text+0x20): undefined reference to `main'
>> collect2: ld returned 1 exit status
>> nilesh@Linux ~ $ gcc -v
>> Using built-in specs.
>> COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3/gcc
>> COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.5.3/lto-wrapper
>> Target: x86_64-pc-linux-gnu
>> Configured with:
>> /media/500GB/gentoo_portage/tmp/portage/sys-devel/gcc-4.5.3-r1/work/gcc-4.5.3/configure
>> --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3
>> --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include
>> --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3
>> --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/man
>> --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/info
>> --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4
>> --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec
>> --disable-fixed-point --without-ppl --without-cloog --disable-lto
>> --enable-nls --without-included-gettext --with-system-zlib
>> --disable-werror --enable-secureplt --enable-multilib
>> --enable-libmudflap --disable-libssp --enable-libgomp --enable-cld
>> --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/python
>> --enable-checking=release --disable-libgcj
>> --enable-languages=c,c++,fortran --enable-shared --enable-threads=posix
>> --enable-__cxa_atexit --enable-clocale=gnu
>> --with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo 4.5.3-r1
>> p1.0, pie-0.4.5'
>> Thread model: posix
>> gcc version 4.5.3 (Gentoo 4.5.3-r1 p1.0, pie-0.4.5)
>> nilesh@Linux ~ $ g++ -v
>> Using built-in specs.
>> COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3/g++
>> COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.5.3/lto-wrapper
>> Target: x86_64-pc-linux-gnu
>> Configured with:
>> /media/500GB/gentoo_portage/tmp/portage/sys-devel/gcc-4.5.3-r1/work/gcc-4.5.3/configure
>> --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.3
>> --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include
>> --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3
>> --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/man
>> --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/info
>> --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4
>> --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec
>> --disable-fixed-point --without-ppl --without-cloog --disable-lto
>> --enable-nls --without-included-gettext --with-system-zlib
>> --disable-werror --enable-secureplt --enable-multilib
>> --enable-libmudflap --disable-libssp --enable-libgomp --enable-cld
>> --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.5.3/python
>> --enable-checking=release --disable-libgcj
>> --enable-languages=c,c++,fortran --enable-shared --enable-threads=posix
>> --enable-__cxa_atexit --enable-clocale=gnu
>> --with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo 4.5.3-r1
>> p1.0, pie-0.4.5'
>> Thread model: posix
>> gcc version 4.5.3 (Gentoo 4.5.3-r1 p1.0, pie-0.4.5)
>>
>> Why is this happening? O_o
>
> First guess, you need an int main() function, or you need to tell gcc
> not to look for one.
>
>
Syntax error doesn't depend on existence of main. I just posted this as
a snippet, I'd been working on one of my practical programs and
spotted this problem with a function I'd just made. And yeah, my
program had main().
--
Nilesh Govindarajan
http://nileshgr.com
09-29-2011, 05:50 PM
Michael Mol
Strange GCC behavior
On Thu, Sep 29, 2011 at 1:43 PM, Todd Goodman <tsg@bonedaddy.net> wrote:
> * Nilesh Govindarajan <contact@nileshgr.com> [110929 13:33]:
>> Default function arguments in C are specified like this:
>>
>> int func(int a = 10) {} // just a dummy function
>
> No they're not. *C doesn't have default function arguments.
That's another problem. (I don't know if gcc extends C to that end, though)
On Thu 29 Sep 2011 11:13:54 PM IST, Todd Goodman wrote:
> * Nilesh Govindarajan <contact@nileshgr.com> [110929 13:33]:
>> Default function arguments in C are specified like this:
>>
>> int func(int a = 10) {} // just a dummy function
>
> No they're not. C doesn't have default function arguments.
>
>>
>> Now I save that in a file called foo.c
>>
>> The above piece of code is valid in C as well as C++
>
> Uh, no it's not.
>
>>
>> Why is this happening? O_o
>
> Because it's not correct C.
>
> Todd
Gah! so that's the reason. I've been coding C++ and other languages
with default arguments that I forgot C.
--
Nilesh Govindarajan
http://nileshgr.com
09-29-2011, 06:00 PM
David W Noon
Strange GCC behavior
On Thu, 29 Sep 2011 22:57:25 +0530, Nilesh Govindarajan wrote about
[gentoo-user] Strange GCC behavior:
> Default function arguments in C are specified like this:
>
> int func(int a = 10) {} // just a dummy function
>
> Now I save that in a file called foo.c
>
> The above piece of code is valid in C as well as C++
Not in C90. The default grammar does not permit default values for
arguments in C, only C++.
> Why is this happening? O_o
Try adding -std=gnu99 as a compiler switch. That switches the grammar
from C90 to C99, with Gnu extensions too.
--
Regards,
Dave [RLU #314465]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
dwnoon@ntlworld.com (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*