0

i am writing some functions on c++ for compiler less to css.

i installed nodejs, less. i created a less file test.less

@color: red;
a{color:@color;}

when i run command on terminal:

lessc test.less test.css

it created a files css with name is test.css, but when i run this command via c++, it return a error. please help me. this is my c++ function:

std::string shell_exec( std::string cmd )
{
    std::string result = "";
    FILE* pipe = popen(cmd.c_str(), "r");
    if (pipe == NULL)
    {
        return result;
    }
   char buffer[128];
   while(!feof(pipe))
   {
        if(fgets(buffer, 128, pipe) != NULL)
        {
            result += buffer;
        }
    }
    pclose(pipe);
    return result;
} 

shell_exec("lessc test.less test.css");

i got a error:

/usr/bin/env: node: No such file or directory

/usr/bin/node is existed.

enter image description here enter image description here enter image description here

================ UPDATE: Fixed==================

Thank you @Bass Jobsen , @Lightness Races in Orbit

i fixed by add absolute path to lessc and nodejs

shell_exec("/usr/bin/node /usr/bin/lessc test.less test.css");
Mr Jerry
  • 1,686
  • 3
  • 14
  • 22

1 Answers1

1

From: https://unix.stackexchange.com/a/29620

The advantage of #!/usr/bin/env python is that it will use whatever python executable appears first in the user's $PATH.

So you should add node to the $PATH of the user that runs your script, see: https://stackoverflow.com/a/13210246/1596547

Notice that i can not compile your code, but i can when using the following code:

int main()
{
std::string r =  shell_exec("lessc test.less test.css");
}

Probably also use using namespace std and string instead of std:string.

Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224