Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popen on c++11 not found

I am trying to run gnuplot from a c++ program in a portable fashion. Ironically enough for WIN_32 I have no problem, but my compiler (visual studio 2015)can't identify the POSIX command popen() that I am trying to use for other OS. Is popen() nonexistent in C++11, is there an equivalent or must I change standards?

this compiles and runs in windows visual studio 2015 :

#include <stdio.h>
#include <stdlib.h>

int main()
{
  _popen(" ", "w");
  return 0;
}

this does not compile :

#include <stdio.h>
#include <stdlib.h>

int main()
{
  popen(" ", "w");
  return 0;
}

error Error C3861 'popen': identifier not found
at the end of the day, I want a behavior like

#include <stdio.h>
#include <stdlib.h>

int main()
{
  #ifdef WIN_32
    _popen(" ", "w");
  #else
    popen(" ", "w");
  #endif
  return 0;
}

I expect this program to be recompiled in g++ when used on Linux and Mac, but I would like to use msvc14 to compile on windows

like image 880
Brett Green Avatar asked Dec 02 '25 20:12

Brett Green


1 Answers

popen() is indeed not present in C++ (any version). It's defined by posix, and so available on most unix like operating systems.

There is no popen() function on windows, but there is the equivalent _popen() function that you can use

MSVC pre-defines the _WIN32 constant, which you could use for conditional compiling, .e.g

#ifdef _WIN32
   _popen(" ", "w");
#else
    popen(" ", "w");
#endif
like image 164
nos Avatar answered Dec 05 '25 00:12

nos