2013-12-16 50 views
3

我想問你有什麼方法可以簡單地使用'std :: cout < <「Hello world」;'直接從命令行。
正如一樣,如果你已經安裝了Python,直接從命令行使用C++命令

$python 
print 'Hello world' 
Hello world 

能像這樣被用於C++以任何方式呢?

回答

8

cling這是一個交互式C++外殼。我沒有使用它,但它可能適合您的需求。

下面是一段更爲擴展的迴應:它花了我一段時間才建立起來,主要是因爲我沒有按照的指令完成,並設置了源代碼樹來包含它應該包含的內容。下面是我用來建造和安裝保鮮(建立一個發佈版本並沒有爲我工作)的步驟:

svn co -r 191429 http://llvm.org/svn/llvm-project/llvm/trunk llvm 
cd llvm/tools 
svn co -r 191429 http://llvm.org/svn/llvm-project/cfe/trunk clang 
git clone http://root.cern.ch/git/cling.git 
cd .. 
cat tools/cling/patches/*.diff | patch -p0 
./configure --enable-targets=host --prefix=/opt/cling 
mk -j8 
sudo - make install 

在此之後,我得到了一個C++殼。當然,我的第一次互動並不完全成功,因爲粘貼頁面說它包含一些標題。我曾假設它肯定包含<iostream>,但事實並非如此。下面是一個簡單的互動的作品,雖然:需要

$ /opt/cling/bin/cling 

****************** CLING ****************** 
* Type C++ code and press enter to run it * 
*    Type .q to exit    * 
******************************************* 
[cling]$ #include <iostream> 
[cling]$ std::cout << "hello, world\n"; 
hello, world 
[cling]$ #include <iterator> 
[cling]$ std::copy(s.begin(), s.end(), std::ostream_iterator<char>(std::cout)); 
hello, world 
[cling]$ .q 
0

我不這麼認爲,C++首先被編譯,Python是一種解釋型語言。

所以你不能有一個調用

cout<<"Hello world"; 

無需首先編譯代碼的腳本。

0

如果你使用GCC和Cygwin或Linux,你可以這樣做以下:

echo -e '#include <iostream>\n int main(void) {std::cout << "Hello world";}'|g++ -xc++ - && ./a.out && rm a.out