how to switch from assembly code to c code in gdb - c

I am trying to debug my c program using
gcc -g -lm -lpthread -std=c99 -w terminalproject.c
and then
gdb a.out
but when I type
layout next, it shows me assembly code not c code.
How to switch to C code in layout?
EDIT: I am using Red Hat Linux 6, I tried to run it in Ubuntu, It is showing C code.
p.s my code has pthreads in it.

This command:
gcc -g -lm -lpthread -std=c99 -w terminalproject.c
is wrong. Use this instead:
gcc -g -std=c99 -pthread terminalproject.c -lm
You should never use -w (suppress all warnings) flag, unless you desire painful debugging sessions.
layout src says No source Available.
This likely means that you are using updated GCC, but ancient GDB. Try building recent GDB release from source -- it's usually not hard.

Related

How to run a C program which uses string.h in Ubuntu

My C program uses string.h.. Initially I was not able to compile it. But then I used
$ gcc filename.c -E
Then it complied but I am not able to run it with both
./a.out
./filename
The -E option to gcc invokes only the preprocessor. If you want to compile you need to do this:
gcc -g -Wall -Wextra -o filename filename.c
The -o option specifies the name of the executable to create, the -W options enable the common compiler warnings, and -g includes debugging symbols so you can use tools such as gdb to step through the code line by line.

How to compile a simple C code with "$gcc test.c -o demo" command?

On this beginner tutorial at TutorialsPoint, it says to save the code as test.c and compile it using "$gcc test.c -o demo" command at CMD.
But I don't have $gcc. What is it?
Let's split this into parts:
$ is a character indicating that the shell is ready to receive a command. It is not part of the command.
gcc is an executable executing the GNU C compiler from the GCC toolchain.
test.c -o demo are arguments supplied to gcc.
The GCC toolchain is only available natively for GNU systems such as Linux. Using MinGW or CygWin you can ape its functionality, though.
Notes:
A nice comment, which I second, to your question by #iharob:
Don't use gcc test.c -o demo specially if you are a beginner, use gcc -Wall -Wextra -Werror test.c -o demo.
The additional switches make the compiler point out more warnings.
GCC (GNU Compiler Collection. Upper case.) is a set of compilers, that can compile several languages.
gcc (lower case) is a command that compile the code you wrote, using the C compiler that GCC includes, into a working C program. Similar commands are g++ for C++ code, gcj for Java code, etc.
Note GCC is intended for Linux or other Unix-like systems (You can use it in Mac OS X with the help of xcode). If you are using Windows, consider [MinGW] (http://www.mingw.org/) or CygWin https://www.cygwin.com/.
As a beginner, if you still have trouble, consider use Dev-C++, an IDE (integrated development environment) that compiles C and C++. It does all the compiler things for you.

Can't name executable with specified optimization

I have to turn off optimizations while compiling c code I wrote while using the gcc compiler on a linux. I have found that I can compile the code but I can only get the code to compile without the executable name specified (default to a.out).
So this works:
gcc -O0 Problem04b.c
But my problem is that I have to submit this assignment and I can't submit an executable called a.out because my instructor needs to know which problem it is. I realize I can probably just run
cp a.out Problem04b
then
rm a.out
but I want to know if there is a way I can just compile the code directly into the executable Problem04b. I've tried to run the command like this:
gcc -O0 Problem04b Problem04b.c
but I'm having no luck.
Thanks for your help.
It's the -o flag:
gcc -O0 -o Problem04b Problem04b.c
To specify the output file, you need to use the -o <filename> option with gcc.
Note : Please mind the lower case here
In your case, it should be
gcc -O0 -o Problem04b Problem04b.c
For reference: From gcc manual
-o file
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
Actually, you also want to get warnings (that won't change the produced executable, but it is very helpful to you), so compile with
gcc -O0 -Wall -Wextra Problem04b.c -o Problem04b
The -Wall option asks for nearly all warnings, the -Wextra option asks for even more of them.
To run your thing (the ./ is useful because of possible PATH issues):
./Problem04b
Notice that -O0 is optional (since it is the default), you could remove it.
gcc -Wall -Wextra Problem04b.c -o Problem04b
If you want real optimization, e.g. for benchmarking, use e.g. -O1 or -O2 or -O3
You probably want to compile with debug information, then
gcc -g -Wall -Wextra Problem04b.c -o Problem04b
and of course you need to learn how to use the GDB debugger. So read some tutorial about that, then type
gdb ./Problem04b
You'll get a (gdb) prompt. Try help at that time.
You probably want to read the chapter about invoking GCC of the GCC documentation.

Segmentation fault during running for mkl's interface

I am running mkl_lab_solution.c which is an example for using MKL, I can compile it correctly, while I run it, I got Segmentation fault.My runtime is below:
OS is centos 6.3
gcc's version is 4.1.2
mkl is mkl_10.3.12.361
makefile is below
gcc -g -L/opt/intel/composer_xe_2011_sp1.12.361/mkl/lib/intel64 -lmkl_intel_ilp64 -lmkl_intel_lp64 -lmkl_intel_sp2dp -lmkl_intel_thread -lmkl_core -lpthread -L/opt/intel/composer_xe_2011_sp1.12.361/compiler/lib/intel64 -liomp5 -L/usr/lib64 -lstdc++ -I/opt/intel/composer_xe_2011_sp1.12.361/mkl/include -o test mkl-lab-solution.c
Since this works fine on my system, let me point you to possible errors.
First, you need to run . /path/to/intel/compilervars.sh intel64 such all environment variables are set, like MKLROOT.
Second, check on intel mkl link line advisor for the options on your system. So reading your compile command I guess: linux, gnu compiler, dynamic linked, 64 bit target architecture, 64 bit long pointer, multithreaded, intel omp library.
These settings give me:
linker options:
-L$(MKLROOT)/lib/intel64 -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm
compile options:
-DMKL_ILP64 -m64 -I$(MKLROOT)/include
For whatever reason the brackets around MKLROOT don't work on bash, so just remove them.
Next remember to put all compile options in front of linker options. The final command line should read like this:
gcc mkl-lab-solution.c -DMKL_ILP64 -m64 -I$MKLROOT/include -L$MKLROOT/lib/intel64 -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm
Since you get runtime errors, I suspect that you are linking the Intel MKL libraries with objects compiled for different interface layers.

Alchemy C Code compilation

I am using alchemy to compile the C code.
This is the way I am compiling
gcc oggvorbis.c -O3 -Wall -swc
oggvorbis.swc -lvorbis
I am getting an error
llvm-gcc: oggvorbis.swc: No such file
or directory.
But the command works fine when i don't use any shared library.
Your command line invocation should probably read (untested)
gcc oggvorbis.c -O3 -Wall -swc -o oggvorbis.swc -lvorbis

Resources