How do debug header file implemetation (its .cpp) in c++ using GDB - c++

I have these 3 file in my program:
sample1.h (method in sample1.cpp are defined here)
sample1.cpp (all the actual implementations)
demo.cpp (I am using the methods in sampe1.cpp here, and have included sample1.h)
Now, I am using GDB to debug and I know the basic commands like "break lineno." or "break methodname". But, how do I debug the methods written in sample1.cpp?
I tried: break "sample1.cpp:mymethod" but it did not work.

try
break mymethod
As the function name in not ambiguous, it should work.
See. http://www.unknownroad.com/rtfm/gdbtut/gdbbreak.html#BCPPFUNC

If mymethod is a member of myclass:
break myclass::mymethod
There should be no need to specify the file.

Related

GCC - "has initializer but incomplete type" error when using macros for function attributes

I have a few header files with some simple glorified structs with just pure virtual methods defined within them. The code compiles fine on Windows with Visual Studio 2015, but GCC is getting stuck. First, the code:
Code Listing
namespace CustomUtils
{
interface API_ABSTRACT overriden
{
virtual int GetStatus() const = 0;
};
}
In an imported header file, interface is just defined as a stuct:
#define interface struct
And API_ABSTRACT is just a macro for nothing:
#define API_ABSTRACT
The interface typedef is part of inherited code I have no control over, and the API_ABSTRACT is in place so that I can define custom attributes in Windows and Linux to limit which API functions I export. While this builds in VS2015, on Linux, I get a build error:
error: variable 'API_ABSTRACT CustomUtils::overridden' has initializer but incomplete type
If I change the line:
interface API_ABSTRACT overriden
To what I presume it is being translated to:
struct overriden
The code will compile fine in Linux. I've tried compiling with gcc -E -dD to have the post-"pre-processed" source rendered to screen to see the typedef and #define substitutions, but it seems to only show the output for .cpp files, and not header (.h) files.
The Question
Why won't this code work when attempting to compile with GCC?
Thank you.
Edit #1
The output from gcc -E shows the offending line to be:
struct API_ABSTRACT overriden
So it seems the culprit is the API_ABSTRACT macro, which evaluates to nothing.
API_ABSTRACT was not defined anywhere (the corresponding file was not included). The way to check it is through tell-tale gcc -E:
struct API_ABSTRACT overriden
With -E, gcc would show preprocessed output, and having API_ABSTRACT in plain sight there means preprocessor knew nothing of it.
When C++ compiler have seen this construct (struct API_ABSTRACT overridden), it thought (according to grammar and if you grant compilers cognizance) that overriden is a variable of type API_ABSTRACT. Followed by braces, it turned the construct into initialization of said variable. However, type API_ABSTRACT was never defined, so compiler complained about initializing a variable of incomplete type.

C++ mangling name for use in Emscripten

I'm trying to compile a simple HelloWorld Prgramm from C++ to Javascript using emscripten.
It works fine when I include a main function which call's e.g. the multi function.
Here is my code (HelloWorld.cpp).
#include <stdio.h>
class HelloWorld {
public: void sayHello() {
printf("Hello World Klasse! %f", multi(7));
}
public: double multi(double x){
return x * x;
}
};
However if I don't include a main function the emcc compile always put's out
ERROR root: No functions to process. Make sure you prevented LLVM
from eliminating them as dead (use EXPORTED_FUNCTIONS if necessary,
see the FAQ)
I know about the 'EXPORTED_FUNCTIONS' option which tells what functions should be included into the compile .js file.
I tried various diffrent things:
Using the mangling name, as far as I understood this the name should be '_multi_d10HelloWorldd'. I also tried without classname and some other combinations.
emcc -s HelloWorld.cpp -s EXPORTED_FUNCTIONS='["_multi_d10HelloWorldd"]'
Using the modifier EXPORT_ALL
emcc -s HelloWorld.cpp -s EXPORT_ALL=1
Whatever I do the functions won't be included in the final js file.
From what I understand from the EMCC FAQ I need to use EXPORTED_FUNCTIONS so I can later on call the desired function e.g. 'sayHello' from JS unsing the same method name.
And this is exactly what I need to do later on.
Could someone please point me to a solution or any other possible option which I may have not thought of ?
Is the mangling name I thought of correct ?
Create an "extern c" block. Inside this block define the functions you want to expose to javascript. These functions should be prefixed with an underscore. Inside one of these functions you can instantiate your C++ class.
This is the same approach as one would take when writing a dynamic library, which has the advantage that you can reuse your library in a native program should you wish.

C++ command line debug argument

How can I change the value of a boolean macro when I run my program through the command line? For instance, suppose I have the following macro in my cpp file, call it MyCpp.cpp
#define DEBUG 1
How can I change this when I run my program? through the command line:
g++ -Wall -Wextra -o MyCpp MyCpp.cpp
I am pretty sure you specify some kind of command line option, does this ring any bells?
Also, I do NOT want to use argv[]
First, change your source code:
#ifndef DEBUG
# define DEBUG 1
#endif
Now you can say on the command line:
g++ -Wall -Wextra -o MyCpp MyCpp.cpp -DDEBUG=5
# ^^^^^^^^^
The command line argument -DFOO=bar has the same effect as putting #define FOO bar in your source code; you need the #ifndef guard to avoid an illegal redefinition of the macro.
Sometimes people use an auxiliary macro to prevent the definition of another macro:
#ifndef SUPPRESS_FOO
# define FOO
#endif
// ... later
#ifdef FOO
// ...
#endif
Now say -DSUPPRESS_FOO to not define FOO in the code...
How can I change the value of a boolean macro when I run my program through the command line?
As it stands, you can't. You are using a preprocessor symbol so the decision as to whether debug information should be printed is a compile time decision. You are going to have to change that compile-time DEBUG symbol to a run-time variable that you set by parsing the command line, via some configuration file read in at run time, or both.
Parsing the command line isn't that hard. There are plenty of low-level C-style tools to help you do that. Boost has a much more powerful C++ based scheme. The trick then is to change those compile-time debug decisions to run-time decisions. At the simplest, it's not that hard: Just replace that DEBUG preprocessor symbol with a global variable. You can get quite a bit more sophisticated than this of course. Eventually you'll have a configurable logging system. Boost has that, too.
Please note the following. If you have in your c/cpp file or one of your included header files:
#define DEBUG 1
then you cannot modify this definition using the command line of the compiler (makefile). There is simply no chance. The cpp file will simply overwrite the command line setting.

Declaration of void abort() throws different exceptions

I am trying to write some C++ code (using the C++ API) for Festival and am getting stuck while trying to compile. Here is how I invoke g++:
g++ -Wall -pedantic -I../ -I../speech_tools/include/ helloFestival.C -o h -L../festival/src/lib/libFestival.a -L../speech_tools/lib/libestools.a -L../speech_tools/lib/libestbase.a -L../speech_tools/lib/libeststrings.a |& tee festival.runLog
The error I get is:
In file included from ../speech_tools/include/EST.h:48,
from ../festival/src/include/festival.h:47,
from helloFestival.C:4:
../speech_tools/include/EST_String.h:50: error: declaration of ‘void abort()’ throws different exceptions
/usr/include/stdlib.h:513: error: from previous declaration ‘void abort() throw ()’
The offending line in EST_String.h would be:
extern "C" void abort(void);
The main() function I have used can be found here: festvox.org/docs/manual-1.4.3/festival_28.html#SEC133
The compilation and linking instructions given here are the ones I have used.
I have looked this problem on the net and some of the solutions suggest that it maybe because of backward compatibility, or calling an abort() from within a destructor etc. My questions are:
How do I get rid of this?
Why do I see this error?
You see this error because the abort() function in speech_tools conflicts with the standard-mandated abort() function. There's probably no really good, clean way to fix this. If you have written EST_String.h yourself, name the function differently.
If not, don't include stdlib.h and EST_String.h in the same file. Yes, that's limiting and bad, but you are in a crappy situation here.
It is a very basic c error. The two definition for abort are conflicting I would try to remove the line in EST_String.h and maybe add a #include <stdlib.h> and see if it compiles after that.
I don't suppose including the stdlib header is the problem. However, you might get better mileage from including either <cstdlib> or <stdlib.h> as the very first header in your translation units
Rationale: just in case the definition in <cstdlib> adds the no-throw declspec.
So I really suggest to ... just fiddle with that. If it doesn't work either way (make sure you don't have conflicting includes or stale precompiled headers), I suggest just removing the offending declarion in EST_String.h
This is still a problem today. As a workaround, i'm using this piece of code.
It's ugly and hacky, but gets it working:
extern "C" void abort_est() { abort(); }
#define abort abort_est
#include <festival.h>
#undef abort

gdb unable to set breakpoint

I am using g++ 4.1.2 and gdb 7.2
I am debugging code that uses Xerces, which I built using the same tools, though presumably without debugging.
GDB steps through my code just fine, but of course does NOT step through Xerces because it probably doesn't have debugging information, and definitely does not know where the source directory is. But all I want is to set a breakpoint when Xerces (a callback parser) calls a callback object.
Their base class is DefaultHandler
I have a class ContentHandlerBase : public DefaultHandler
Then leaf classes inherit from ContentHandlerBase. These leaf classes are inside namespace A, for example
in gdb I try to set a breakpoint.
b A::LeafContentHandler::LeafContentHandler
b A::LeafContentHandler::endElement
The first breakpoint works because the code is inline (defined in the header).
The second breakpoint does not work, meaning gdb claims that no such symbol exists, even though it obviously does. It is a virtual function defined in the Xerces library, if that makes a difference. Before I recompiled Xerces, it was built with g++3.4.6 and I could not find the symbol in gdb. Now, gdb finds the symbol (I can hit tab) but then it says it doesn't exist, should I wait for a library to load.
Can anyone tell me what I have to do to make it work? I'd prefer not to build all of xerces with debugging.
Note that in some cases, with the constructor in the .cpp file, it also worked for some reason, and then, because it was in the same file, I could set a subsequent breakpoint at linenumber, and that worked.
Try gdb 7.1 - it seems there are some problems in setting breakpoint by function name in gdb 7.2