I want to add some code before every function call to do some checking. The only way I know is:
#define SOME_CODE printf("doing something...");
class testObject
{
void function1()
{
SOME_CODE
...
}
void function2()
{
SOME_CODE
...
}
}
Is there a cleaner way to achieve this? I'm looking for a method so I don't have add "SOME_CODE" to every function manually.
The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. A program usually stops executing at the end of main, although it can terminate at other points in the program for a variety of reasons.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C++ standard library provides numerous built-in functions that your program can call.
An entry point is a location in code where a transfer of program control (execution) occurs. The main function ( main() ) is the entry point to a C/C++ program and is called when the application starts executing. Calls to other functions, for example from the main function, provide entry points to function code.
For gcc there's a similar solution to the MSVC one someone else posted as an answer:
#include <iostream>
int depth=-1;
extern "C" {
void __cyg_profile_func_enter (void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_exit (void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_enter (void *func, void *caller)
{
depth++;
}
void __cyg_profile_func_exit (void *func, void *caller)
{
depth--;
}
}
class Foo {
public:
void bar() {
std::cout << "bar: " << depth << std::endl;
}
};
int main() {
Foo f;
f.bar();
return 0;
}
Compile with g++ -Wall -Wextra -finstrument-functions
. Be careful not to call an instrumented function from within the instrument hooks though! (See the man page for ways of excluding things)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With