C++ Cross-platform Windows/Mac?

So I've been hearing several things, and I'm assuming the answer lies somewhere in the middle. I've been coding in C++ on Windows, but I heard that to have it run on Mac, you need a windows emulator (like wine - used it before was pretty nice) or to rewrite your whole code.

That being said, I also hear about how C++ is good for cross platform (which can't be that true if you have to rewrite whole programs !).

Now - My question is WHEN do you need to rewrite your code when going to a Mac? Moreover, HOW do you need to rewrite it? What do you have to change with the code in order for it to work?

For Example - What would I need to change with code like this for it to work on a Mac? Thanks !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  
#include "stdafx.h"
#include <iostream>
#include "constants.h" // ONLY HOLDS VALUE OF "gravity" WHICH IS 9.8 METERS/SECOND
#include <cmath>
#include <iomanip>

// Sorry For Not Initializing - But So Far Visual Studio Handles Them As If They're '0' Until Otherwise Assigned.
char g;
double h;
double d;
int s;
double a;
double p;
std::string o;

double Distance(int s)
{
	a = pow(s,2) / 2;
	return a * constants::gravity;
}

int main()
{
	std::cout << "One Meter Is 3.28084 Feet. One Foot Is .3048 Meters." << '\n' 
	<< "Type In 'C' Followed By The Number Of Feet To Convert Feet To Meters."  << '\n' << std::endl;
	std::cout << "The Height In Meters Of The Dark Tower: ";
	std::cin >> g;

	if (g == 'C' || g == 'c')
	{
		o = std::cin.peek();
		if (o.find('\n') != std::string::npos) // In Case They Type In 'C'/'c' With No Number To Convert.
		{
			std::cout << '\n' << "Must Input A Number To Convert." << '\n' << std::endl;
			return 0;
		}
		double x;
		std::cin >> x;
		h = x / 3.28084;
		std::cout << x << " Feet Is " << h << " Meters." << std::endl;
	}

	else
	{
		std::cin.putback(g);
		std::cin >> h;
	}

	if (std::cin.fail())
	{
		std::cout << '\n' << "Input Only A Number." << '\n' << std::endl;
		return 0;
	}
	
	double i = h;

	for (h; i > 0.000; s)
	{															//Precision Set At 6 Since It Shows Less Math Error
		std::cout << "At " << s << " Seconds, The Ball Is At: " << std::setprecision(6) << i << " Meters." << '\n';
		s = s + 1;
		i = h - Distance(s);
	}
	if (i <= 0.00) // Was Coding Quickly - this may not need to be an if function - But Better Safe Than Sorry.
	{
		std::cout << "At " << s << " Seconds, The Ball Is On The Ground." << '\n';
	}

    return 0;
}
Last edited on
As long as you use standard C++ you don't need to rewrite it.
You 'only' need to recompile it.
The code you posted should work fine on a Mac.

You would need to rewrite it if you use some Windows specific functions - like the Win API.
Last edited on
Ah, I see - Thanks! That's what I thought at first but I wasn't sure. Thanks again Thomas1965.
closed account (E0p9LyTq)
Line 2 would probably "choke" on a Mac compiler, "stdafx.h" is a Visual Studio specific header. Added to manage precompiled headers in the VS IDE.

I use VS 2017. When I create a new project/solution I always use the Windows Desktop Wizard so I can choose to not use precompiled headers and create an empty project. I add source files as needed.

The rest of your source is standard C++.
Last edited on
Line 2 would probably "choke" on a Mac compiler, "stdafx.h" is a Visual Studio specific header. Added to manage precompiled headers in the VS IDE.
It will work if you remove all non-standard headers from stdafx.h.
It's just a header so it will work like any other header. Not sure if clang or GCC support pre-compiled headers.
closed account (E0p9LyTq)
The OP wants standard C++ coding for cross-platform use.

Why not just remove the non-standard header? Or make sure it isn't automatically added?
I would initialize them, because gcc/g++ under cygwin in Windows 10 doesn't always set them to zero...

... I mean if you want it to be cross-platform, why not include the most popular cross-platform compiler as a working target ;-)
Why not just remove the non-standard header? Or make sure it isn't automatically added?

stdafx.h is just a user-defined header. It doesn't matter if it is standard or not. Important is that the content is standard. On VS you can use it for pre-compiled headers on Mac or Linux use it as a normal header.
Thanks everyone for the replies. If I was going to use this code on a different computer/compiler, then I'd put the 0's in just in case, but for now it kinda feel weird to do it - I'm developing bad habits already !

"stdafx.h" is only for visual studio, right? - The code will work on any other compiler even if that header is removed (In fact some other compilers wont work unless that specific header is removed).

I'm just glad to know that C++ is good with cross platform for if in the future I need code that'll work on other computers. I usually stay away from Windows specific coding for this reason.

Thanks again everyone !
Last edited on
"stdafx.h" is only for visual studio, right?

NO,
imagine the following scenario. You have a project with main.cpp and stdafx.h created on Windows which look like this:
main.cpp
1
2
3
4
5
6
#include "stdafx.h"

int main()
{
  // some code here
}

stdafx.h
1
2
3
4
5
6
7
8
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

// some other code that rarely changes 

On Windows and Visual Studio you can use pre-compiled headers if you are interested that's what stdafx normally is for, but you can easily ignore this.

To compile your project on Linux and Mac you just copy main.cpp and stdafx.h to the other coumputer and use clang, GCC or sth. else to compile.
Topic archived. No new replies allowed.