Search code examples
c++macosterminallatexsystem

Can't call some commands with the 'system' function in C++ on MacOS (they work in terminal by hand)


I'm starting my adventure with LaTeX and I want a .tex file to be converted to .pdf by a C++ program. I can do it by hand by typing pdflatex foo.tex in Terminal and it gives me foo.pdf. So to the working directory of my program I copied the foo.tex file and I want to use the system("pdflatex foo.tex") function to convert it but the program gives me this error: sh: pdflatex: command not found. Using the pwd function gives me the correct directory containing the .tex file so it can't be not finding the file. Other simple commands like mkdir work. Can anyone help?


Solution

  • This is because the shell which is spawned by system() uses a different PATH environment. Although like @samcarter-is-at-topanswers-xyz says, use the full path in the system() call, otherwise you will need to modify those default PATHs to include your tool’s location. Cf. https://apple.stackexchange.com/a/33246/311469