Calling SED for a source in Makefile.am

可紊 提交于 2019-12-10 10:03:38

问题


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?


回答1:


Don't use in-place sed.

Instead:

gentest_SOURCES = gentest-seded.cc

gentest-seded.cc : gentest.cc
    $(SED) "s|__PREFIX__|@prefix@|" $< >$@



回答2:


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"'



回答3:


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.



来源:https://stackoverflow.com/questions/1327998/calling-sed-for-a-source-in-makefile-am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!