LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-08-2011, 07:59 PM   #1
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Rep: Reputation: Disabled
Question Does gcc with different version include some different default header files?


Hi,

I have a question about header file.

test.cpp:

Code:
#include <string>

int main()
{
    char c[8] = {0};
    strcpy(c, "123");
    int n = atoi(c);
    return 0;
}
the code above use strcpy and atoi, they are in string.h and stdlib.h, but I didn't include these, and just include <string>.
It can be compiled correctly by gcc4.1.1(fedora6) and MSVC6(windows2003), but wrong with gcc4.6.0(fedora15).

Is there a way to make gcc4.6.0 compiling the code correctly?

thanks.
 
Old 07-08-2011, 08:23 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
You should refer to the man-page for specific information as to which header files should be included.

For strcpy(), you should include cstring (not string.h, since this is applicable to C, not C++).

For atoi(), you should include cstdlib (not stdlib.h)


P.S. In C++, there's little use for strcpy(). Use the string class instead when defining a string. As for atoi(), it too has issues. If the input is invalid (ie. non-numeric), atoi() returns zero. This could be interpreted by the program as a legit number. Consider using stringstream instead; it provides the ability to query whether the conversion from a string to a number was successful or not.
 
1 members found this post helpful.
Old 07-08-2011, 09:44 PM   #3
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
You should refer to the man-page for specific information as to which header files should be included.

For strcpy(), you should include cstring (not string.h, since this is applicable to C, not C++).

For atoi(), you should include cstdlib (not stdlib.h)


P.S. In C++, there's little use for strcpy(). Use the string class instead when defining a string. As for atoi(), it too has issues. If the input is invalid (ie. non-numeric), atoi() returns zero. This could be interpreted by the program as a legit number. Consider using stringstream instead; it provides the ability to query whether the conversion from a string to a number was successful or not.

Thanks, you let me know more knowledge about c and c++.

the test.cpp is just an example to point out that on some verion of gcc it's correct and some are wrong.

I have some project compiled correctly on fecora6, but cann't be compiled on fedora15, the main resean is that there doesn't specify correctly header file as in test.cpp.
 
Old 07-09-2011, 12:24 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
As dwhitney67 pointed out, you should always check the manpages. I've had C code that compiled for years across different distributions and then suddenly stopped compiling because a secondary include was different in a different libc version. I'd forgotten to include the correct header, but since another header had included the one I needed I never noticed. I've also downloaded open-source projects that wouldn't compile because of missing includes, most likely because of this very issue. A bad habit of mine is to guess the headers for functions I use a lot and if it compiles I leave it.

On a separate note, you've inadvertently written a C program, except for the C++ header. You might therefore consider playing around with C rather than making what you've written proper C++. I became very proficient with C++ before I learned C, but I certainly prefer C if I don't need complicated data structures. I've also used C++ compilers where some of the standard C functions were in namespace std, which is a mess! I think the committee for the C++ standard just included things like <cstring> because they didn't want those "archaic" .h includes, but if you're using C functions you either can't do it in C++ (e.g. fcntl) or you're just not using the C++ symbol meant to replace it. In either case, you might as well just include the .h and make it proper C!

As far as what you've written, strncpy should be used instead of strcpy because it can prevent a buffer overrun if your source string is longer than you anticipated. Lastly, strtol replaces atoi and is a more recent function.
Kevin Barry

PS The differences between C and C within C++ aren't as arbitrary as they seem on the surface. You can run into some serious linking issues with C++, even if the code you've written is 100% C.

Last edited by ta0kira; 07-09-2011 at 12:42 AM.
 
1 members found this post helpful.
Old 07-09-2011, 08:05 PM   #5
cplus
LQ Newbie
 
Registered: Jul 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks to dwhitney67 and ta0kira.

maybe due to my ugly engish, you have some mistake of my purpos.

at last, I find <cstdlib> and <cstring> are included by <string> indrectly in MSVC7. this must be why the test.cpp can be compiled correctly. and in new version of gcc, they are not included by <string> indrectly.

thank you very much.
 
Old 07-10-2011, 05:09 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
@cplus:

C++ and C are different languages. Any claim of C++ being a superset of C is incorrect. C++ originated as an offshoot of C, but has since diverged, and is a completely separate, different language nowadays. Both are actively developed, albeit by different people, and standardized in totally different documents.

When mixing C and C++ code, it is very important to do it correctly (using the well-documented patterns and guidelines), or you will end up with fragile or buggy code. Just because it compiles, does not mean it will work as intended.

You can compile most, but not all, C code with a C++ compiler, but the end result will be different to what you get if you used a C compiler. For example, compare the dynamic libraries the final binaries depend on using the ldd command. What is more important, is that the two binaries will have different runtime behaviour, especially with respect to memory management and exception and signal handling.

Most compilers today are actually compiler suites, with a number of "frontends" for different languages -- typically C, C++, and possibly one or more varieties of Fortran. Therefore selecting a different compiler is usually just a matter of different compiling options. I guess that can be confusing.

C and C++ compilers do both have small libraries built-in. These include for example strcpy(), strncpy(), memset(), memcpy() and memmove() functions; I haven't looked for an exhaustive list -- but it quite likely varies from version to version. These are all defined in the standard, and therefore do not really need a separate header file; think of these as having a header file built-in to the compiler.

While GCC does prefer its own implementation, you can tell it to not do that by using the --fno-builtin-function option, where function is the name of a function. (If GCC does not have its own version of the function, the option is ignored.) You can access the GCC implementation of these functions using __builtin_function().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Possibly missing /usr/include/linux header files.. how to fix? brfindla Debian 2 08-10-2009 11:21 AM
C Header files missing in /usr/include zorro_kwh Ubuntu 3 04-12-2008 06:57 PM
Include kernel header files fallen3019 Programming 10 05-12-2007 08:35 PM
Kernel header (include) files - where do i find these walkerx Mandriva 2 04-17-2005 08:19 AM
Problem with C++ include/header files! Pisces107 Programming 12 12-23-2003 11:06 PM


All times are GMT -5. The time now is 11:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration