Is there a way to create cross platform programs in C ++?

14

I'm programming C and I'm learning Java now, but I really enjoyed learning C ++ and I wanted to know if it's possible to create cross platform programs, that is, to run on any OS without having to compile the code in each OS.

    
asked by anonymous 08.06.2016 / 21:39

2 answers

11

Without compiling for each platform there is no way. But there is virtually no language more portable than C ++. It is more than Java.

Java's motto is "write once, run everywhere", even though it's a lie that runs anywhere and does not tell the whole truth because it does not tell you where it runs, it usually does badly, because it uses the least common denominator run everywhere.

C ++ prefers "write once, compile everywhere". Of course you have to take some care, you have a situation that you do not only write once. But at least it runs everywhere and runs well, if the programmer knows what he's doing. C ++ does not sell illusions.

Note that it is not normally necessary to have the target operating system to produce an executable for it. Good C and C ++ compilers can produce executables for other platforms from where they are running. Of course this is not worth testing. At the very least you'll have to have a virtual machine with the desired operating system if you want to test if everything is ok on that platform. Something that everyone should do, even programming in Java, save very simple applications.

In good applications, it is actually useful to compile for multiple platforms is not a problem. Who chooses C ++ usually wants quality and C ++ gives everything it needs to deliver this quality. It requires more effort, of course.

Virtually everything that counts for C, applies to C ++.

Cross-platform library

Each operating system has its peculiarities. Whenever you are doing IO (data entry and exit operations, including screens, networks, etc.) you may encounter different conditions. Some libraries can abstract this and help the same code be used in different systems without or with minimal modifications. This is the same as in Java.

But no library will do the best, they all adopt the "least common denominator." Some more successful than others. Those who try to abstract more end up creating bad products on all operating systems, although they may eventually privilege some specific ones to the detriment of others. Those that abstract less produce better results, but require their code to have different parts for each operating system, to a greater or lesser extent.

One of the most commonly used GUI libraries that comes with several extra abstractions of operating systems is Qt . It works very well on Windows, is considered native to Linux, has greatly improved in the mobile world (mainstream ) and has acceptable results on MacOS.

Applications with less IO features, or at least more basic IO like console and files, are easier to write a single code and run on all platforms.

    
08.06.2016 / 21:46
2

There are several alternatives in C ++, allowing you to run the same source code on several platforms, but none is "automatic" without having to compile for the final platform as asked.

You can make the code compiled from one machine to another (cross-compile), but you really do need to compile for each specific platform you want to use.

    
08.06.2016 / 21:57
=, BINARY, LIKE, LIKE BINARY, REGEXP, SQL binary collation ___ ___ erkimt Error in MySQL "expects parameter 1 to be resource, boolean given in" ______ qstntxt ___
%pre%

I have this code and can not see error in it, but it gives me this error. I know there should already be a topic of this on the site but I have tried all the ways and nothing. Give me the bool error (false)

    
______ azszpr28190 ___

This error happens when %code% or %code% fails, it is usually a syntax error in the sql query and returns a %code% as explained in manual . For mysql_fetch _ * () to function properly you must pass a variable of type %code% or %code% which is the return of %code% / %code% on success.

To fix the error you can force mysql_ * mysqli _ mysqli _ to display the database error with the mysql_error () or mysqli_error ()

Version with old mysql functions _ *

%pre%

Version with mysqli_ * procedural

%pre%

Version with mysqli_ * OO

%pre%

The query will return this error because %code% is a reserved mysql word.

  

Error Code: 1064. You have an error in your SQL syntax; check the   manual that corresponds to your MySQL server version for the right   syntax to use near 'desc'

The other way is to print the query and test directly at the database:

%pre%

Related:

Why should not we use functions of type mysql_ *?

MySQL vs PDO - which is the most recommended for to use?

How to print the SQL statement being sent to the bank?

    
______ azszpr333477 ___

The problem occurs because the %code% method needs a parameter with the connection to the database.

I leave an example below. Remember to modify the data to connect to the bank.

I leave it as a hint for you to study using the PDO because it is safer and the %code% is already depreciated.

  

Example:

%pre%     
___