This question already has answers here:
How to write portable code in c++?
(12 answers)
Closed 9 years ago.
I'm trying to write code that can be compiled with any modern version of g++ and am having difficulties. I wrote the code using Visual Studios and it worked fine but I tried compiling it on another computer and it had problems.
One problem was it didn't recognize the macro EXIT_SUCCESS. I see here it's defined in cstdlib but how come in VS I didn't need to import that library?
Also in a .h file I had const private int PI = 3.1416 and on the other computer it didn't like that.
So how do you know what's going to be portable? I thought C (and therefore inherently C++) was invented to be compiled anywhere. I asked this question but think it go misinterpreted, are there certain settings that must be done to the IDE to ensure portability?
EDIT: I might understand it better knowing why did the code compile in Visual Studio without include <cstdlib> and how do you turn off such feature?
In practice the only way to tell whether the code is portable is to compile it under all target compiler/platform. You can try to adhere to C++ standard but reality is harsh and there is no 100% complient compilers. Even simple program can give you surprises on other compilers.
Related
This question already has answers here:
Which headers in the C++ standard library are guaranteed to include another header?
(2 answers)
Closed 2 years ago.
While making a project compile across both GCC and Clang, I've noticed that functions like fabs were causing compilation issues on GCC, since I had never included <cmath> myself. I was developing on Clang, so I never noticed this, since the llvm <cstdlib> header file I was using includes <cmath> itself, seemingly without doing anything with it, while the GCC version does not.
Is there any reason for this? and is there anything I can do to make sure my code will compile across multiple standard libraries besides just trying it?
For every function you use, consult the C++ reference to find out which header you should include.
The reference for fabs says that you have to include <cmath>.
This question already has answers here:
Why does omission of "#include <string>" only sometimes cause compilation failures?
(7 answers)
Closed 3 years ago.
I've stumbled across a surprising behavior on mac. When I use smart pointers and compile with apple's clang, it compiles even if I don't include the memory header.
This happens when I compile from terminal or use an IDE. When I use clion it doesn't notify me about the missing headers or anything.
This causes problems when I try to build on Linux, because the headers are missing. Is there a way where I can force clion or the compiler to be more strict about this?
The issue here is that the C++ standard only requires that the appropriate #include brings in the functionality you want into your compilation unit. This is by design and even allows compiler writers to hardcode some C++ standard library functionality into the compiler!
Many C++ standard library implementations #include headers that bring in C++ standard library functionality implicitly. Some are worse than others in this respect.
There's not much you can really do about this other than being strict about using the mandated #includes; something you can almost fix by trial and error.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a single-file CPP app, built for Windows and is running in command line only.
I am trying to make it compilable also for Linux.
The only libraries the app is using are:
#include <iostream>
#include "setjmp.h"
#include "windows.h"
#include "time.h"
It also uses all kinds of unsigned integers (uint8, uint16, uint32, uint64).
Other than this there aren't any other Windows-specific APIs.
My question is how to convert the code so it will be compatible with Linux?
Is there an easy way to do this?
Do it the other way round. Code, probably using some cross-platform framework like Poco or Qt (which is also usable in non-GUI code, e.g. using QtCore without QtGui), or Boost, in some Windows independent way and compile it regularly on both OSes.
BTW setjmp.h is C++ unfriendly (messing and incompatible with C++ exceptions), even on Windows.
So remove
#include "setjmp.h" /// wrong in all C++ programs
#include "windows.h" //// specific to Windows
then fix the code (e.g. all compilation errors; but on Linux compile with all warnin & debug info, e.g. with g++ -Wall -Wextra -g), probably using some framework like the ones I mentioned.
Don't convert C++ code (from Windows to Linux) but do try hard to write portable C++ code, thanks to some well chosen framework.
Perhaps your application could be written in some pure portable C++11 (but then, no need to #include "windows.h"). Command-line utilities like wc, cat, grep (with a subset of all features) could probably be written in portable C++11 -and might not even require any additional framework.
PS. Without having your source code and without any idea about what that application is, it is impossible to help you more. Your first task is to understand that code precisely and what it is supposed to do on Windows. Perhaps rewriting it in clean C++11 (maybe with some additional framework....) is the quickest way to do it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I compiled two code using Turbo C++ 3.0 and Borland C++ 5.02 compilers and come across some odd things my cods are like these :
First Code
void main()
{
}
Second Code
#include<iostream.h>
#include<conio.h>
void main()
{
}
and i got these results from them :
- Borland C++ (First Code) : 51KB
- Borland C++ (Second Code) : 51KB
- Turbo C++ (First Code) : 5.89KB
- Turbo C++ (First Code) : 16.3KB
I checked two Borland execute files with a hex viewer and realize they are exactly the same.
I examined the First Code form these compilers in IDA pro and come across these graphs :
Turbo C++
Borland C++
Now i have these question i'd like you to answer
1-Why Borland C++ compiled files are the same when one of them clearly dosen't have some include and another have?
2-Why Boland C++ compiled files are that big? (nearly 10 times bigger) and what is compiled that have that much size?
3-When i submit First Code to this Site i can see the assembly code of simple void main function and i realized that Borland C++ code is very much the same but Turbo C++ assembly code is very very complicated and isn't the same, why?
4-Why this simple Code that compiled with Turbo C++ create this much functions that you can see in it's graph?
Sorry about this long question but if you can clarify this to me that would be awesome i'm so confused right now.
I will do my best at answering these, but you may need to post your questions to the Borland forums for detailed answers. In any case, upgrade your compilers.
1-Why Borland C++ compiled files are the same when one of them clearly dosen't have some include and another have?
Your program has no functionality and is incorrect. (The main function returns an int, always.)
You can include all the header files you want. You don't use them, so there is no additional code generated.
Your program doesn't require any header files. The have the same functionality.
2-Why Boland C++ compiled files are that big? (nearly 10 times bigger) and what is compiled that have that much size?
There are many possibilities. You'll have to either look at the assembly code generated, machine code generated or post to the Borland forums.
This also depends on whether you compiled in Debug mode or in Release mode. It also depends on whether you compiled for static libraries or dynamic libraries.
Fundamentally, the Borland Compiler may be generating code that meets the standards required by later versions of Windows than Turbo C++ was required to support. Research the difference between ".com" and ".exe" formats.
3-When i submit First Code to this Site i can see the assembly code of simple void main function and i realized that Borland C++ code is very much the same but Turbo C++ assembly code is very very complicated and isn't the same, why?
See my answer to #2.
4-Why this simple Code that compiled with Turbo C++ create this much functions that you can see in it's graph?
Most likely because you are compiling in Debug mode; or because Turbo C++ is a simpler compiler, it doesn't optimize the libraries and code as much as Borland does. In Debug mode, there are symbolic information placed into the executable file.
By the way, the size of the executable may not be the size of the executable code placed in memory. The executable format allows for stuff other than executable code to be placed in the file, such as program symbols an line numbers.
Don't worry about program sizes anymore. Get the program working correctly, robustly and safely before optimizing for size.
Too those who know how, this may be a stupid question, but I'll be asking it anyway because I need some pointers.
The library I'm trying to compile is the SPro toolkit for speech signal processing which is written in (for lack of a better description) Gnu C++
It's a library written for unix and I want to compile it in windows. As much as an object exercise in porting code from, as anything.
Toward that end I have installed code::blocks and a MinGW compiler. I read that I could also use cgywn and that this would be introducing a layer interpreting the gnu c before executing it natively, but let me leave that to the side for the moment.
The first issue I ran into was that the #includes need a little love - ok no problem with that.
But now I find that
scopy.c|462|error: 'SIZEOF_SHORT' undeclared (first use in this function)|
I also installed visual Studio C++ and get the same.
I gather that SIZEOF_CHAR, SIZEOF_SHORT, SIZEOF_LONG, SIZEOF_FLOAT and SIZEOF_DOUBLE would be declared, in a header or somewhere.
Unfortunalty I don't have any idea where, so have no idea what to include to have access to the definitions.
What should I include?
Do I need to define these constants myself as I am in a different environment?
Also I may be barking up the wrong tree, any help is appreciated.
#define SIZEOF_SHORT sizeof(short)
#define SIZEOF_CHAR sizeof(char)
Repeat for all other types.