C/C++ - MinGW - run/compile program with batch - c++

I'm trying to run my program with MinGW compiler through batch file. But when I try to launch it, it says nothing.
Here is a code:
g++ -o Learning.exe Main.o
pause
When I just opening .exe file, it working perfectly. How to fix that?

GCC is silent if there aren't any issues, errors or warnings.
Do you want to run the actual program you created? Right now you just link it.
To compile and run if there weren't any issues, you can use the following:
g++ main.cpp -o learning.exe && learning.exe
pause
The part behind && won't be executed, unless there haven't been any error during compilation (return value not being 0).

Related

How to run C++ program from terminal VS Code

I want to run a C++ program in VS Code. All I get from Google is that click on run and debug (the play button) on top right in VS Code and my program will be up and running. I don't want to do from that. I want to do it from terminal.
Example, to run:
A Python file I do: python3 fileName.py
A Flutter program: flutter run
A Java file: javac fileName.java
A Go file: go run fileName.go
Is there any command similar like this in C++?
Apologies, I know my question is a little naïve.
i guess the short answer would be :
$ g++ -o < name-you-want-to-give> source.cpp
In place of replace it by any name like myprogram, etc.
./myprogram
This mean you had to install gcc compiler beforehand.
I need to be in my project directory and then i need to run
g++ 01inputFromUser.cpp -o 01inputFromUser && "/home/aman/Desktop/arjun/cpp/"01inputFromUser
so this was what I was looking for
g++ fileName.cpp -o fileName && "/path/to/project/"fileName

GDB list shows nothing

A program compiled with arm-linux-gnueabi-gcc -g3 main.c .
$ gdb a.out
$ l
$ main.c: No such file or directory.
It is unable to display program lines with line number. let me know If i am missing something ?
However I am able to run program , with run command even backtrace I am able to get.
My issue is same as gdb can not load source file?
but , GDB version 7.8.
GDB needs source code to be present on same machine as in where binary been run.
mine was cross compilation , I was running program on different host . That's why I had that issue.

MinGW gcc C compiler works, but g++ does not

I'm a noob, I admit it. Regardless, I've had a really annoying problem with MinGW.... I can write AND compile programs in C with no problem whatsoever, but recently I've tried to install Cmake, but I can't because it fails every time it tests the C++ compiler (g++). So that lead me to just write a simple "hello world" program in C++ and try to compile it. No dice. Again and again I get no response whatsoever. On the command line, I'm typing
g++ -o hello++.cpp hello++
but have also tried
g++ -o hello++.cxx hello++
g++ -o hello++.cc hello++
g++ -o hello++ hello++.cpp
(Of course I saved copies of the source code in the same directory with the .cxx and .cc extensions respectively)
and a bunch of other combinations thereof. Everytime I get nothing. No warning. No error. Nothing. No .exe file was created in the directory, and typing "hello++" on the command line afterwards just gives me a "command not found" error. Soooo.... what the hell is going on? Why does the gcc command work but the g++ not?
I'm on windows 8, using cygwin.
The last one should have succeeded; you won't get a message saying it's succeeded, but you should see the executable in the current directory. The others all try to take the executable as input and output the source, which won't work; although I'm surprised you don't get error messages.
If it did succeed, then simply typing hello++ is unlikely to run it since the current directory is typically not on the path. Try ./hello++ instead.
If it didn't succeed, then it's possible that g++ isn't installed properly. I'm afraid I've no idea how to fix a broken Cygwin installation. Perhaps which g++, to see which program is actually being run, might give some clues.

How do I create a .exe from a .cpp file in Code Blocks?

I've just started learning C++, and to display the outputs of code I found this method. This worked when I first compiled Structure of a Programme.cpp:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
It gave me a .exe, which I opened, ran, and got a lovely 'Hello World!' appearing, but when I tried compiling a second one, Variables.cpp:
#include <iostream>
using namespace std;
int main ()
{
int a, b;
int result;
a=5;
b=2;
a=a+1;
result=a-b;
cout << result;
return 0;
}
I didn't get a .exe at all, so couldn't work out how to open it. I tried re-compiling Structure of a Programme.cpp (after deleting all the associated files), but now that won't create a .exe anymore, either. The only files created are Structure of a Programme.o and Variables.o (in a sub-directory obj\Debug).
The only question I could find that seemed similar was this, but the problem seems to be slightly different, and I tried deleting one of the files (so there was only one of Structure of a Programme.cpp or Variables.cpp in the folder) and I still had the same result.
Also, there were no compiler errors with either file, and I don't think I changed any options in Code Blocks between Structure of a Programme working and everything not working.
Thanks,
Dalkius
edit: Build logs:
Compiling: Structure of a Programme.cpp
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
Compiling: Variables.cpp
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
edit 2: 'Full Commandline' build logs:
Build started on: 14-12-2011 at 07:57.39
Build ended on: 14-12-2011 at 08:01.03
-------------- Clean: Debug in cplusplus.com Tutorial ---------------
Done.
mingw32-g++.exe -Wall -g -c "D:\My Documents\0HOME\Programming\C++\Code Blocks\cplusplus.com Tutorial\Structure of a Programme.cpp" -o "obj\Debug\Structure of a Programme.o"
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings
mingw32-g++.exe -Wall -g -c "D:\My Documents\0HOME\Programming\C++\Code Blocks\cplusplus.com Tutorial\Variables.cpp" -o obj\Debug\Variables.o
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
From looking at your updated build log, it appears the linking step isn't being performed to generate the final executable. There are a couple of things you can check and some ideas to try out:
Make sure the linker executable and proper path is set so C::B can locate it. For MinGW the linker is invoked through the compiler driver named 'g++.exe'.
Check that 'Console application' is selected under 'Type'.
If everything looks okay but it still doesn't link try creating a new blank console project. Add the existing files to that project and try building it.
Try building it manually from a 'cmd' command prompt to make sure the toolchain itself is functioning. You should find a 'mingwvars.bat' script under your mingw install. Run that script to open up a proper commandline environment. Do a simple test compile using this command:
cd "D:\My Documents\0HOME\Programming\C++\Code Blocks\cplusplus.com Tutorial"
g++.exe -Wall -g Variables.cpp -o Variables.exe
Lastly, this is what your log should approximately look like when it's building correctly:
EXE files are mostly build each time when you run the code. Try finding the exe file of your program where you have installed or copied the C++ program files.
I'm not too familiar with codeblocks, but I'll try to help by explaining what the compiler is doing. Those .o files it's creating are called Object Files. Compilation at a high level works as such:
1) Your source code get's compiled by a compiler.
2) The compiler will interpret your code and create an object (or .o file) for each file you have (in general anyways).
3) These files are then "linked" together in part of the compilation process known as "the linker".
4) Finally, the linker puts out your .exe file.
There's of course more to this (such as library files, pre-compiled dlls, pre-processing, etc.) but for your purposes you can think of it like above as you're just starting out.
My guess would be you may have accidentally modified something with codeblocks linker, or it is looking in the wrong place to link the files - or even the linker is throwing an error (though most IDEs inform you of this). Again, I'm unfortunately not too familiar with codeblocks.
If there is any way in codeblocks to trigger a "clean" you should also try that and try rebuilding. This will remove (clean) up any old files that may still be there from last build.
After building your program, in Build log you can see 'Executing:' where you can find the path of .exe file which your program just created.

Displaying info in the eclipse console

I'm trying to compile a C++ project (Hello World) in windows 7 using Eclipse Helios. After creating the project the console shows me the next message:
Internal Builder is used for build **
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\hello.o ..\src\hello.cpp
g++ -ohello.exe src\hello.o
c:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe:
warning: auto-importing has been activated without --enable-auto-import specified on the command line.
If I open the command line and execute: g++ hello.cpp -Wl,-enable-auto-import, I can generate the exe file and I can run it.
In eclipse after adding the argument "-Wl,--enable-auto-import" in the MinGW C++ Linker - Miscellaneous section, I didn't get any warning however, the console is always empty. Again, using the command line, if I move to the directory and execute the exe file generated by eclipse I get the expected result.
I know that Eclipse is compiling the cpp file properly but, why the eclipse console is not displaying the info? Is something missing in the configuration?
Sorry guys my stupid question.
I appreciate your help.
This looks like an environment variable(s) and/or register paths problem.