Hello, world 的に。

メインの nssample.cpp から、 foo 名前空間の Hello クラスを使ったり、 bar 名前空間の Hello クラスを使ったりしてみる例。

nssample.cpp


#include <iostream>
#include "fooHello.h"
#include "barHello.h"
using namespace std;
 
int main() {
	cout << "hello, world" << endl; // prints !!!Hello World!!!
 
	foo::Hello fh;
	fh.SayHello();
 
	bar::Hello bh;
	bh.SayHello();
 
	return 0;
}

fooHello.h


#ifndef FOOHELLO_H_
#define FOOHELLO_H_
 
namespace foo {
 
class Hello {
public:
	Hello();
	virtual ~Hello();
	void SayHello();
};
 
} /* namespace foo */
#endif /* FOOHELLO_H_ */

fooHello.cpp


#include "fooHello.h"
#include <iostream>
 
namespace foo {
 
Hello::Hello() {
}
 
Hello::~Hello() {
}
 
void Hello::SayHello() {
	std::cout << "hello, foo" << std::endl;
}
 
} /* namespace foo */

barHello.h


#ifndef BARHELLO_H_
#define BARHELLO_H_
 
namespace bar {
 
class Hello {
public:
	Hello();
	virtual ~Hello();
	void SayHello();
};
 
} /* namespace bar */
 
#endif /* BARHELLO_H_ */

barHello.cpp


#include "barHello.h"
#include <iostream>
 
namespace bar {
 
Hello::Hello() {
}
 
Hello::~Hello() {
}
 
void Hello::SayHello() {
	std::cout << "hello, bar" << std::endl;
}
 
} /* namespace bar */

Eclipse CDT によるビルドの様子。


**** Build of configuration Debug for project nssample ****
 
make all 
Building file: ../src/barHello.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/barHello.d" -MT"src/barHello.d" -o "src/barHello.o" "../src/barHello.cpp"
Finished building: ../src/barHello.cpp
 
Building file: ../src/fooHello.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/fooHello.d" -MT"src/fooHello.d" -o "src/fooHello.o" "../src/fooHello.cpp"
Finished building: ../src/fooHello.cpp
 
Building file: ../src/nssample.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/nssample.d" -MT"src/nssample.d" -o "src/nssample.o" "../src/nssample.cpp"
Finished building: ../src/nssample.cpp
 
Building target: nssample
Invoking: MacOS X C++ Linker
g++  -o "nssample"  ./src/barHello.o ./src/fooHello.o ./src/nssample.o   
Finished building target: nssample
 
 
**** Build Finished ****

実行結果。


hello, world
hello, foo
hello, bar

ちなみに、サンプルを実行した環境。

Mac OS X Snow Leopard (MacBook Air)

Eclipse IDE for C/C++ Developers
Version: Indigo Service Release 1
Build id: 20110916-0149


$ uname -mrsv
Darwin 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
 
$ g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

tags: cpp namespace

Posted by NI-Lab. (@nilab)