3

I am writing a C program that needs to open a location in the default file explorer (e.g. open C:\Program Files (x86)\somefolder in the Windows file explorer, or open /usr/share/somefolder in Nautilus/Nemo/whatever default file explorer program is set).

Is there an universal way to do that, as in a way that uses the same code for both Windows and Linux?

I did find this question that uses ShellExecute(), but this probably doesn't work on Linux. So, is it possible, and if not, is there a way to separate the code for Linux and Windows?

Yun
  • 3,056
  • 6
  • 9
  • 28
LinuxCat
  • 51
  • 4
  • 3
    I believe the best way is to use the preprocessor: see [this](https://stackoverflow.com/a/6649992/6874310). – Jardel Lucca Sep 11 '21 at 17:24
  • @JardelLucca I see, so is there no code that works on both OS? – LinuxCat Sep 11 '21 at 17:29
  • I don't it's possible. You could maybe use a cross-platform solution for this, such as converting your code to python. I'd rather go with the preprocessor way, because it seems the way it works now you have to compile the code for Linux and separately for Windows anyway. – Jardel Lucca Sep 11 '21 at 17:39
  • 1
    @JardelLucca Python would have exactly the same problem. – G. Sliepen Sep 11 '21 at 17:43
  • 1
    @G.Sliepen, thanks! I thought there was a package for this. What about [this](https://stackoverflow.com/a/12407305/6874310)? Seems to work. (Using a module named webbrowser to open the file explorer seems quite a hack though!) – Jardel Lucca Sep 11 '21 at 17:54
  • 1
    @JardelLucca Ah, I didn't know about that one. Although at least on Linux, this will open the directory in the webbrowser, not in the native file explorer (like you would get with `system("open /usr/share/somefolder")`). – G. Sliepen Sep 11 '21 at 18:03
  • @G.Sliepen Funny it actually opened on Nautilus for me. At any rate it seems it's not really cross-platform anyway... – Jardel Lucca Sep 11 '21 at 18:15
  • The utility `open` is a very handy, but only on MacOSX. The equivalent Windows utility is `start`. (In fact, `start` pre-dates `open`, 1995?) – Neil Sep 12 '21 at 17:20

1 Answers1

4

This is not possible in pure C. In fact, the C standard does not mention "Linux" or "Windows" at all. Even the words "operating system" only occur twice. The first occurrence is in 5.1.2.1-1:

In a freestanding environment (in which C program execution may take place without any benefit of an operating system) [...]

The alternative to a freestanding environment is a hosted environment (5.1.2-1). The standard continues to use the term "hosted environment", but does not distinguish between different operating systems.

A common solution is to use preprocessor macros to conditionally compile code depending on the target operating system. E.g.:

#ifdef __linux__ 
    // Open in Linux. E.g. // system("xdg-open ...");
#elif __CYGWIN__
    // Open in Windows.
#else
#error "Error: OS not supported."
#endif

This question contains a list of operating systems and corresponding macros.

Yun
  • 3,056
  • 6
  • 9
  • 28