Best practice to get access to existing function in C? [closed] - c

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.
Say I'm working on a large project and I need access to a function, foo(), in a source file named example.c. The prototype for foo() is declared in example.c as well. example.c has no header specific to this file, such as example.h. What is the best practice to get access to example.c's foo() from the new source file that I'm working on?

If there is no header file which declares the function the function is probably not meant to be used as an interface. It is only meant for usage withing the translation unit where it is declared.
Check and confirm why does the design does not expose it as an interface.
Once you confirm #1, and see no problems about it being used through external TU's then add the function declaration in a header file. Include the header file in TU which used it to begin with and also include the header in TU which wants to use this function.

Provided that example.c contains no main() function, you can just do
#include <example.c>
for your code to access foo() inside the file :)

Related

Right header organization [closed]

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 have several C source files and headers. Some headers are for public use and some for internal use by library. I would like to separate them logically into folders so that headers for internal use do not mix with headers for public use.
Are there any commonly used patterns for that simple task?
here is a typical pattern, and it may depend on your compiler what is available to you... you have 2 types of include path during compilation:
User Header search path, denoted by double quoted #include "header.h" header names, these are typically just for the project that you are working on; as in you wouldn't address library headers this way typically.
System Header search path, denoted by angle braces #include <mylib/header.h>, this can also be used for your libraries...
lets imagine the following simple scenario...
Project A depends on Lib A.
in lib_a we have one c file and 2 headers...
lib_a.c, lib_a.h, and lib_a_internal.h
when you build lib_a.c you use the following includes:
#include "lib_a.h"
#include "lib_a_internal.h"
in your build script you will copy lib_a.h to include/lib_a/lib_a.h somewhere in your system headers path (maybe not in your real system headers, but a build specific one, depending on how atomic and sophisticated you want to be.)
You would also likely copy your lib_a.a (or .so or .dyld, or .dll) to lib/lib_a.a where lib is typically a peer of include above.
then when you build Project A you use the following:
#include <lib_a/lib_a.h>
Why not put them into separate directories and use the flags to fetch the appropriate ones during the build?
Leave the internal headers with the lib's sources and place the headers prototyping the lib's external API to /usr/include/<your lib's name without the prefix "lib">.
Alternativly put the external headers to /usr/lib/<your lib's name without the prefix "lib">/include and link /usr/include/<your lib's name without the prefix "lib"> to the latter.
This allows you to place additional files to you lib under /usr/lib/<your lib's name without the prefix "lib">/include. For example config settings in etc which then can be linked from /etc/<your lib's name without the prefix "lib">.

CCS C compiler , command line instructions [closed]

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.
Anybody here can please say what are the commands to compile and run C files from compile line.Like
GCC -o op_filename ipfilename.c in gcc compiler.
same way are there any specific commands of CCSC.
I work on regular c but am really new to Embedded C. So please help me.
The rather obvious suggestion perhaps is to read the manual!?
For dsPIC/PIC24.
For PICs with 12, 14, or 16 bit op-code interuction sets.
In both cases the section "Invoking the Command Line Compiler" directly addresses your question.
As you can see you need to be more specific about what compiler you are using, especially since "CCS" may also refer to Code Composer Studio rather than CCS Inc. You should also specify the compiler version you are using in order to get an accurate answer.
Best suggestion will be to read the manual. In that it'll be clearly explained.
In command line go to compiler location for eg Texas Instruments\ccsv4\tools\compiler\tms470\bin. Compiler in case of tms470 is cl470.exe .
For example, if you want to compile two files named symtab.c and file.c, assemble a third file named
seek.asm, and link to create an executable program called myprogram.out, you will enter:
cl470 symtab.c file.c seek.asm --run_linker --library=lnk.cmd
--library=rts16.lib --output_file=myprogram.out

C Library for Advanced Trigonometry? [closed]

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.

Source code browsing, comprehension and reading tools [closed]

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.

Generating call graph for C code [closed]

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.

Resources