Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Usage
--------
Load extension using %load_ext cppmagic.
Use %%cpp at the beginning of the cell and write your C++ code. Execute to get results or compilation errors.
Use %%cpp -t to get a timeit result.
Use %%cpp -t to get a timeit result.
Use %%cpp -- [-args...] to pass arguments to the C++ compiler. (For example, %%cpp -- -O3 -lpthread)

<br><br>

Expand Down Expand Up @@ -91,7 +92,6 @@ TODO:
--------
- test on windows
- test on python 2
- add compiler flags
- add timeit params


8 changes: 5 additions & 3 deletions cppmagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def get_argparser():
parser = argparse.ArgumentParser(description='CppMagic params')
parser.add_argument("-t", "--timeit", action='store_true',
help='flag to return timeit result instead of stdout')
parser.add_argument("rest", nargs = argparse.REMAINDER)
return parser


Expand All @@ -23,8 +24,9 @@ def __init__(self, shell):
super(CppMagic, self).__init__(shell)
self.argparser = get_argparser()

def _compile(self, file_path):
subprocess.check_output(["g++", file_path + ".cpp", "-o", file_path + ".out"], stderr=subprocess.STDOUT)
def _compile(self, file_path, compilerOpts=None):
cmd = ["g++", file_path + ".cpp", "-o", file_path + ".out"] + compilerOpts[1:len(compilerOpts)]
subprocess.check_output(cmd, stderr=subprocess.STDOUT)

def _run(self, file_path, timeit=False):
if timeit:
Expand All @@ -48,7 +50,7 @@ def cpp(self, line, cell):
with open(file_path + ".cpp", "w") as f:
f.write(cell)
try:
self._compile(file_path)
self._compile(file_path, compilerOpts = args.rest)
output = self._run(file_path, timeit=args.timeit)
except subprocess.CalledProcessError as e:
print(e.output.decode("utf8"))
Expand Down