trying to install and run C++ test code in vs code - c++

Hi guys i installed the vs code and downloaded the needed extensions (C/C++-code runner)
and i installed Mingw
but when i run this test code
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
it shows this
PS C:\Users\faroo\Desktop\C++ 101> cd "c:\Users\faroo\Desktop\C++ 101\" ; if ($?) { g++ calssCar.cpp -o calssCar } ; if ($?) { .\calssCar }
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
can you please help me

The compiler is working fine but the linker trying to link it as a Windows GUI App, which uses WinMain() instead of main() as entry point of application.
You should recheck your compiler flags in vsCode settings. If I remember properly, for Mingw-w64, the flag is -mwindows.
I suspect you probably used some template to create vscode project but chose the wrong one (gui app instead of console app)
Also, as a beginner I would recommend using TDM-GCC instead of msys2, as it's better intergreted with Windows and is easier to work with.

one thing i noticed is IF YOU RUN YOUR CODE WITHOUT SAVING it is giving this error.
try RUN after saving

I suggest you create a new cpp file with another title.Then copy your code to the new file.Try to run the code in the new file again.Maybe it can works.
Such error usually happen to my laptop.It dosen't matter.

It's because maybe you haven't set your path of Mingw. Let me guide you to do that if you're using Windows:
Install Mingw (which you already have), like I have installed it in C:\Mingw
Then open Mingw\bin folder from that drive, like I have opened C:\MinGW\bin
Copy "C:\MinGW\bin" and then open This Pc.
Right-click on open space and click properties.
Go to Advanced system settings.
Click on the Environment variable.
Then in system variables, double click on PATH.
Click on new, and paste your path there.
Press ok and now run your code again in Vscode, it'll surely work!

Related

i don't know what is wrong with vs code in running cpp

i just started to code in cpp and nothing is working idk if i didn't install the gcc properly or what but i already set the path of the bin file idk why it refuses to run
the code:
#include<iostream>
int main()
{
std::cout<<"hello";
}
and the problem is that when I try to use the "code runner extension" it is not working so I just press f5 and then when I get the error messages which says at first "could not find the task file'c/c++:g++.exe build active file' " and I get three options 1:debug anyway
2:configure task
3:cancel
when I choose debug anyway I get this error here
Since you're on windows consider installing Visual Studio or CLion. They're more beginner friendly - VSCode can get tricky to run c++ on windows. Either way, looks like you're trying to run your project without building it first. There should be a build option right next to the run option. Try building it, then running. The build is what compiles and creates the project.exe file, which is what the compiler is saying isn't there.
The referenced IDE's always auto-build on run, so there's that
If you're familiar with using the command line, these commands will do what you want, assuming the .cpp file is in your current directory.
g++ <FILENAME>
./a.out
There are wonderful flags you can add to the first command (the compiling command) like -Wall, -Wextra, -o, and many more. But, since it seems like you're just starting out, there's no need to worry about those!
If you're not familiar with the command line, I would encourage you to become familiar! It is an extremely powerful tool for a programmer. Here is a YouTube video that talks about it.

Failed to locate: "CL.exe". The system cannot find the file specified

I'm trying to get into SDL2 in C++ and I have followed all the steps in a variety of YouTube tutorials. I am using vs2019, and every time I try to run anything I get this error: Failed to locate: "CL.exe". The system cannot find the file specified.
For example, it happens when I try running this code:
#include "SDL.h"
#undef main
#include <iostream>
int main() {
int x = 1;
std::cout << "Hello World!\n";
std::cin.ignore();
return 0;
}
I have followed all of the steps, but nothing seems to work. Similar questions have been asked, but their solutions don't work for me. I'm just running it in the editor. Can anybody help me?
You need to run VCVARSALL.BAT for Visual studio 2019 in the console for making cl.exe available.
Alternatively you can run "Developer Command Prompt for VS 2019" from the Start menu and run cl.exe there (after changing the appropriate working directory to one containing your code).
I am going to post this as a potential answer, pending OP’s clarification to what he means by “running it in the editor”.
MSVS does not install with the C and C++ compiler and libraries unless you specifically ask for them. To install them you must re-run the installer. You will eventually get a screen with a lot of different options on it. It is a tabbed page with “Workloads”, “Individual Components”, and “Language Packs” at the top.
Find and make sure that you select both:
“Universal Windows Platform development”
“Desktop development with C++”
Continue as usual. The installer will update MSVS with the ability to compile C and C++ programs.
You can also read Microsoft’s instructions.

Can not run make command in Dev-cpp application

I have a very simple example named HelloWorld and located in folder "C:/HelloWorld" in Dev-Cpp as follows:
#include <iostream>
using namespace std;
int main(){
cout << "Hello World";
return 0;
}
Of course, if I press F11 (Compile and run) then the program executes normally.
But I want to execute this program in an another way: use make command!!!
I have known that the syntax to run make command : make -f [make file name]. In Dev-Cpp, this command is make -f "Makefile.win". So I jump into HelloWorld folder and type as follows:
The console displays many errors relating to included headers.
How can I run make command without any errors? Thanks in advance.
not so easy with make :)
at first if you use msvc or DevCpp try to find "Export makefile"
at second you need to configure your compilator(s).
at third you need to show what Makefile.win contains.
man:
http://www.gnu.org/software/make/manual/make.html
gcc:
https://gcc.gnu.org/install/binaries.html
All right. There are something wrong with my make command.
The problem is I had installed two versions of gcc in my computer. The HelloWorld example is compiled and built with the latest GCC version.
However, the make command I had type is belonged to the older GCC version. As a result, it leads to many errors.
In order to run make command correctly, I must to use the right make command that is defined in Dev-Cpp (below picture): mingw32-make.exe

