As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I am primarily a C and C++ programmer and i often need to quickly comprehend the structure of very large code bases (gcc, linux kernel). I wonder if there are any tools to help in this regard. I am particularly interested in call graphs, data structure references across the project, include dependency graphs, quick symbol location, etc. I known about ctags and cscope but i am looking for something with more visualization like a call graph that allows to quickly locate definition of a function, root the graph at a particular call, inverting it (i.e. locating all calls to a given function), etc.
If you want to build call graphs, you could roll your own with GCC's -finstrument-functions.
Basically, when you compile a program with that option enabled, GCC calls the following functions whenever the target program enters or exits a function:
void __cyg_profile_func_enter (void *this_fn,
void *call_site);
void __cyg_profile_func_exit (void *this_fn,
void *call_site);
What you need to do is define these functions, and write in your logic to produce the call graph there.
This extremely thorough tutorial explains how you could produce a call graph using -finstrument-functions and GraphViz. All the tools involved are FOSS and gratis.
Of course:
The graphs GraphViz produces are stand-alone, and not part of an IDE.
I'm not really sure if producing a call-graph of Linux (the kernel) is possible in this way.
Please try and use SourceInsight. It is quite helpful with browsing code and understanding it. It provides most of the features requested by you.
You could try cflow. It gives you a graf of the calls of functions inside. It is not very flexible though.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I am looking for a library that has things like:
Find the intersection points between 2 circles
Find the tangents between a point and a circle
Find the tangents between 2 circles
Stuff like that. I have done the research and found the answers, but there must be a library for this stuff.
C89 contains a header file called <math.h> which contains library functions to compute Trigonometric functions (sin(), cos(), tan(), asin(), acos(), atan() and atan2()) and Hyperbolic
functions (sinh(), cosh(), tanh(), asinh(), acosh() and atanh()) which might be of help to you. But as far as I know there are no standard C functions to cater to your needs directly. Either you have to create your own functions with the help of the above mentioned functions or depend on a language like Python, MATLAB or Mathematica in which most of the mathematical functions are already implemented in the standard library.
C99 also adds functions in the header file <complex.h> to perform trigonometric functions on complex numbers (csin(), ccos(), ctan(), etc.) by introducing a new keyword _Complex.
The GNU Scientific Library (GSL) is a numerical library for C which contains functions to perform various mathematical operations.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
After half an hour of research on the Internet, I couldn't find any reasoned discussion of the advantages of function prototyping.
I manage in Java/Android, and am beginning a C course. Prototyping looks cumbersome compared to my previous experience, and I would like to know the reason(s) why it still exists in 2013.
I understand that life was more difficult for Ritchie and pals; however, a compiler could be written today that would generate a list of functions in a first pass, then do its usual thing using that list of functions as a current compiler would use a header file.
It probably can't persist either only because of backwards compatibility. It would be feasible to create a compiler that could switch between current operation mode, and the hypothetical new mode I just described, depending on the code it is shown.
If prototyping persists, it must therefore have an interest for the programmer, not for the compiler programmer. Am I right or wrong - and where can I find a reasoned discussion of the advantages of function prototyping vs. no prototyping?
You're forgetting that in C you can call a function whose source you don't have.
C supports binary distribution of code, which is quite common for (commercial) libraries.
You get a header that declares the API (all functions and data types) and the code in a .lib (or whatever your platform uses) file. This is typically the case for all of C's standard library; you don't always get the source to the compiler vendor's library but you must still be able to call the functions, of course.
For that to work, the C compiler must have the declarations when processing your code, so it can generate the proper arguments for the call, and of course deal with any return value correctly.
It's not enough to just rely on your source, since if you do
GRAPHICSAPI_SetColorRGB(1, 1, 1);
but the actual declaration is:
void GRAPHICSAPI_SetColorRGB(double red, double green, double blue);
the compiler cannot magically convert your int arguments to double if it doesn't have the prototype. Of course, having the prototype makes it possible to error-check that the call makes sense, which is very valuable.
Interesting idea about having the compiler have a first look over all source files to take notice of all functions prototypes.
However
libraries (object code) need to have their declarations somewhere, this is why the includes exist
Also I find convenient to be able to grep the includes as "free text", like
grep alloc /usr/includes/*
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
As a newb, like myself, to have great difficulty with searching the header files such as stdio.h for a function like getchar(). Somehow I picked up some information somewhere that I should not be afraid when looking in header files to "see how things work." (C++ Primer Plus, Stephen Prata)
I am very inexperienced with header files to say the least, and programmig in general.
In my attempt to find getchar() I found that stdio.h simply branches to more and more headers, and locating getchar() became increasinly complicated and time consuming, and I never found it. Clearly I am going about this all wrong, my intention was merely to find some source code for functions I am using.
My question therefore is: Where can I find source code to truly 'understand' what the standard functions are 'really' doing?
If you're looking for the declarations for variable order or other usage notes, just use an online reference or man.
If you're looking for the actual code, look into an implementation of the C standard library, like GNU's libc.
It's worth noting though, that implementations are not simple, and their graph of dependencies goes far and wide. They also tend to interact with the machine on a lower level than most of us are used to.
Consider libc's implementation of getchar:
int
getchar ()
{
int result;
_IO_acquire_lock (_IO_stdin);
result = _IO_getc_unlocked (_IO_stdin);
_IO_release_lock (_IO_stdin);
return result;
}
Probably not what you were expecting :).
(Note: No idea how good of reference that is for C -- it's just the one I typically use for C++.)
you shouldn't search the header files, you should use the man pages or MSDN help.
The C Standard does not mandate the standard headers files (like stdio.h) to physically exist. They can be just built-in.
If you don't know the parameters or the return value of a function, read the C Standard or the man pages.
In C header files are used (included) to pre declare functions. The functions that are predeclared may either be your own, where the implementation is in another (or the same for that matter) .c file, or an already compiled library. The stdio.h is an example of the last.
You should not have to look in the header file to find the function declaration. try using google typing 'man '
cheers
That is kinda like modifying the executable file explorer.exe to perform a simple action in Windows. Those are the base files, leave them be and write your functionality directly in your files. Also, if searching and altering files is an issue, make sure that you are using an IDE and not trying to do things by hand through notepad or another program like that.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm writing a tool and I need to generate the callgraph for some C projects.
I was able to generate the callgraph of one file using clang, but I was not able to find out how to generate the call graph across the whole project which contains tens of header and source files.
Any tool that can generate the callgraph to a file that can be parsed will be fine. A usable library will be better.
Also worth mentioning, the excellent GNU cflow:
GNU cflow analyzes a collection of C source files and prints a graph, charting control flow within the program.
GNU cflow is able to produce both direct and inverted flowgraphs for C sources. Optionally a cross-reference listing can be generated. Two output formats are implemented: POSIX and GNU (extended).
Input files can optionally be preprocessed before analyzing.
Edit
As for the library request. You might like to "tweak" output.c and instead of printing do something else with the data. The internal flow is organised into output handlers, so I think writing your own handler could already do the trick. It's not out of the box though.
Turning my comment into an answer.
You can have a look at the assembly output and process that using a script. Assuming gcc on linux, you pass the -S flag to gcc and process the result with something like this:
perl -ne '/^([^. \t#].*):/ and $f=$1;/call\s+([^*]\S*)/ and print "$f -> $1\n";' *.S
This will give you a line for each static call, containing the calling and the called function. You could add a bit of boilerplate around that and feed the result to dot, or whatever you want to do with it.
You can omit the “must not start with a star” part of the regular expression to obtain some indication of indirect calls as well. You still won't be able to tell what functions will get called at that point, but at least you'll know that there is something more to know.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
After doing some work with Ruby, Rails, and RSpec last summer and I learned to TATFT. Now I can't write code without writing tests first.
I'm taking a programming course in C next year, and I would like to learn to write C test-driven. Is it a good idea (or even possible) to do TDD with C? If so, are there any good testing frameworks compatible with C?
Is it a good idea (or even possible) to do TDD with C?
Yes, it obviously is a good idea, as with other languages. However, due to the procedural nature of the language, it comes with its some more difficulties.
Static functions quickly get in the way. This can be solved by including the source file under test, or defining a STATIC macro that only means static when compiling the code for production - not unit test
#if defined(UNIT_TEST)
#define STATIC
#else
#define STATIC static
#endif
There is no isolation: there is only one global context. With an OO language you can just instantiate one object (or a cluster of collaborating objects) to test it (them), you can also use mock objects.
With C, you can, however, override functions just by re-defining them in your unit tests. This works fine on Unix-like systems where the linker invokes the first function he is finding - I'm not sure on Windows.
If so, are there any good testing
frameworks compatible with C?
You can start small with minunit. The learning curve is flat as it only is four macros long.
EDIT: There are two lists of UT frameworks for the C language, that were mentioned in other answers and I didn't repeat : one on Wikepedia and another one on xprogramming.com.
We use "check" from http://check.sourceforge.net/, it provides basic functionality for testsuites and tests (similiar to junit), but is fairly lightweight. On feature I like is that it handles if your test dumps code and considers that a failure.
Also note "check" is a "C" based framework rather than a "C++" one.
I just discovered CSpec, which does BDD in C. Doesn't look very mature, but it reminds me of RSpec.
There are a number of unit testing harnesses for C. Wikipedia has a much better list than I could assemble here.
If you are actually using a C++ compiler, but using it in 'C' mode by compiling .c files, then, also, any of the C++ unit test frameworks will work OK.
Take a look at the original list of xUnit frameworks at http://www.xprogramming.com/software.htm
This similar question also has a lot of answers "Unit Testing C Code"
I used RCUNIT, it is mature and has everything I need. I have also used ipl canata which is great but is very expensive so that is probability not what you want.
You certainly can do unit testing in C (I do). The framework I use (for the Windows platform) is CunitWin32
Here is the list of unit test frameworks for c:
http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C
enjoy it!
So a proper C programmer will tell you that because C is statically typed it catches all bugs you might have and therefore you don't need a unit test framework.
They are full of shit, but that's the argument for statically type languages like C.
I think you should probably take the approach that Adobe did with Photoshop. Write a series of core libraries in C, and then all the glue and real logic of the application should be in a higher level language. Photoshop is mostly written in Lua, but many languages work for this.