How to use gdb on c++ header files? - c++

I tried to search this question online, but it seems that I can't find a good solution for my problem. Well, I'm trying to use gdb to debug my c++ program. And the program is made up of a simple main.cpp and a model.h. And the compiling command is
g++ -Wall -g -c main.cpp
g++ -Wall -g main.o -o OUTPUT
As almost all the algorithm is stored in model.h, I need to debug that header file rather than the cpp file. However, whenever I tried to place a break point on the header like
tbreak model.h:163
gdb always give me a message that"No source file named TNFmodel.h".
In another question breakpoints in GDB, I saw a solution by adding the folder that containing the header into the library by "dir". But my header file is already in source folder, and after trying
dir ./
The problem maintains.
So anybody know what's wrong? How to use gdb to debug a header file?

As suggested by https://stackoverflow.com/users/760746/nobody, one way to make sure the header to be in the sources is to veryfy it by checking
info sources
After ensuring the header itself be in the sources(in my case, the problem is that the case of a letter in the header name was mixed up, and somehow it went through the compiling on my mac book), inserting breakpoint in lines of a header file works just fine.

Try to use break with your class/method name like this:
break class::method

What I've found is that is that the file names are sometimes shortened. Using info sources I was able to find the shortened name that GCC used. When I set the breakpoint using the shortened file name, GDB correctly set the breakpoint.
For example the file CommonLibrary\headers\Endian.h was changed to COM~2\headers\Endian.h
This on Windows 10, running mingw-64.

Related

How to add environment variable into Makefile?

Since I am using IDE instead of GNU writing codes, I am having a hard time reading those documents about Makefile, so far I couldn't find any documents states clearly where to put $BOOST_HOME. (I am using Boost Library for my project so it needs to compile with it)
so far I am compiling the program with this command:
g++ -Wall -std=c++11 -o ladder Source1.cpp -I/home/s/suw/boost_1_65_1
since this will be compile in other's computer so the path of boost library will be different thus I need to put $BOOST_HOME into Makefile, any help would be appreciated.
EDIT:
So I think I need to set $BOOST_HOME a path and put it into makefile, but path is varies by different user, how to let somebody run my program by just using a simple:
g++ *.cpp -o foo
???

cygwin: no header file found

The following problem: Cygwin or Visual Studio give the error.
CGAL/Splitters.h: No such file or directory
This error appears for every header! The header is implemented by the code
#include <CGAL/Splitters.h>
The error disappears, if I change the code, such that I write the whole path:
#include <c:/path1/path2/CGAL/Splitters.h>
But this is no solution which satisfies me, because I would have to change hundreds of such code fractions.
I think it should be a problem of Visual studio or cygwin. In cygwin I wrote the command:
$ g++ -std=c++11 example.cpp -o example
What is the reason for the error? How can I fix it?
Please give easy understandable instructions, since I am a beginner in C++.
The compiler does not know where to look for the header file
referred to by #include <CGAL/Splitters.h> unless you tell it,
because that header file is not located in any of the compiler's
default search directories for header files.
You tell the compiler where to look by passing it an -I option:
$ g++ -I/path/to/cgal/headers -std=c++11 example.cpp -o example
where that header file will be:
/path/to/cgal/headers/CGAL/Splitters.h
Further reading: An Introduction to GCC - for the GNU compilers gcc and g++
Later
So for every of the header files I have to write the - I option?
No. -I/path/to/cgal/headers/ by itself will of course tell the compiler
where to find every CGAL header file used in your program.
You can check include path in Visual Studio :
Tools > Options > Projects and Solutions > VC++ Directories > Include files
So, check default header file path.
I hope this can help you.

c++ compiler (g++) strange behaviour (no such file or directory)

