LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-17-2007, 12:08 PM   #16
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115

The C++ FAQ opinion

I prefer to use std:: on everything since it makes it clear that the class is part of the standard library. Whenever is just see "string", I'm not 100% sure that it is really the standard version, so I have to be slower about it. In fact, I don't use "using" at all, so an unscoped type actually expresses that the type is part of the current scope.
 
Old 09-17-2007, 07:08 PM   #17
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by osor View Post
This, as you may now know, is not strictly a matter of style. I won’t dwell on the difference between “<iostream>” and “"iostream"” (since others have already pointed it out, and since it is implementation-defined). There is a greater difference, however, between the standard library headers with and without the “.h” suffix. The old-style headers include the “.h” but have no support for the standard namespace. The new-style headers have no such suffix, but require reference to namespace std, either by a using declaration or by explicit qualification.

So what I’m trying to say is that this is a conforming application:
Code:
#include <iostream.h>

int main(int argc, char *argv[]) {
     cout << "Hello World!\n";
}
While this is not:
Code:
#include <iostream>

int main(int argc, char *argv[]) {
     cout << "Hello World!\n";
}
As tempting as it may look, don’t use the first one (even though it’s standards-conforming, it’s also deprecated—meaning its support may be removed at any time).
Thanks. So it is more important to include the .h than to nitpick about < > and " " - at least to ensure the program will compile in 5 years regardless of standard committee politics.
 
Old 09-17-2007, 07:09 PM   #18
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by tuxdev View Post
The C++ FAQ opinion

I prefer to use std:: on everything since it makes it clear that the class is part of the standard library. Whenever is just see "string", I'm not 100% sure that it is really the standard version, so I have to be slower about it. In fact, I don't use "using" at all, so an unscoped type actually expresses that the type is part of the current scope.
Thanks. That made a lot of sense.
 
Old 09-17-2007, 07:14 PM   #19
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Another question about the includes. Is it possible to interchange the libraries between C and C++? I mean could a C program have #include <iostream.h> and a C++ program have #include <stdio.h>? I ask because there is a plugin for VIM (I don't know if I'll use it alot - probably not as I don't quite like how it places things and such, but it seems nice in case you have brain freeze on a certain command) that has a lot of plug and type options, one of which is the includes, but only seems to include those from C - despite having the heading of C/C++.
 
Old 09-17-2007, 10:54 PM   #20
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
C++ programs can use C libraries but not vice versa. At least not easily.
 
Old 09-18-2007, 08:55 AM   #21
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by Dan04 View Post
C++ programs can use C libraries but not vice versa. At least not easily.
When I tried the Hello World program with the stdio library, it didn't like it at all. I had to change the program to have C syntax. In that context, all this means is that the C++ compiler can compile C programs. I suppose that one can mix and match C and C++ syntax, which is usually, from what I can tell so far, fairly similar - but there are some differences (i.e. cout vs. printf). So at the very least, this seems like it could get very confusing and be in poor programming style.

Or am I missing something?
 
Old 09-18-2007, 01:19 PM   #22
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by JMJ_coder View Post
So it is more important to include the .h than to nitpick about < > and " " - at least to ensure the program will compile in 5 years regardless of standard committee politics.
I can’t tell if by include you mean “#include” the preprocessing directive or “include” the english word. In any case, it is recommended use standard header files without “.h” (because in 5 years’ time, a program using antiquated headers may no longer be valid).

The reason why “<>” vs. “""” is less important is because the search paths and methods of file identification are left to be defined by the implementation (§16.2, clauses 2 and 3). As such, you can find extensive information in the g++ documentation regarding the differences between the command-line switches “-I”, “-I-”, “-idirafter”, “-iprefix ”, “-iwithprefix”, “-iwithprefixbefore”, “-isystem”, and “-iquote”. Additionally, if a search for a file happens to fail when using “""”, the fallback is to search for the file as if it had been with “<>” (which is specified in §16.2, clause 3). By convention all files in places like “/usr/include”, “/usr/local/include”, or “/usr/lib/gcc/${ARCH}/${GCC_VERSION}/include” (i.e., system include dirs) are searched for by “<>”. With “""”, all files in the same directory as the translation unit are searched (of course the other directories are a fallback).

I hope that’s not too much language-legalese.
Quote:
Originally Posted by JMJ_coder View Post
When I tried the Hello World program with the stdio library, it didn't like it at all. I had to change the program to have C syntax. In that context, all this means is that the C++ compiler can compile C programs. I suppose that one can mix and match C and C++ syntax, which is usually, from what I can tell so far, fairly similar - but there are some differences (i.e. cout vs. printf). So at the very least, this seems like it could get very confusing and be in poor programming style.

Or am I missing something?
The difference is not really in the syntax, but in what each header provides:

When you “#include <stdio.h>” (or even “#include <cstdio>” and use namespace std), what you get is a defined type for interacting with a unique stream (i.e., FILE), a bunch of prototypes for for certain functions that may use such objects (e.g., printf(), fprintf(), fopen(), etc.), and certain external variables which are instances of pointers to that type (e.g., stdin, stdout, and stderr), with the guarantee that those functions and variables will be available during linking (to the standard library).

When you “#include <iostream>”, what you get is a bunch of defined classes (e.g., ios, istream, ostream, etc.), member function prototypes for said classes (e.g., istream:perator>>(), ostream:perator<<(), etc.), and certain external variables which are instances of said classes (e.g., cin, cout, and cerr), with the guarantee that those classes (including their member functions) and variable instances will be available during linking (to the standard library).

So the syntax is necessarily different because the overall API is different.

There is, however, something else meant by the possibility to “to interchange the libraries between C and C++”. It has to do with the differences between linking C object files to C++ object files. For example, assume I have two files: foo.c (a file which could be C or C++) and bar.cc (a C++ file). foo.c is as follows:
Code:
int foo(int a, int b)
{
	return a+b;
}
And bar.cc is as follows:
Code:
#include <iostream>

extern int foo(int,int);

using std::cin;
using std::cout;

int main(int argc, char *argv[]) {
	int num1, num2, result;
	cout << "Enter num1: ";
	cin >> num1;
	cout << "Enter num2: ";
	cin >> num2;
	result = foo(num1, num2);
	cout << "Result is: " << result << "\n";

	return 0;
}
If I compile them like this:
Code:
$ g++ -c foo.c
$ g++ -c bar.cc
$ g++ -o foobar foo.o bar.o
It all works out. However, suppose foo.c has been compiled as a C program (after all, the source is compatible with C or C++). Then, you get this:
Code:
$ gcc -c foo.c 
$ g++ -c bar.cc 
$ g++ -o foobar foo.o bar.o 
bar.o: In function `main':
bar.cc:(.text+0xbc): undefined reference to `foo(int, int)'
collect2: ld returned 1 exit status
We can remedy this situation, by changing the third line of bar.cc to this:
Code:
extern "C" int foo(int,int);
and recompile for it to work this way. So any function that from an external object file which uses the C calling convention must be specified as such. This is very common for libraries (because there is no portable way to use the C++ calling convention from C). Many libraries (which don’t make use of any C++ features) are written and compiled in C. To use any of their functions, you have to explicitly state that the function uses the C calling convention. This way, the library itself will get the maximum exposure, since both C and C++ programmers will use its functions. Luckily most such libraries provide sane header files for prototyping their functions. In this case, the preprocessor checks to see if you’re compiling C++. If so, the entire header (or at least the functions) are armored with “extern "C"”. On headers where the author did not think of this, you may do it yourself, by armoring your entire include with “extern "C"”. E.g.,
Code:
#include <iostream>
#include <SaneCLibrary.h>

extern "C" {
#include <InsaneCLibrary.h>
}
…
…
 
  


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
new [quote] style Ynot Irucrem LQ Suggestions & Feedback 9 11-09-2005 09:25 PM
How to know if I am using mbox-style or maildir-style? Niceman2005 Linux - General 1 09-23-2005 12:08 PM
VIM-style wrapping to OpenOffice style schmmd Linux - Software 1 12-21-2004 06:50 PM
how to change mandrake style menu to kde style menu msalimane Mandriva 1 12-07-2004 01:16 PM
Fluxbox Style oulevon Linux - Software 3 12-24-2002 10:25 PM

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

All times are GMT -5. The time now is 05:07 AM.

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