How's GNU extended function running when didn't defined _GNU_SOURCE










1















I test GNU extended function such as "wcstoq" at different platforms.
At first,I didn't compile the testcase with D_GNU_SOURCE,so I got a compile warning as follows:



wcstoq.c:31:12: warning: implicit declaration of function 'wcstoq'; did you mean 'wcstol'? [-Wimplicit-function-declaration]
retval=wcstoq(nptr,endptr,base);
^~~~~~
wcstol


when I use gdb to debug this testcase,it goes into the correct function which is same as the case I defined _GNU_SOURCE.
But the function goes wrong when I did't define _GNU_SOURCE.



for example:



When I test the out of range case in x86_64, it should set retval= LLONG_MAX(0x7FFFFFFFFFFFFFFF),but it actually set retval= -1(0xFFFFFFFFFFFFFFFF).
The result also confused me when I test it in other platforms ppc, it actually set retval=0x000000007FFFFFFF.
when I defined _GNU_SOURCE, function runs ok in x86_64 and ppc, it both returns LLONG_MAX(0x7FFFFFFFFFFFFFFF).



my question is:



1 Why when I don't define _GNU_SOURCE,but gcc still can find the right function?



2 Why the fuction failed even the gcc find the right one,and in some cases the function can still be used,but in other cases it goes wrong?



3 Why the failed result of fuction is different in different platforms, how architecture platforms or other factors impact the different results?



4 The right way to use GNU extend function?



Thks!










share|improve this question



















  • 1





    There's no prototype definition in the case you've shown, so the compile does not know the sizes (or numbers) of parameters. The external symbol exists, since it is not a macro or inline.

    – Thomas Dickey
    Nov 13 '18 at 9:31











  • I think the best answer to 4) is that you should define _GNU_SOURCE to get the library headers to include prototypes for GNU extensions. Without declarations at all, the compiler assumes the return type is int.

    – Peter Cordes
    Nov 13 '18 at 15:23











  • Thanks,@PeterCordes,Do you know any more information about the behavior of function with implicit declaration, not only the return type but also some input or output parameters, is it mean the behavior of this function is unpredictable or undefined?Especially for GNU extended functions?

    – qiqi
    Nov 14 '18 at 5:04











  • Thanks,@ThomasDickey but what I am confused and curious about why the function could also be called even if not defined _GNU_SOURCE,and why the behavior or output of the function is not the same in different platforms

    – qiqi
    Nov 14 '18 at 5:05
















1















I test GNU extended function such as "wcstoq" at different platforms.
At first,I didn't compile the testcase with D_GNU_SOURCE,so I got a compile warning as follows:



wcstoq.c:31:12: warning: implicit declaration of function 'wcstoq'; did you mean 'wcstol'? [-Wimplicit-function-declaration]
retval=wcstoq(nptr,endptr,base);
^~~~~~
wcstol


when I use gdb to debug this testcase,it goes into the correct function which is same as the case I defined _GNU_SOURCE.
But the function goes wrong when I did't define _GNU_SOURCE.



for example:



When I test the out of range case in x86_64, it should set retval= LLONG_MAX(0x7FFFFFFFFFFFFFFF),but it actually set retval= -1(0xFFFFFFFFFFFFFFFF).
The result also confused me when I test it in other platforms ppc, it actually set retval=0x000000007FFFFFFF.
when I defined _GNU_SOURCE, function runs ok in x86_64 and ppc, it both returns LLONG_MAX(0x7FFFFFFFFFFFFFFF).



my question is:



1 Why when I don't define _GNU_SOURCE,but gcc still can find the right function?



2 Why the fuction failed even the gcc find the right one,and in some cases the function can still be used,but in other cases it goes wrong?



3 Why the failed result of fuction is different in different platforms, how architecture platforms or other factors impact the different results?



4 The right way to use GNU extend function?



Thks!










share|improve this question



















  • 1





    There's no prototype definition in the case you've shown, so the compile does not know the sizes (or numbers) of parameters. The external symbol exists, since it is not a macro or inline.

    – Thomas Dickey
    Nov 13 '18 at 9:31











  • I think the best answer to 4) is that you should define _GNU_SOURCE to get the library headers to include prototypes for GNU extensions. Without declarations at all, the compiler assumes the return type is int.

    – Peter Cordes
    Nov 13 '18 at 15:23











  • Thanks,@PeterCordes,Do you know any more information about the behavior of function with implicit declaration, not only the return type but also some input or output parameters, is it mean the behavior of this function is unpredictable or undefined?Especially for GNU extended functions?

    – qiqi
    Nov 14 '18 at 5:04











  • Thanks,@ThomasDickey but what I am confused and curious about why the function could also be called even if not defined _GNU_SOURCE,and why the behavior or output of the function is not the same in different platforms

    – qiqi
    Nov 14 '18 at 5:05














1












1








1








I test GNU extended function such as "wcstoq" at different platforms.
At first,I didn't compile the testcase with D_GNU_SOURCE,so I got a compile warning as follows:



wcstoq.c:31:12: warning: implicit declaration of function 'wcstoq'; did you mean 'wcstol'? [-Wimplicit-function-declaration]
retval=wcstoq(nptr,endptr,base);
^~~~~~
wcstol


when I use gdb to debug this testcase,it goes into the correct function which is same as the case I defined _GNU_SOURCE.
But the function goes wrong when I did't define _GNU_SOURCE.



for example:



When I test the out of range case in x86_64, it should set retval= LLONG_MAX(0x7FFFFFFFFFFFFFFF),but it actually set retval= -1(0xFFFFFFFFFFFFFFFF).
The result also confused me when I test it in other platforms ppc, it actually set retval=0x000000007FFFFFFF.
when I defined _GNU_SOURCE, function runs ok in x86_64 and ppc, it both returns LLONG_MAX(0x7FFFFFFFFFFFFFFF).



my question is:



1 Why when I don't define _GNU_SOURCE,but gcc still can find the right function?



2 Why the fuction failed even the gcc find the right one,and in some cases the function can still be used,but in other cases it goes wrong?



3 Why the failed result of fuction is different in different platforms, how architecture platforms or other factors impact the different results?



4 The right way to use GNU extend function?



Thks!










share|improve this question
















I test GNU extended function such as "wcstoq" at different platforms.
At first,I didn't compile the testcase with D_GNU_SOURCE,so I got a compile warning as follows:



wcstoq.c:31:12: warning: implicit declaration of function 'wcstoq'; did you mean 'wcstol'? [-Wimplicit-function-declaration]
retval=wcstoq(nptr,endptr,base);
^~~~~~
wcstol


when I use gdb to debug this testcase,it goes into the correct function which is same as the case I defined _GNU_SOURCE.
But the function goes wrong when I did't define _GNU_SOURCE.



for example:



When I test the out of range case in x86_64, it should set retval= LLONG_MAX(0x7FFFFFFFFFFFFFFF),but it actually set retval= -1(0xFFFFFFFFFFFFFFFF).
The result also confused me when I test it in other platforms ppc, it actually set retval=0x000000007FFFFFFF.
when I defined _GNU_SOURCE, function runs ok in x86_64 and ppc, it both returns LLONG_MAX(0x7FFFFFFFFFFFFFFF).



my question is:



1 Why when I don't define _GNU_SOURCE,but gcc still can find the right function?



2 Why the fuction failed even the gcc find the right one,and in some cases the function can still be used,but in other cases it goes wrong?



3 Why the failed result of fuction is different in different platforms, how architecture platforms or other factors impact the different results?



4 The right way to use GNU extend function?



Thks!







gcc compilation gnu cpu-architecture glibc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 14:43







qiqi

















asked Nov 13 '18 at 9:13









qiqiqiqi

83




83







  • 1





    There's no prototype definition in the case you've shown, so the compile does not know the sizes (or numbers) of parameters. The external symbol exists, since it is not a macro or inline.

    – Thomas Dickey
    Nov 13 '18 at 9:31











  • I think the best answer to 4) is that you should define _GNU_SOURCE to get the library headers to include prototypes for GNU extensions. Without declarations at all, the compiler assumes the return type is int.

    – Peter Cordes
    Nov 13 '18 at 15:23











  • Thanks,@PeterCordes,Do you know any more information about the behavior of function with implicit declaration, not only the return type but also some input or output parameters, is it mean the behavior of this function is unpredictable or undefined?Especially for GNU extended functions?

    – qiqi
    Nov 14 '18 at 5:04











  • Thanks,@ThomasDickey but what I am confused and curious about why the function could also be called even if not defined _GNU_SOURCE,and why the behavior or output of the function is not the same in different platforms

    – qiqi
    Nov 14 '18 at 5:05













  • 1





    There's no prototype definition in the case you've shown, so the compile does not know the sizes (or numbers) of parameters. The external symbol exists, since it is not a macro or inline.

    – Thomas Dickey
    Nov 13 '18 at 9:31











  • I think the best answer to 4) is that you should define _GNU_SOURCE to get the library headers to include prototypes for GNU extensions. Without declarations at all, the compiler assumes the return type is int.

    – Peter Cordes
    Nov 13 '18 at 15:23











  • Thanks,@PeterCordes,Do you know any more information about the behavior of function with implicit declaration, not only the return type but also some input or output parameters, is it mean the behavior of this function is unpredictable or undefined?Especially for GNU extended functions?

    – qiqi
    Nov 14 '18 at 5:04











  • Thanks,@ThomasDickey but what I am confused and curious about why the function could also be called even if not defined _GNU_SOURCE,and why the behavior or output of the function is not the same in different platforms

    – qiqi
    Nov 14 '18 at 5:05








1




1





There's no prototype definition in the case you've shown, so the compile does not know the sizes (or numbers) of parameters. The external symbol exists, since it is not a macro or inline.

– Thomas Dickey
Nov 13 '18 at 9:31





There's no prototype definition in the case you've shown, so the compile does not know the sizes (or numbers) of parameters. The external symbol exists, since it is not a macro or inline.

– Thomas Dickey
Nov 13 '18 at 9:31













I think the best answer to 4) is that you should define _GNU_SOURCE to get the library headers to include prototypes for GNU extensions. Without declarations at all, the compiler assumes the return type is int.

– Peter Cordes
Nov 13 '18 at 15:23





I think the best answer to 4) is that you should define _GNU_SOURCE to get the library headers to include prototypes for GNU extensions. Without declarations at all, the compiler assumes the return type is int.

– Peter Cordes
Nov 13 '18 at 15:23













Thanks,@PeterCordes,Do you know any more information about the behavior of function with implicit declaration, not only the return type but also some input or output parameters, is it mean the behavior of this function is unpredictable or undefined?Especially for GNU extended functions?

– qiqi
Nov 14 '18 at 5:04





Thanks,@PeterCordes,Do you know any more information about the behavior of function with implicit declaration, not only the return type but also some input or output parameters, is it mean the behavior of this function is unpredictable or undefined?Especially for GNU extended functions?

– qiqi
Nov 14 '18 at 5:04













Thanks,@ThomasDickey but what I am confused and curious about why the function could also be called even if not defined _GNU_SOURCE,and why the behavior or output of the function is not the same in different platforms

– qiqi
Nov 14 '18 at 5:05






Thanks,@ThomasDickey but what I am confused and curious about why the function could also be called even if not defined _GNU_SOURCE,and why the behavior or output of the function is not the same in different platforms

– qiqi
Nov 14 '18 at 5:05













1 Answer
1






active

oldest

votes


















1














Implicit function declarations, a GCC extension which is enabled by default, use a return type of int, however the function is defined as returning long long. With such a type mismatch, anything can happen. In practice, the results depend on the calling convention, and that is why they vary across architectures.



You should really compile with -Werror=implicit-function-declaration (or use C++). Implicit function declarations were removed from C close to twenty years ago (in ISO/IEC 9899:1999), but we still cannot change the GCC default because too many autoconf checks would break, resulting in software accidentally losing features.



(I think you meant to write retval in your question instead of errno.)






share|improve this answer

























  • Maybe worth mentioning explicitly that implicit declarations are allowed in ISO C89. But yes, in later ISO C standards (and C++) it's not allowed, and agreed that it's generally a bad idea. There's a reason GCC warns about it even without -Wall.

    – Peter Cordes
    Nov 22 '18 at 23:04











  • Yes,I mean retval instead of errno and I reedit it.

    – qiqi
    Nov 27 '18 at 1:08










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277504%2fhows-gnu-extended-function-running-when-didnt-defined-gnu-source%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Implicit function declarations, a GCC extension which is enabled by default, use a return type of int, however the function is defined as returning long long. With such a type mismatch, anything can happen. In practice, the results depend on the calling convention, and that is why they vary across architectures.



You should really compile with -Werror=implicit-function-declaration (or use C++). Implicit function declarations were removed from C close to twenty years ago (in ISO/IEC 9899:1999), but we still cannot change the GCC default because too many autoconf checks would break, resulting in software accidentally losing features.



