LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-25-2012, 07:53 AM   #1
carbonbasedlifeform
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Rep: Reputation: Disabled
Question Why do I not always have to explicitly include a system library for it to compile?


Newb question. I am having to switch between two computers (Centos 5.8 and Centos 6.2) and I have noticed that on the CentOS 5 system I do not need to explicitly include the string library while on the CentOS 6 system I do, such as the following:

Code:
#include <iostream>

int main() {

        std::cout << strlen("test") << std::endl;
        return 0;
}

This will compile fine on the CentOS 5 system but on the CentOS 6 system i receive the following:

Code:
stringtest.cpp: In function ‘int main()’:
stringtest.cpp:5: error: ‘strlen’ was not declared in this scope
Could this be a compiler or gcc version difference? Thanks for any tips!
 
Old 10-25-2012, 08:26 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by carbonbasedlifeform View Post
Newb question. I am having to switch between two computers (Centos 5.8 and Centos 6.2) and I have noticed that on the CentOS 5 system I do not need to explicitly include the string library while on the CentOS 6 system I do, such as the following:

Code:
#include <iostream>

int main() {

        std::cout << strlen("test") << std::endl;
        return 0;
}

This will compile fine on the CentOS 5 system but on the CentOS 6 system i receive the following:

Code:
stringtest.cpp: In function ‘int main()’:
stringtest.cpp:5: error: ‘strlen’ was not declared in this scope
Could this be a compiler or gcc version difference? Thanks for any tips!
It is probably due to the implementation of iostream. In older versions, it probably included <cstring> or some other header which did, whereas in current versions it does not.

We had a similar problem in my office recently; someone made code changes and only tested the compilation on RHEL5. But on RHEL6, it would not compile.

My suggestion is to write better code.
 
Old 10-25-2012, 08:43 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Just for the sake of accuracy, C/C++ header files are not libraries. They are source code. Libraries are bound to your application at link time, by a linker (and again at runtime, in the case of shared object libraries). Header files are commonly associated with libraries, as they can be used to describe to the compiler how the library content can be accessed. The libraries are already separately compiled, so the compiler can only know about their content by including source code statements that describe the content.
--- rod.
 
1 members found this post helpful.
Old 10-25-2012, 10:57 AM   #4
carbonbasedlifeform
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
I found the problem. It is an issue porting from gcc 4.1 to 4.3 and higher.

http://gcc.gnu.org/gcc-4.3/porting_to.html

"As detailed here (Header dependency streamlining), many of the standard C++ library include files have been edited to only include the smallest possible number of additional files. As such, many C++ programs that used std::memcpy without including <cstring>, or used std::auto_ptr without including <memory> will no longer compile.

Usually, this error is of the form:

error: 'strcmp' was not declared in this scope"
 
Old 10-25-2012, 01:51 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Note: In real life problems like this arise all the time. There is no point in muling over them, you simply solve them as they come.
 
Old 10-25-2012, 02:29 PM   #6
carbonbasedlifeform
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
that was insightful

Quote:
Originally Posted by NevemTeve View Post
Note: In real life problems like this arise all the time. There is no point in muling over them, you simply solve them as they come.
Note: In real life people also discuss issues as a way to find resolutions.
 
  


Reply



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
[SOLVED] How to compile c++ program without changing #include line & header files not system Dstruct0 Programming 3 04-03-2011 08:20 PM
include a library Geminias Linux - Newbie 1 03-16-2006 02:57 AM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM
How to include library directories? davee Programming 7 04-05-2004 05:41 AM
include a library in program calble Programming 1 10-09-2003 12:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration