## C++ Examples in IPython

MUQ is written primarily in c++ with some lightweight python wrappers. Thus, we have many examples that are written solely in c++. Unfortunately, this makes it slightly more difficult to use IPython notebooks to document our examples. Instead of running the code in the notebook itself (like we do for Python examples), we construct a normal c++ file by concatenating code cells in the notebook. This is facilitated by the IPython %%writefile magic command. Once the file is created, we then compile and run it as you would with any c++ code. This is possible using the %%bash magic command. These magic commands are visible in the example source, but in the MUQ website, they are removed for clarity; only the code is displayed on the website.

### Hello world example

To illustrate a c++ example, we use the standard "Hello World" example.

First, we include the iostream header and open up the std namespace.

#include <iostream>
using namespace std;


Now we can create the main function, which will print "Hello World!" to stdout.

int main()
{
cout << "Hello World" << endl;
return 0;
}


#### Compiling the source

We compile this example using a very simple CMake file that simply creates an executable called HelloWorld in the build folder (see the MUQ source repository for details on the CMake file).

cd build; cmake ../ > BuildLog.txt; make; cd ../

Scanning dependencies of target HelloWorld
[100%] Building CXX object CMakeFiles/HelloWorld.dir/HelloWorld.cpp.o
Linking CXX executable HelloWorld
[100%] Built target HelloWorld



#### Running the executable

Not that we've compiled the executable, we can run the executable to greet the world!

build/HelloWorld

Hello World



## Completed code:

#include <iostream>
using namespace std;

int main()
{
cout << "Hello World" << endl;
return 0;
}


Test Status
• ##### Develop Branch
OS Compiler
OSX Clang
Ubuntu Clang
Ubuntu g++ 4.7
Ubuntu g++ 4.8
Ubuntu g++ 4.9
Acknowledgments
This material is based upon work supported by the National Science Foundation under Grant No. 1550487.

This work was supported by the DOE Office of Science through the QUEST SciDAC Institute.

Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.