I am working in eclipse with MinGw (g++) compiler.
So my problem is when I import .h file from library I have downloaded and I try to build(compile) my project, error is "no such file or directory" for that .h file you can see on picture but still the class from that header file is recognised in the code!
Another strange thing is if I make intentional error in that .h file #import is succesfull and the error from that .h file is shown, that means it trys to compile that .h file.
So it does not know where the file is but it still compiles it ??? what???
cmd line:
g++ -Ic:D:\Documents\cpp_testing\bignum_testing\lib Main.cpp
error:
Main.cpp:10:22: fatal error: Cbignums.h: No such file or directory
#include "Cbignums.h"
^
compilation terminated.
I hope somone will know how to fix this and that it will help other people!
Picture without error in .h file:
Picture with intentional error in .h file!
I suspect that you should
#include "lib/Cbignums.h"
in bignum_testing.cpp if that is the path of the Cbignums.h - relative to the source file.
The IDE recognizes the type because you have the header in your project but the red lines indicate that the include will fail.
EOF is mostly a C thing from <cstdio> (or <stdio.h> form the C standard library, not the C++ one) which you probably forgot to include (e.g. by commenting its //#include <cstdio>)
Try compiling with appropriate -I and -H preprocessor options to g++ (of course, take time to read the documentation to find what they are doing).
BTW, you could ask for the preprocessed form with g++ -C -E Cbignums.cpp > Cbignums.ii then look, with some editor or pager, into the generated Cbignums.ii file.
I strongly recommend you to read a good book about Programming using C++ and to read the GCC documentation. In general, read the documentation of everything you are using for development (tools, e.g. compilers, and libraries)
PS. Every free software C++ compiler I know (GCC & Clang/LLVM...) are command line tools, so run them in a terminal, perhaps thru GNU make. Notice that Eclipse is not a compiler (it is an editor, self-glorified as an IDE), and you probably are not using it cleverly. Don't forget to pass -Wall -g to g++

Using gcov to test a c++ program

I am using gcov for the first time to analyze my program (C++)
The program consists of three classes and I have built the project using Code::Blocks.
When I am invoking the program using the following command:
C:\Users\XXX\Documents\Test\TreeObjModel\src>gcc
-fprofile-arcs -ftest-coverage Tree.cpp
I receive the following error:
Tree.cpp:1:18: fatal error: Tree.h: No such file or directory
compilation terminated
While the cpp files are in the directory "C:\Users\XXX\Documents\Test\TreeObjModel\src\" , the header files are in directory "C:\Users\XXX\Documents\Test\TreeObjModel\include\"
Do we need to have both the code and header files in the same directory?
Thanks in advance.
You should use the -I flag to specify where your header files are.
Judging from your example, you should add -I../include
You have at least two options to instruct the compiler where to find the header files (includes).
-Ipath_to_includes as parameter for gcc compiler. E.g. -I../include
When including in your program, specify the directory. E.g. #include "../include/foo.h"
My strategy would be to just compile my project successfully and only then try to use some other stuff, like flags for code coverage. I say this because your error does not have anything to do with gcov, and trying to instrument your program to get code coverage before your program even compiles, makes things more complicated for you. One step at a time ;)

Vim code completion doesn't work after including a standard header

I have to develop my project in text-mode debian linux. I'm using Vim and I installed the clang_completion plugin on it. I made .clang_completion file in root of my project :
-I.
-I/usr/include
-I/usr/include/c++/4.6
When I write a program like below, the completion works fine.
//#include <stdio.h>
int main()
{
struct A
{
int x, y;
};
A a;
a. // After putting dot, the suggestion popup appears
return 0;
}
However, after removing the comment of first line, it doesn't work! How can I overcome this issue?
I found the easiest way to get clang_complete to work is to use the provided cc_args.py file.
when compiling a project use clang_complete/bin/cc_args.py instead of gcc/g++
This will generate the correct .clang_complete file with all libraries and dependencies.
Provided the clang_complete source directory in your home folder.
Example Makefile:
CXX=$(HOME)/clang_complete/bin/cc_args.py g++
all:
$(CXX) main.cpp
I've successfully used the clang_complete plugin in the past (now I just use cscope and ctags, which I consider enough).
Including external headers worked fine in my configuration, but, as the clang complete plugin page specifies, the file in which to put include paths (or any other flag you may want to pass to the clang compiler), must be named .clang_complete and not .clang_completion.
Also, I used to put the options on a single line, just as I was going to pass the plain content of the .clang_complete file as a command line option (don't know if separating lines with \ will work).
Hope this helps.