Skip to content

Instantly share code, notes, and snippets.

@benjaminfuchs
Last active February 21, 2019 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminfuchs/52022f13cee59acc05ab448d3bcd8c5d to your computer and use it in GitHub Desktop.
Save benjaminfuchs/52022f13cee59acc05ab448d3bcd8c5d to your computer and use it in GitHub Desktop.
import subprocess
import tempfile
import os
import re
from string import Template
STRING = """
#include <iostream>
#include <string>
#define STR(x) std::cout << #x"=" << x << std::endl
#include "$header_file"
int main()
{
$main
}
"""
names = []
with open("test.hpp", "r") as fp:
for line in fp.readlines():
match = re.match(r"^\s*#define\s+\b(\w+)\b", line)
if match:
names.append(match.group(1))
main = ""
for name in names:
main = main + "STR(" + name + "); "
t = Template(STRING)
string = t.substitute(header_file="test.hpp", main=main)
with open("main.c", "w") as fp:
fp.write(string)
p = subprocess.Popen(["g++", "-o", "main", "-xc++", "main.c"], stdout=subprocess.PIPE)
out, error = p.communicate()
p = subprocess.Popen(["./main"], stdout=subprocess.PIPE)
out, error = p.communicate()
print(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment