How to extract Defined Function Names from C file

This is a discussion on How to extract Defined Function Names from C file within the C Programming forums, part of the General Programming Boards category; Is there any tool or API that could extract the defined function names from the particular C file. e.g Test.c ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    How to extract Defined Function Names from C file

    Is there any tool or API that could extract the defined function names from the particular C file.

    e.g

    Test.c file defined three functions

    Code:
    int firstFunction(int)
    {
    
    /*some statements*/
    
    }
    
    int secondFunction(int)
    {
    
    /*some statements*/
    
    }
    
    
    int thirdFunction(int)
    {
    
    /*some statements*/
    
    }
    
    /*end of file*/

    what I want to get the function names: firstFunction, secondFunction and thirdFunction.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depending on exactly what you want to do, there are probably several different tools that can be used to do this.

    When I say "depending on what you want to do", I mean:
    - You may want a list to see which function is defined in which .C file - in which case you probably are looking for a "Cross Reference" tool.
    - You may want to list the functions actually produced by the compiler when you compile the code, in which case there are various tools to produce a list of "function names in this object file".

    There are several other options too, so if neither of the above is suitable to what you "actually" want, then please explain more.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    If I understand your question correctly, then it'd be no problem to write a program in C to do just that.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    I wanted to develop a simple tool, which will have a browse button and list box. Through Browse button I will select a C source file and then list box will show a function names defined in this file.

    I donít want to use object files for this purpose.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    If I understand your question correctly, then it'd be no problem to write a program in C to do just that.
    No, but it gets "interesting" when you have to figure out what is a function and what isn't. Consider:
    Code:
    int (*f)(void);   /* Not a function */
    struct blah func
    (int x)   // This is a function
    {
    ....
    }
    
    int func1(int (*f)()) { ... } // Yes, this is a function 
    int (*(func2(int x)(void)) { ... } // And a function returning a pointer to function. 
    
    struct blah func(int x);  /* May be a function, depending on whether you include prototypes or not */
    For simple, basic functions, then it's not too bad.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by matsp View Post
    No, but it gets "interesting" when you have to figure out what is a function and what isn't. Consider:
    Code:
    int (*f)(void);   /* Not a function */
    struct blah func
    (int x)   // This is a function
    {
    ....
    }
    
    int func1(int (*f)()) { ... } // Yes, this is a function 
    int (*(func2(int x)(void)) { ... } // And a function returning a pointer to function. 
    
    struct blah func(int x);  /* May be a function, depending on whether you include prototypes or not */
    For simple, basic functions, then it's not too bad.

    --
    Mats
    Yes, I thought about that. But instead of just analyzing the source file for the names, once could first parse it so it has a specific layout(Eg. no function parameters on separate lines and the like) and then analyze it.

    But even so, yes, it can get a bit complex.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    But I am talking here only about defined function names, not about prototypes or declarations.

    May be any third party library provide this facility?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by imranhabib View Post
    But I am talking here only about defined function names, not about prototypes or declarations.

    May be any third party library provide this facility?
    I'm not aware of a tool that does ONLY what you want to do - there are tools which do this as part of their processing, but they also do a lot of "other" things [e.g. building a cross-reference of all symbols declared in a C/C++ project, and allowing you to jump from one file to another graphically by clicking on things - e.g. "I see func1 in this file, where is func1 defined", or "I have this func1 definition, show me where it is being used"].

    What do you actually want to do with the information "later on"?

    This determines how "much effort" you need to put into it.

    Also, if the files are "nicely formatted" then it becomes much easier to do than if you have to allow for ALL formats that the C/C++ standard allows you to use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Yes, you are right there are so many tools and I myself searching this kind of tool or API, but mostly I get the bigger tool.

    One tool which I found close to this was Flex lexical analyzer , but it is hard to learn it.

    Other one is library

    http://ctool.sourceforge.net/

    But again it has very less documentation.

    May be some one which has used this tools before or have experience in this area, could tell us better about this.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    No, but it gets "interesting" when you have to figure out what is a function and what isn't. Consider:
    Code:
    struct blah func(int x);  /* May be a function, depending on whether you include prototypes or not */
    How could that not be a function?

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    That is a function declaration, not a function definition.

    As my question was regarding function definition so robwhit is pointing out that is not function definition.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    oh right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21