Hello, On 07/20/2015 10:34 AM, Zhoulai wrote:
I am trying to invoke a coreutils function, say 'echo' from a C program. But because coreutils are made of 'main' function themselves, so I cannot write a program likeint main(int argc, char ** argv){ //echo.main(argc,argv); } because it is not allowed to have two 'main' functions. Is there a workaround to invoke Coreutils via another "main" program? Thanks.
A recent feature by Alex Deymo allows for something very similar to what you need: http://git.savannah.gnu.org/cgit/coreutils.git/commit/?id=71e2ea773414b2316bbe6b803b9a52c38d3752e8 Try: ./configure --enable-single-binary make clean ; make The main program is now 'src/coreutils.c', and it calls the 'main()' of each program based on the given parameters. The compilation 'trick' is based on using a preprocessing flag the renames the 'main' function to something else: For example, compiling 'ls.c' is done with: gcc [....] \ -Dmain=single_binary_main_ls (int, char **); int single_binary_main_ls" \ -Dusage=_usage_ls [...] src/ls.c Which causes the 'main' to be compiled and linked as 'single_binary_main_ls'. I'd recommend trying the above, and studying 'src/coreutils.c' as a starting point (search for 'launch_program' function). However, it's important to remember that coreutils' programs as not designed to be incorporated into bigger programs. One issue is that all the programs terminate on any errors - if there was an error in the parameters (or during runtime), the called function (e.g. your "echo.main()") will not return to your 'main' with an error code - it will terminate your program. Alternatively, since coreutils is GPL'd, it might be simpler to just reuse to code in your project instead of trying to force the programs to work the way you want (assuming your project is also GPL'd). regards, - assaf
