Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++, can you #include a file into a string literal? [duplicate]

I have a C++ source file and a Python source file. I'd like the C++ source file to be able to use the contents of the Python source file as a big string literal. I could do something like this:

char* python_code = " #include "script.py" " 

But that won't work because there need to be \'s at the end of each line. I could manually copy and paste in the contents of the Python code and surround each line with quotes and a terminating \n, but that's ugly. Even though the python source is going to effectively be compiled into my C++ app, I'd like to keep it in a separate file because it's more organized and works better with editors (emacs isn't smart enough to recognize that a C string literal is python code and switch to python mode while you're inside it).

Please don't suggest I use PyRun_File, that's what I'm trying to avoid in the first place ;)

like image 619
Joseph Garvin Avatar asked Aug 07 '09 18:08

Joseph Garvin


People also ask

How many caesareans can a woman have?

There's usually no limit to the number of caesarean sections that you can have. But the more caesareans you have, the longer each operation will take, and the higher your risk of complications becomes. If you've had a caesarean in the past, it's still possible to give birth to your baby vaginally.

What is Sigerian operation?

A C-section, also called a cesarean section or cesarean delivery, is a surgical procedure in which a baby is delivered through incisions in your abdomen and uterus. They're performed when a vaginal delivery is not possible or safe, or when the health of you or your baby is at risk.

Are C-sections painful?

You won't feel any pain during the C-section, although you may feel sensations like pulling and pressure. Most women are awake and simply numbed from the waist down using regional anesthesia (an epidural and/or a spinal block) during a C-section. That way, they are awake to see and hear their baby being born.

What's the most C-sections a woman has had?

Kristina House (USA) has given birth to 11 children (six girls and five boys) all by Caesarean section between 15 May 1979 and 20 November 1998.


2 Answers

The C/C++ preprocessor acts in units of tokens, and a string literal is a single token. As such, you can't intervene in the middle of a string literal like that.

You could preprocess script.py into something like:

"some code\n" "some more code that will be appended\n" 

and #include that, however. Or you can use xxd​ -i to generate a C static array ready for inclusion.

like image 190
bdonlan Avatar answered Sep 22 '22 21:09

bdonlan


This won't get you all the way there, but it will get you pretty damn close.

Assuming script.py contains this:

print "The current CPU time in seconds is: ", time.clock() 

First, wrap it up like this:

STRINGIFY(print "The current CPU time in seconds is: ", time.clock()) 

Then, just before you include it, do this:

#define STRINGIFY(x) #x  const char * script_py = #include "script.py" ; 

There's probably an even tighter answer than that, but I'm still searching.

like image 44
Michael Bishop Avatar answered Sep 22 '22 21:09

Michael Bishop