Debug argument based C program with gdb - c++

I have c++ program which I run by passing string with it.
g++ -o a main.cpp -lpthread
and execute it with ./a "Good nice"
But how I debug it with gdb?
main.cpp calling functions from other files which are included in it.
gdb ./a "Good nice"
takes "--" as files and says no such file!
I want to debug line by line!

Use the --args option of gdb:
gdb --args ./a "Good nice"
Also add the -g option to your compiler call, because otherwise gdb won't be able to connect your executable with your source code:
g++ -g -o a main.cpp -lpthread

Use gdb without argument
gdb ./a
Then in gdb, before running the program
set args "Good nice"
And you can see what arguments you set, use
show args
See here for detail.

gdb ./prog -> set args string -> run.

Anther choice is provide argument after run
$gdb ./a
run "Good nice"

Related

Debugger error: not in executable format: File format not recognized

I've written a program, saved it on the desktop under the name 'Swap.cpp' and when I run gdb (the first time), I get the error:
"/Users/myname/Desktop/Swap": not in executable format: File format
not recognized.
I have no idea what I'm doing wrong. Any help will be appreciated.
Sorry I should've given more information:
I am using Mac OS.
I've already compiled the program and have the Swap.o file that I can see on my desktop.And here are the commands that I enter while trying to run the debugger from bash:
$ clang++ -g Swap.cpp -o Swap
$ ./Swap
this runs Swap and then I try to access the debugger using:
$ gdb Swap
that then gives me the aforesaid message. I tried doing what Rakholiya Jenish suggested but to no avail.
To run gdb on windows:
path_to_gdb.exe program_to_debug
If the compilation was not proper, either compile it with your IDE (if you are using) or with g++ using cmd.exe as:
g++ -g Swap.cpp -o output -lm
I figured out how to use lldb instead of gdb. lldb works just fine. Here is what I did:
$ clang++ -g -o Swap Swap.cpp
$ lldb Swap
Thank you all for your help.

Command line arguments with CodeBlocks

So, I have been using CodeBlocks IDE for compiling my C and C++ programs. But I want to run the program from the command line. How do I go about this?
P.S I want to run it on Windows platform.
If you want to build a Code::Blocks workspace from the command line, use codeblocks.exe --build --target=W32_Release --no-batch-window-close MyApp.workspace where MyApp.workspace is your workspace. If you want to build a program from the command line without using Code::Blocks, use gcc -O2 program.c -o program where program.c is your program, or g++ -O2 program.cpp -o program for C++. You can get gcc & g++ from here.

Command not found, c++ application

I'm workin in a bingo application, in pairs, my partner sends me his source code, it compiled with no problems but when I tried to run it:
~/Escritorio/Bingo $ g++ -o Bingo main.cpp Bingo.cpp Bingo.h -std=c++11
~/Escritorio/Bingo $ ./Bingo --version
$: Command not found
it saids no command found /(orden no encontrada)
He's on windows while I'm on linux instead, I never had this problem before.
Your compilation command builds a Bingo executable. Your execution command tries to run a bingo program. Case is significant.
You really should compile with all warnings and debug info and don't need to pass any header file to the compiler (header which you should #include inside Bingo.cpp and main.cpp):
g++ -std=c++11 -Wall -Wextra -g main.cpp Bingo.cpp -o Bingo
Then, edit your code till you got no warnings, and repeat the compilation above.
Check with
ls -l Bingo Bingo.cpp main.cpp
that you've got that executable and that its timestamp is newer than the source code.
Then try to run it:
./Bingo --version
if you need to debug it, use gdb as
gdb ./Bingo

Tell ifort to show tool commands

Say I have two files file_1.f90 and file_2.f90 and they use some libraries. Could be any programming language. Then I compile and link in one step using
ifort -I/include_dir_loc -o my.o file_1.f90 file_2.f90 -L/Lib_dir_loc
Is there a way to tell the terminal or ifort or whoever takes over to tell me the individual steps it carries out. It could be that it goes
ifort -I/include_dir_loc -c -o file_1.f90
ifort -I/include_dir_loc -c -o file_2.f90
ifort -o my.o file_1.o file_2.o -L/Lib_dir_loc
What actually happens after I type the first command? Who carries out the compilation using what commands and who coordinates between the compiler and the linker?
for ifort:
-v will show the tool commands and execute them
-dryrun will show the tool commands but will not execute

how to add debug flags on compilation script execution:

I have a simple ./compile.make script that produces a bunch of object .o files. The contents are like this (first 5 lines printed):
compile.make:
gfortran -c -O3 active.f
gfortran -c -O3 alchemy.f
gfortran -c -O3 analysis.f
I run the script by doing ./compile.make. I'd like to compile everything with the -g flag so I can debug using (gdb) but I was wondering if there is a better way to add the "-g" flag without having to manually edit every line of my compile.make file.
*EDIT: I know that find/replace option is available and not much of a hassle at all. I was just curious as to whether the flags can be added upon execution of the script.
http://www.brunolinux.com/02-The_Terminal/Find_and%20Replace_with_Sed.html