Qt not building project file

I'm getting started with C++ and I use Qt Creator (I run Lubuntu 13.04).
Today, as any good start with programming, I wrote my Hello World program to see if things work after installing Qt5 and Qt Creator.
I created a new Console Application project and wrote:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
Saved my project as Test, and tried to run it.
I just keep getting
Starting /home/MYUSERNAME/Qt Programming/Test/Test...
Cannot change to working directory '/home/MYUSERNAME/Qt Programming/Test': No such file or directory
/home/MYUSERNAME/Qt Programming/Test/Test exited with code -1
Any ideas on how to fix this?
Thanks in advance.
Just maybe problem is a space in 'MYUSERNAME/Qt Programming/Test'.
Anyway take a look in project build settings. Something is wrong with path where compiler is looking for your project
Are you sure there is a "Test" directory in the "/home/MYUSERNAME/Qt Programming/Test" directory ?
Using my qt4 installation I've tried starting a new Qt Console Application, chosen the folder to create project in, typed out Test for the Project name hit next, deselected Debug, but kept release. Hit next. chose none for version control, hit finish.
Program skeleton shows up. Must delete the include statement for QCoreApplication. Replace it with iostream. Deleted "QCoreapplication a" and "return a.exec". put in std::cout<<"Hello World \n"; and return 0;. Then I saved everything hit the play button and xterm is opened and displays hello world. No problems here. I even tried with spaces in directory names. Again no problems.
Then I tried creating project non-qt-project->plain c++ project. After the same menus as above, found a skeleton with exactly your code except with an endl at the end of cout statement. Hit the play button. Again no problems here. Again it opened up xterm and printed out Hello World!
Don't know what else to tell you. If you changed the name to Test AFTER you created the project, I would say, start again and this time don't change the name.
I may install qt5 on my opensuse 12.3 x86_64 installation and will be able to see if there are problems with it.

"windows cannot access the specified device....." error in c++

I am MFC guy working on visual studio 2010 create some executables using visual studio!! but on linux and mac my executables are not working as usual windows!!.
So i decided to use "MinGW" compiler to create executables.
Note:-Please give me one suggestion is that," is minGW is best compiler for cross plateform working ??or any thing else is there??"
I successfully install WinGW compiler on my C drive and start working with following program..
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello ";
return 0;
}
I compile it using following command,
g++ -static-libgcc -static-libstdc++ Main.cpp
I found one executable in same folder with name a.exe :).Working fine:)
But after some time i decide to modified same program in following manner like,
int main ()
{
return 0;
}
I compile it with same command but when i execute it using command line it show me error "Access is denied so i goto that folder and run same executable as "Run as Administrator" it show me one messagebox with the message windows cannot access the specified device path or file. you may not have appropriate permissions
---EDIT--
follwing code is NOT WORKING:-
int main ()
{
int k;
return 0;
}
but this program WORKING :-
int main()
{
int k;
k = 0;
return 0;
}
If you are getting this access denied error, then the most likely cause is that the executable file is open in another process, probably the linker or debugger. Try installing Process Explorer and hit Ctrl+F and type in the name of your .exe. This should show the processes that the .exe file open. Kill those processes (or if you are still debugging, then end debugging first). You then should be able to build again.
Note that this has nothing to do with Microsoft APIs, as in any case you're using gcc.
EDIT: If there are no processes holding the .exe then it may be that there is some other kind of permission problem. Does the .exe file exist? Can you delete the file and rebuild? Another thing to try is run Process Monitor and filter for the name of the .exe -- that may show a regular permission denied error, or perhaps another error such as a sharing conflict.
Note:-Please give me one suggestion is that," is minGW is best compiler for cross plateform working ??or any thing else is there??"
No. And there's nothing else out there.
Use whatever compiler is available on target platform, ensure your code compiles on all of them.
Avoid platform-specific and compiler-specific code at all costs (use cross-platform frameworks).
I successfully install WinGW
There are many different versions of mingw provided by different sites. If you install compiler from mingw.org using mingw-get, it'll probably work. If you install mingw from some other site, it may or may not work.
I compile it using following command,
Use a build systems. cmake, qmake or something similar.
it show me error "Access is denied
Launch process monitor and see after which system call it terminates. It is also possible that your antivirus software interferes with your program, or maybe there's some stray dll in your path or something like that.
Check the permissions for the entire folder in which the executable resides. Trying to 'Run as Administrator' doesn't have any effect if the folder doesn't allow the permissions.
It doesn't have anything to do with your code. This is an environmental problem, something is pretty messed up about the permissions your user account has for one or more of the directories on your hard disk. The generic diagnostic is that the default working directory for the program does not permit read or list access.
A possible starting point would be to use Explorer and right-click the directory where MinGW is installed. Use the Security tab and ensure that your user account has all permissions enabled. Further narrow it down to trying to run the program from the command prompt, using different directories as the default directory.