1

I am working for a very complex application deployed on Unix(HP-UX) having C/C++ source file count>10-15k. I wish to know if i can search exact file's name having defintion of a specific user defined function. there are lot of such functions called from several places and it takes time to search using grep.

I found discussions on Cscope,ack in other threads but these are not available on my client machines. so is there any other to achieve it?any suggestion would be great help!

Also how can i tag a function in vi editor which helps to move in another file having its definition? i know it can be achieved using ctags(Ctrl + [). is there any other way possible ?

Please post if relevant thread exists. Thank you.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 2
    Use `nm` to list all symbols from object files, remove the undefined symbols with `sed` and then use grep on the collected output to find which object file defines what symbol. – fuz Sep 27 '15 at 13:35
  • Also, is this C or C++ source? There might be tools that work for one but not the other. – fuz Sep 27 '15 at 13:40
  • few modules are in C but new ones are in C++. – honeytechiebee Sep 27 '15 at 13:42
  • How much time does it take to search using grep? I would expect less than a second, no? – John Zwinck Sep 27 '15 at 13:51
  • i am not concerned about time though, but it will also list all files calling a function and since code format varies i am looking for a constructive solution. – honeytechiebee Sep 27 '15 at 14:10
  • The focus of the StackOverflow site is to help people with coding issues. Your's is more of a meta-coding issue, which for some people is on topic. Maybe they will answer. I've voted to close because you haven't included any code. We may be able to help you if you posted a sample of your problem. Maybe you only need a `sed` or `sort -u` or something else to pipe your `grep` output into, but without seeing your work, we can't help you. Good luck. – shellter Sep 27 '15 at 15:53

1 Answers1

1

If you cannot install new tools on your development machine, there are two general approaches you can use.

Text Search

You already mentioned using grep but said it takes time to sift through the results, because call sites appear along with definitions. To avoid this, you can tweak your grep pattern. First, try grepping only header files:

find PATH -name '*.h' | xargs grep FUNC

If your project follows common practice, whenever you find a function declared in a header file, it will be defined either in the same file or in a correspondingly-named code file.

Alternatively, try a special pattern to look for definitions only:

grep 'FUNC(.*) *$'

That will match only those places where the right-parenthesis is at the end of the line (modulo whitespace). This will filter out most call sites and declarations, though it isn't perfect.

Object Code Search

In this approach, you first generate a list of symbols defined in each object file. This assumes you have or can generate these intermediate build artifacts, and again that your project follows common practice. You would use nm as detailed here: How I can find function in shared object files using objdump and bash functions in linux?

You can make aliases for the above commands in your .bashrc to make them easier to use.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436