Dev C++ compiler file is used in VS Code - c++

I have set Dev C++ Compiler MinGW bin folder as Enviroment Variable Path, So that i can use it in cmd and VS Code terminal also, But it compiles well and only performs simple operations.** It cannot give class object function output
**
Path which i have added C:\Program Files (x86)\DevC++\MinGW\bin
#include <iostream>
using namespace std;
class Base{
public:
void greet(void){
cout<<"How are You ? \n"<<endl;
}
};
int main(){
Base obj;
cout<<"Good Morning!\n";
obj.greet();
return 0;
}
Output:
Good Morning!
Expect Output:
Good Morning!
How are you ?
I want to know if it is same compiler file of MinGW in DevC++ why cannot we use outside DevC++, why do i have to download seperate compiler for VS Code.

The IDE you're using (Dev C++ / VS Code) doesn't really matter that much with regards to your issue, what does matter is the compiler.
Make sure to not mix compilers, certainly if their versions very much apart.
And Dev C++ is shipped with an old version.
I recommend using a newer MinGW-w64 compiler - you can get a standalone version fro https://winlibs.com/ - and configuring your IDEs (both Dev C++ and VS Code) to use this same compiler.
Only then can you be sure you're linking things together that play well together (unless you're still pulling in other dependencies build with different compilers).

Related

Assistance including a basic SDK into a C++ program Dev C++

I have been making some applications that I would like to link to discord, specifically with Discord's SDK. The SDK comes with 64 and 32 bit x84 lib files, and a C++ folder full of the includes it needs, so I would assume C++ is supported.
However, I am very terrible at linking libraries or anything at that, and always run into issues when linking. I am using Dev C++ as my IDE, and my code is as follows:
#include <iostream>
#include "Discord/discord.h"
using namespace std;
void InitDiscord()
{
auto discid = 772671910668133376; //Not my actuall discord app ID, but real one does not make a difference
discord::Core* core{};
discord::Core::Create(discid, DiscordCreateFlags_Default, &core);
}
int main(){
InitDiscord();
cout << "Discord active";
while(1){
}
return 0;
}
and I am getting the error:
C:\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe Discord Testing.o:Discord Testing.cpp:(.text+0x32): undefined reference to `discord::Core::Create(long long, unsigned long long, discord::Core**)'
for only the line discord::Core::Create(discid, DiscordCreateFlags_Default, &core); and not discord::Core* core{};
I am using C++17 and a newer TDM-GCC compiler, the same one that works for all of my other applications. I am including the .lib files and .dll files in the program's directory, and in the linker the only thing I am using is -discord_game_sdk.dll.lib which is a valid directory. I have also tried discord_game_sdk.dll.lib and putting the library in the same directory as the includes: Discord/discord_game_sdk.dll.lib. I have tried using both 32 bit and 64 bit libraries in all project and compiler directories with no change, and im sure this is something probably really simple, but nowhere have I found any example C++ discord programs or how to include their SDK.
If anyone could figure out what the problem is and how I can fix it, that would be very helpful and appreciated.
EDIT:
It appears that user4581301 was right, TDM-GCC and other Mingw compilers do not support .lib files, and will ignore them despite being linked. The SDK did not come with any other formats other than .dylib, .so, and .bundle.
This creates a somewhat new issue, I already have my compiler set up and cannot really switch to Visual Studio, so I need a way to convert .lib to .a somehow. A post here recommends http://code.google.com/p/lib2a/ , which requires a .def file, another file that did not come with the SDK, but apparently a program called gendef.exe that came with my compiler can create .def files from .dll files. That is indeed the case, however when attempting it I get the error:
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin>gendef.exe discord_game_sdk.dll
* [discord_game_sdk.dll] Found PE image
* failed to create discord_game_sdk.def ...
with no other warnings. Now I need to know if I am converting wrong, if there is an easier workaround, or if one of the other file types can be converted or used. Any suggestions at this point are welcome and appreciated!

Why does string type cause c++ program to not run?

I have a simple program, written in C++, on a Windows 10 machine, compiled with the MinGW g++ compiler.
I am including the <string> header, and it runs fine. When I include the string data type, it will compile, but it will not run.
The minimum amount of code to reproduce this is:
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting;
cout << "hi" << endl;
return 1;
}
Please note, this works fine:
#include <iostream>
#include <string>
using namespace std;
int main() {
//string greeting;
cout << "hi" << endl;
return 1;
}
The version of the compiler is 6.3.0
g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
I downloaded it 3 days ago, so I assume it is the most recent version. (Maybe not?)
I have googled and browsed stackoverflow for answers.
The closest question I could find, the person gave up and changed operating systems.
One other solution I found was to use Cygwin's compiler. I would rather not, as I already have a seemingly otherwise fine compiler. It seems that MinGW tools are widely used enough that I should be able to use the compiler.
Any other forum/blog/etc resources have problems about converting strings or calling string methods.
NOTE: The same exact code runs fine on Ubuntu 16.04, with the included compiler.
EDITS:
While I realize that "It does not run" is not helpful, I don't know how else to describe it. I run the compiled program, and it behaves the same as if I entered echo ''. There is no output, no indication that anything has happened. What is the most helpful way to phrase that behavior?
If I use a debugger, I get program exited with code 0xc0000139
A quick google search returns results indication that it is a problem with the compiler. Same as the comments below about my compiler version...
To compile the program, I run g++ main.cpp -o b.exe
To execute it, I run ./b.exe
Mingw has a long standing issue with certain consoles (see their faq). It might be worth checking whether it's failing to output rather than execute by e.g. redirecting to a file ./b.exe >out.txt.
After some useful comments, and much frustration, I decided to try to build the most current compiler. I caved and used Cygwin. (I really have no problem with Cygwin, I just wanted to get MinGW to work.)
I followed this site's instructions (after downloading the current source from https://gcc.gnu.org).
http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/
Notes:
This is for an older version of gcc, but I replaced the appropriate version numbers with 8.2.0 (current at this time).
I was missing some prerequisites, and had to cd into the source directory and run ./contrib/download_prerequisites from the Cygwin terminal. It handled everything seamlessly, and while it took a little bit, I am now able to use string data types and run the program (successfully).
Don't forget the final make install command. I did, and it was a headache.
Lastly, thanks to all the helpful comments, and those that asked for clarification. When I call the mechanic, and say, "my car won't run", they ask helpful questions, and we work together to get them the relevant information they need. That's what happened above, and I learned some things (and solved my issue).
I had the same issue. I downloaded mingw a few days from sourceforge and everything complied fine except when I declared a string. Then it would show no output. Maybe it was some problem with the old version (6.3.0). I deleted all the files and re-installed using the given tutorial : https://code.visualstudio.com/docs/cpp/config-mingw . This is a newer version (8.1.0). Now it works just fine!

How to make sure code (c++) written in Xcode can compile on other platforms?

I am a beginner was trying to do some C++ programming on Xcode. It works fine, but when I try to compile the same c++ file on my windows pc using VS, there were some errors. After I look at my code closely, there are really some stupid mistakes that I have made which caused the errors, but Xcode seemed to have ignored them...
My question is that is there any setting that I need to change to prevent Xcode from being so smart?
For example, the following code can actually compile in xcode:
#include <iostream>
using namespace std;
int main() {
if (true or false){
cout << "How is this possible? \n";
}
return 0;
}
There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.
As far as I can see there is nothing wrong with your code.
The ISO C++ standard does not specify which standard headers are included by other standard headers. So, it is entirely possible that the version of iostream used by Xcode directly or indirectly includes ciso646. Whereas Visual Studio's version of iostream does not include ciso646. There are many similar cases with other headers. You just need to read the error messages and realize that your error (when you move your file to a different platform) is due to a missing header file.
It would be nice if writing portable code meant writing code in accordance with the C++ standard specification, but unfortunately that's not the case. Although there are various compiler options on various implementations which can help bring different implementations closer together, in general you will just have to bring the code into the target environment and actually test it there.
So ultimately writing portable code means you'll have to learn some subset of C++ that is accepted by all the implementations you want to target.
or is an 'alternative token' in C++, and VS is incorrect to reject it. There's no option in Xcode to disable support for alternative tokens. However VS has non-standard support for or as a macro using the header <ciso646>, and Xcode does have a header <ciso646> which does nothing (as the standard specifies). So you can write code which uses or and which works in both Xcode and VS by including this header.
#include <iostream>
#include <ciso646> // does nothing in Xcode, allows `or` in VS
using namespace std;
int main() {
if (true or false){
cout << "How is this possible? \n";
}
return 0;
}
Unfortunately VS can't support all of the alternative tokens through macros and so Xcode will still support some that VS doesn't.
There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.
If you give specific examples then I can provide additional advice on how to write portable code.
Rather than changing your Xcode settings, I suggest cross-checking your code using another development environment.
If you're looking for something cheap and full-proof. Download a VirtualBox Windows VM, and run download Dev C++ (bloodhshed)
VS does not support or: you need to use || instead.
You can include some special files but it doesn't inject or sufficiently well into the language for it to work in all instances.
If you want to suppress use of or (and your compiler supports no better way)
#define it to something that emits a compiler error, for example
#define or OR
This at least means that the nature of the compilation errors will be identical on Xcode and VC.

Orwell Dev C++ doesn't work with C++11

I'm trying to use any of the C++11 features in Orwell Dev C++ but with no luck. I installed the version with minGW and whatever I set in the compiler options, I just get the "[Error] 'to_string' was not declared in this scope" in this code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string test = to_string(5);
}
I tried setting -std=gnu++11 and -std=c++0x but neither does the job. What's the most curious is that when I click on to_string, it shows me the overloaded functions - for long, float, int and so on. Thus, it must somehow get what the function does - how come it doesn't compile it, then? The compiler is set correctly to MinGW GCC 4.7.2 (the one bundled with the installer).
If you want to use C++11 in Dev-C++ you should to this steps:
Go to Tools > Compiler Options
Go to the tab Settings > Code Generation
Change the parameter Language Standard (-std) to ISO C++11
It is a known bug that to_string does not work with MinGW yet (which is actually GCC's fault, to a degree):
http://sourceforge.net/p/mingw/bugs/1578/
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015
Intellisense is often driven by a different engine than the compiler (as very few compilers have hooks to make intellisense easy), so that's likely why you're seeing it in your IDE when it's not supported by your compiler.

How to Make Visual Studio C++ 2010 Compilation Behave Like gcc/g++? (or vice-versa)

Say you've got the following simple main.cpp file:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
const string FILENAME = "foo.txt";
ifstream somefile(FILENAME);
populations.close();
return 0;
}
This compiles fine via Visual Studio C++ 2010.
However, on a Linux-based system, if I execute make main and compile, we get an expected error since we didn't call c_str() on the string constant, like so:
ifstream somefile(FILENAME.c_str());
As is commonly known, and described in this SO thread.
How can I get VS to behave like gcc/g++ and raise a compilation error for the code above? Or, how can I get gcc/g++ to behave like VS and compile the above without error? (Is it a simple matter of upgrading my gnu compiler?)
(I don't believe disabling compiler extensions is a solution, as I've done this and it still compiles without error.)
Visual Studio behaves correctly in this case with respect to the C++11 standard (it works on g++ now, too). I'm not sure why would you want to do this, but you'll probably need to edit MSVC's headers (not advisable and rather drastic).
Strange thing is though, that they don't write it in their documentation. Can you check which constructor is actually being called?
It is available as part of the newer c++ standard.
To disable, add
#define _HAS_CPP0X 0
at the top before your includes.