(I think you meant to write retval in your question instead of errno.)






share|improve this answer

























  • Maybe worth mentioning explicitly that implicit declarations are allowed in ISO C89. But yes, in later ISO C standards (and C++) it's not allowed, and agreed that it's generally a bad idea. There's a reason GCC warns about it even without -Wall.

    – Peter Cordes
    Nov 22 '18 at 23:04











  • Yes,I mean retval instead of errno and I reedit it.

    – qiqi
    Nov 27 '18 at 1:08















1














Implicit function declarations, a GCC extension which is enabled by default, use a return type of int, however the function is defined as returning long long. With such a type mismatch, anything can happen. In practice, the results depend on the calling convention, and that is why they vary across architectures.



You should really compile with -Werror=implicit-function-declaration (or use C++). Implicit function declarations were removed from C close to twenty years ago (in ISO/IEC 9899:1999), but we still cannot change the GCC default because too many autoconf checks would break, resulting in software accidentally losing features.



(I think you meant to write retval in your question instead of errno.)






share|improve this answer

























  • Maybe worth mentioning explicitly that implicit declarations are allowed in ISO C89. But yes, in later ISO C standards (and C++) it's not allowed, and agreed that it's generally a bad idea. There's a reason GCC warns about it even without -Wall.

    – Peter Cordes
    Nov 22 '18 at 23:04











  • Yes,I mean retval instead of errno and I reedit it.

    – qiqi
    Nov 27 '18 at 1:08













1












1








1







Implicit function declarations, a GCC extension which is enabled by default, use a return type of int, however the function is defined as returning long long. With such a type mismatch, anything can happen. In practice, the results depend on the calling convention, and that is why they vary across architectures.



You should really compile with -Werror=implicit-function-declaration (or use C++). Implicit function declarations were removed from C close to twenty years ago (in ISO/IEC 9899:1999), but we still cannot change the GCC default because too many autoconf checks would break, resulting in software accidentally losing features.



(I think you meant to write retval in your question instead of errno.)






share|improve this answer















Implicit function declarations, a GCC extension which is enabled by default, use a return type of int, however the function is defined as returning long long. With such a type mismatch, anything can happen. In practice, the results depend on the calling convention, and that is why they vary across architectures.



You should really compile with -Werror=implicit-function-declaration (or use C++). Implicit function declarations were removed from C close to twenty years ago (in ISO/IEC 9899:1999), but we still cannot change the GCC default because too many autoconf checks would break, resulting in software accidentally losing features.



(I think you meant to write retval in your question instead of errno.)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 9:10

























answered Nov 22 '18 at 21:32









Florian WeimerFlorian Weimer

15.4k3944




15.4k3944












  • Maybe worth mentioning explicitly that implicit declarations are allowed in ISO C89. But yes, in later ISO C standards (and C++) it's not allowed, and agreed that it's generally a bad idea. There's a reason GCC warns about it even without -Wall.

    – Peter Cordes
    Nov 22 '18 at 23:04











  • Yes,I mean retval instead of errno and I reedit it.

    – qiqi
    Nov 27 '18 at 1:08

















  • Maybe worth mentioning explicitly that implicit declarations are allowed in ISO C89. But yes, in later ISO C standards (and C++) it's not allowed, and agreed that it's generally a bad idea. There's a reason GCC warns about it even without -Wall.

    – Peter Cordes
    Nov 22 '18 at 23:04











  • Yes,I mean retval instead of errno and I reedit it.

    – qiqi
    Nov 27 '18 at 1:08
















Maybe worth mentioning explicitly that implicit declarations are allowed in ISO C89. But yes, in later ISO C standards (and C++) it's not allowed, and agreed that it's generally a bad idea. There's a reason GCC warns about it even without -Wall.

– Peter Cordes
Nov 22 '18 at 23:04





Maybe worth mentioning explicitly that implicit declarations are allowed in ISO C89. But yes, in later ISO C standards (and C++) it's not allowed, and agreed that it's generally a bad idea. There's a reason GCC warns about it even without -Wall.

– Peter Cordes
Nov 22 '18 at 23:04













Yes,I mean retval instead of errno and I reedit it.

– qiqi
Nov 27 '18 at 1:08





Yes,I mean retval instead of errno and I reedit it.

– qiqi
Nov 27 '18 at 1:08

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277504%2fhows-gnu-extended-function-running-when-didnt-defined-gnu-source%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto