Author Topic: OS auto detection - project feature  (Read 1440 times)

0 Members and 1 Guest are viewing this topic.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
OS auto detection - project feature
« on: August 15, 2013, 12:27:42 pm »
I am working on a project now in C++ which i want to be portable for windows, Linux and Macos.
Problem is the auto detection of the os so as to locate certain file which are stored in different paths as per the OS.

Right now the project works just fine but it requires the file path as an arg which might be pretty difficult for non-experienced users.
Am trying to stay away from the #include <windows.h> header which might restrict the code to windows and environ() isn't working its magic to my liking.

Any code snippet is welcome.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline Ragehottie

  • Knight
  • **
  • Posts: 313
  • Cookies: -9
  • Hack to learn, not learn to hack.
    • View Profile
Re: OS auto detection - project feature
« Reply #1 on: August 16, 2013, 11:49:04 pm »
That's very poor practice with compiled languages.


Try detecting the os at compile time, or distribute 3 separate source codes/binaries
« Last Edit: August 16, 2013, 11:54:02 pm by Ragehottie »
Blog: rexmckinnon.tumblr.com

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: OS auto detection - project feature
« Reply #2 on: August 17, 2013, 07:26:46 am »
It is impossible to make a compiled program portable on run time. It can only be done in compile time, bro.
Different systems have different executable structures. What is compiled for windows, will not work for linux and vice versa, even if you don't use <windows.h>.
However, scripts, such as Python, can be portable between systems.

You will have to think of something better.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: OS auto detection - project feature
« Reply #3 on: August 19, 2013, 02:50:47 pm »
My only solution is to try code something for each OS then distribute different binaries.
OR better just distribute the source code for the different OS so people can compile it themselves.

Thanks.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: OS auto detection - project feature
« Reply #4 on: August 19, 2013, 03:15:19 pm »
Google "premake4" or "CMake" for a good compile time multi OS thingy :).
do some compiler commands to see if the build is OSX, Win, *nix or whatever.
CMake is also implemented for most IDE's
« Last Edit: August 19, 2013, 03:16:34 pm by WirelessDesert »
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: OS auto detection - project feature
« Reply #5 on: August 19, 2013, 03:59:03 pm »
CMake is also implemented for most IDE's
If you mean most IDEs like Code::Blocks, am right on it now.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: OS auto detection - project feature
« Reply #6 on: August 19, 2013, 11:00:00 pm »
It is impossible to make a compiled program portable on run time. It can only be done in compile time, bro.
Different systems have different executable structures. What is compiled for windows, will not work for linux and vice versa, even if you don't use <windows.h>.
However, scripts, such as Python, can be portable between systems.

You will have to think of something better.

Wrong I did it in OLink.  But it is a bit of a pain and probably too much trouble for most things.  But you can do it.  It just requires writing your own binary loader for say, ELF binaries, and then loading them yourself on each platform.  You will need a slight bit of start up code on each platform though.
« Last Edit: August 19, 2013, 11:00:55 pm by bluechill »
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: OS auto detection - project feature
« Reply #7 on: August 20, 2013, 07:41:50 am »
Wrong I did it in OLink.  But it is a bit of a pain and probably too much trouble for most things.  But you can do it.  It just requires writing your own binary loader for say, ELF binaries, and then loading them yourself on each platform.  You will need a slight bit of start up code on each platform though.
Doable while not very practical.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: OS auto detection - project feature
« Reply #8 on: August 20, 2013, 02:22:34 pm »
For the project am working on, it's just not worth the sweat as of now.
Just gonna bundle install scripts with the source code for both Os x and Linux.
I will try that with a more promising project and alot of time on my hands.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]