views:

422

answers:

3

I've got c++ code that needs a sed done to it prior to compilation. How do I place this into Makefile.am?

I tried the typical makefile setup and the target appears to not exist:

gentest.cc:

$(SED) -i "s|FIND|REPLACE|" gentest.cc

If you are interested as to why I want to do this, it's because I wrote my program (slider3.py) in python and my partner wrote his in c++ (gentest.cc) and his needs to call mine. I'm accomplishing this by editing the argv and then using execv().

... {

char **argv2 = new char*[argc];

memset(argv2,0,sizeof(argv2));

argv2[0] = "__PREFIX__/bin/slider3.py";

memcpy(argv2 + 1, argv + 2, sizeof(char *) * (argc - 2));

int oranges = execv(argv2[0], argv2);

printf("%s\n", strerror(oranges));

return oranges;

} ...

I've already handled getting the #! added to slider3.py and chmod +x by using the method that was not working for gentest.cc. I've also handled adding slider3.py to the list of files that get installed.

EXTRA_DIST=testite.sh slider3_base.py

bin_SCRIPTS = slider3.py

CLEANFILES = $(bin_SCRIPTS)

slider3.py: slider3_base.py

rm -f slider3.py

echo "#! " $(PYTHON) > slider3.py

cat slider3_base.py >> slider3.py

chmod +x slider3.py

gentest is defined this way in Makefile.am:

bin_PROGRAMS = gentest

gentest_SOURCES = gentest.cc

gentest_LDADD = libgen.a #../libsbsat.la $(LIBM)

And this fails to be run during make (note the @ pattern is successfully expanded in Makefile):

gentest.cc:

$(SED) -i "s|__PREFIX__|@prefix@|" gentest.cc

Any ideas on how to get sed to run before compiling gentest.cc?

+4  A: 

Don't use in-place sed.

Instead:

gentest_SOURCES = gentest-seded.cc

gentest-seded.cc : gentest.cc
    $(SED) "s|__PREFIX__|@prefix@|" $< >$@
Douglas Leeder
+5  A: 

Have you ever considered #define-ing it in config.h (you're using autotools, right?) or passing it using -D when compiling? This is really not the case for sed.

The details from Andrew Y's answer:

in your C++ source, specify:

argv2[0] = SCRIPTPREFIX "/bin/slider3.py";

then compile with

-DSCRIPTPREFIX='"/your/script/prefix"'
Michael Krelin - hacker
Yes, I'm using autotools -- for the first time. Can you show me examples of your suggestions?
Nerdling
Ah, is Andrew Y's suggestion the -D you spoke of?
Nerdling
If you have config.h (AC_CONFIG_HEADERS), you can use something like AC_DEFINE_UNQUOTED([THE_PREFIX],["$prefix"]) in your configure.ac. You really have to play with it, with the documentation and other source to get the feeling.It may be easier to add the CPPFLAGS to your Makefile.am and pass it using -DTHE_PREFIX=\"@prefix@\" (or something like that — watch out for proper escaping).In both cases you would then do the THE_PREFX "/bin/slider3.py" in your code. Which makes me think you may prefer @bindir@ over @prefix@.
Michael Krelin - hacker
Yes, I mentioned both and the second one coincides with Andrew Y's. (I was the first:))
Michael Krelin - hacker
Ah, didn't even think of using @bindir@, but that'd be much more correct.
Nerdling
@hacker: +1 - yours was 39 seconds earlier, indeed :) care to move more details to your answer, so I could delete mine ?
Andrew Y
Andrew Y, sure. But when I said I was the first, I didn't mean to establish my priority, but just meant that I haven't seen yours ;-)
Michael Krelin - hacker
:-) in any case it's most about helping the person who asked. Thanks for updating!
Andrew Y
It is, that's why I explicitly mentioned I didn't intend it to be a typists competition ;-) And you're welcome ;-)
Michael Krelin - hacker
A: 

Have you considered calling the Python code directly from the C++? Here is a tutorial on using boost to call python functions from C++. The method you are describing here seems very brittle.

Shane C. Mason
My partner also decided to use CVS, so you can imagine this will be a long fight until we're done :-) The problem with switching to Boost is that I'd rather not rewrite his code. I thought using a replace via the makefiles was suitable to avoid that.
Nerdling
I'll definitely keep boost in mind for working with C++ in the future!
Nerdling

related questions