It's more common to see this hello world source code:
$ vim hello.cpp
#include
int
main(){
std::cout<<"hello world!\n";
return 0;
}
Compile it with g++ and execute it:
$ g++ -o hello hello.cpp
$ ./hello
It's the basic. How about if we manage to build a complex program? We divide it. We made several files. Here's the hello world with multiple files version. In this example there are three files:
- helloworld.cpp
- helloworld.h
- main.cpp
First, the helloworld.cpp
#include "helloworld.h"
#include
using namespace std;
void hello_world(){
cout << "hello world!\n";
}
Then the helloworld.h
#ifndef _HELLOWORLD_H
#define _HELLOWORLD_H
#include
void hello_world();
#endif
Last, the main.cpp
#include "helloworld.h"
#include
using namespace std;
int main(){
hello_world();
return 0;
}
Then it's time to compile it. How to do it?
g++ -o hello main.cpp helloworld.cpp
Execute it!
$ ./hello
hello world!
Reference, accessed on 2010.10.01 14:20 (GMT+7).
Tidak ada komentar:
Posting Komentar