3

I wanted to ask Is there any way by which you can simply use 'std::cout << "Hello world";' directly from command line.
As in like if you have python installed,

$python
print 'Hello world'
Hello world

Can something like this be done for C++ by any means?

4 Answers 4

8

There is cling which is an interactive C++ shell. I haven't used it but it may fit your needs.

Here is a bit of a more expanded response: it took me a while get cling built, primarily because I wasn't following instructions exactly and set up the source tree to include things it should have included. Here are the steps I used to build and install cling (building a release version didn't work for me):

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

After this, I got a C++ shell. Of course, my first interaction wasn't entirely successful because the cling page says that it includes some headers. I had assumed that it would surely include <iostream> but that wasn't the case. Here is a simple interaction that works, though:

$ /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
Sign up to request clarification or add additional context in comments.

Comments

0

Python is an interpreted language: http://en.wikipedia.org/wiki/Interpreted_language

C++ is a compiled language: http://en.wikipedia.org/wiki/Compiled_language

So no. Sorry

1 Comment

That's true for most implementations, but there's nothing in the language specification of either language which requires it. (Python does require a compiler or interpreter as part of the runtime, in order to implement eval.)
0

I don't think so, C++ needs to be compiled first, Python is an interpreted language.

So you can't have a script that calls

cout<<"Hello world"; 

without first compiling the code.

Comments

0

If you're using GCC and Cygwin or Linux, you could do something like the following:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.