How can I jump to function when doing C development in Emacs? - c

I am doing C development in Emacs. If I have a source file open with multiple functions and "the marker" is at a function call e.g. int n = get_number(arg); is there any way I can "jump to" the implementation of that function? e.g. to int get_number(int *arg) { ... }
I have done some Java development in Eclipse and is missing this functionallity, because I'm not that used to Emacs but I would like to learn.

You have to create a tag file.
Under Unix, you have the etags program that understands the syntax of C, C++, Java... and that create a tag file that can be used by Emacs.
This rather old page (2004) provides more information.
To jump to a function use M-. (that’s Meta-Period) and type the name of the function. If you
simply press enter Emacs will jump to the function declaration that matches the word under
the cursor.

There are several "tags" systems which allows that (there is one bundled with emacs, there is GNU global which isn't bundled with emacs but integrate well with it and has some advantages). Compared with Eclipse, you'll need to build the tags file.
Then there is semantic/EDE which is now bundled with emacs which should provide a solution without needing to build a database explicitly. I've not tried to use it recently. When I did, it has performance problem and I found the set up was painful. (Both possibly due to the fact that I'm working on a big -- several 10's millions lines -- and old -- some things date back to the mid 80's -- project without the possibility of reorganizing it).

I think semantic-mode should do you the same result. Although I haven't tried to jump to another file, but in one file it's very excellent. Go to a variable, issue keystroke C-c,j, it will jump to the definition of the variable. Go back to previous line using C-uC-space. To display reference to the symbol, use keystroke C-c,g
It really helps me.
I haven't tried it to jump to another file, because my current project is a modified Java program, where we are using preprocessor (a non standard java process). So I think that is where the problem lies.
Anyone success with semantic-mode???
thanks

I really like cscope for this, but etags probably works as well.

Related

How to navigate glibc source?

I'm trying to learn the glibc source, and I've found navigation to be quite formidable. I'm not referring to the code itself, but simply finding it: it seems to be a maze of macros and wrappers, such that just finding the actual code I want is quite tough.
Not only system dependent things, like setjmp, but even portable functions like fprintf: in each case, I struggle to find the true definition in C. It's easy to find the start, but it's usually an empty shell wrapping defines and macros.
This feels like a modern day equivalent of goto statements, with the spaghetti problem all over again.
How can I navigate glibc and find the actual implementation for lib functions?
Update
As an example, try looking up the definition of hidden_def in glibc. It's a macro taking you to hidden_def1, which is a macro taking you to hidden_def2, which is a macro taking you to hidden_asm, which is a macro taking you to hidden_asm1, at which point...
Moreover, each of these macros is defined in several different files, with other #defines controlling which definition is actually invoked.
This is not unusual: it seems to be de rigeur throughout the source code. How does anyone follow it? How do the GNU developers follow it?
How can I navigate glibc and find the actual implementation for lib functions?
Type the function in the search bar at https://code.woboq.org/userspace/glibc or at https://github.com/bminor/glibc . Navigate the results manually until you find the definition.
If you want to index the project locally, use cscope, ctags, GLOBAL tags or clangd to index the project and then use that tools specific interface to search for the definition.
As an example, try looking up the definition of hidden_def in glibc
Type hidden_def glibc into google. My first hit is woboq.org https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#550 .
I use firefox. I type ctrl+f and type hidden_def. Type Enter until I find # define hidden_def at https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#550 .
Then select __hidden_ver1 and type ctrl+c ctrl+f and ctrl+v and search for it. In the web browser. I type enter until I find https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#540 . __hidden_ver2 is just below on line 542.
For most cases all you need is a browser, google, coboq.org and github.org.
It's a macro taking you to hidden_def1
There are no such macros as you mentioned, at least at the version hosted at woboq.org.
How does anyone follow it?
While IDE is a powerful help, each project is unique and requires different settings, that take time to figure out. Mostly browsing the source code is grep (or faster alternatives, like ag, very useful for big projects like glibc) and going through the result list.
Not only system dependent things, like setjmp
Developers are (shoudl be :) sane people - in most cases a function named setjmp will be in a file named setjmp.c. or setjmp.S. Or in the same directory as setjmp.h. Or inside directory named stdlib or setjmp.
Type setjmp in github search bar. https://github.com/bminor/glibc/search?q=setjmp You see there are multiple definitions for each architecture powerpc s390 etc. But files are all named setjmp. Go back. Type "Go to file" on https://github.com/bminor/glibc . Search for a file named x86/setjmp. There are 3 implementations, the most standard one seems to be https://github.com/bminor/glibc/blob/master/sysdeps/x86_64/setjmp.S .
even portable functions like fprintf
As above, saerch for file named fprintf . You quickly find https://github.com/bminor/glibc/blob/master/stdio-common/fprintf.c .

vim autowrite functions in header file. C lang

I tried to use vim for c program editing. Is there a way to auto write function skeleton defined in header file?
situations like
"my_code.h"
int temp(int*);
and "my_code.c"
<<< here auto write >>> like
int temp(int*) { return }
int main()
{
}
I'm using c.vim plug-in. I tried to find it, but I couldn't make it.
There are code completion scripts, yes.. However, this is not something you generally want. It works for simple things like basic C functions, and fails horribly beyond that (i.e. templates etc in c++). You don't save any time by using such plugins, and mastering vim motion/yank/paste commands provide the same result in the same amount of time, and you become more familiar with a modal editor. Is it that hard to copy-paste the function prototype and add some braces {/}?
If you want something to help as a reminder to write function definitions to go with function prototypes, consider using the taglist plugin.
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.
Additionally, there are also template plugins that pre-initialize a new, empty file with a skeleton, often including a file header and copyright statement. Search vim.org; you'll find plenty.
I've have a :GOTOIMPL command in lh-cpp that generates an empty function definition from a function declaration.
However, you'll have to execute the command on each function declaration and go back to the header file. I've never took the time to batch the process from an header file and no implementation file -- as this is not a use case I have as there exist other solutions...
IOW... there exist projects that do the job from the command-line (and which you could call from vim then) (like for instance https://github.com/Davidbrcz/header-expander), or even other plugins (like protodef: http://www.vim.org/scripts/script.php?script_id=2624).

Sublime Text - Any way to see definition of a method from standard C library?

I don't know whether it's a stupid question. But here it is: In Sublime Text I haven't found any way to see the definition/documentation of a C/C++ method, no matter it comes from my own project or from system library. "Go To Definition" simply doesn't do anything. This is especially inconvenient when I'm reading some code which uses a bunch of std library stuffs which I don't necessarily recall at first sight, such as strloul, getopt, in which case I have to look them up in terminal or online, inducing overhead.
Thanks in advance.
You should install the SublimeText CTags package. Once installed and you have built your tags, you can put the cursor on the function of interest and (on OS X) type ctrl+t,t to jump to the method, wherever it might be located. Typing ctrl+t,b will bring you back to where you initiated the search from.
Example:
Cursor in the print_matrix function. Press ctrl+t,t
And it jumps to another file with the function definition:
ctrl+t,b takes you back again.

How does PC-Lint (by Gimpel) look across multiple modules?

I'm using Gimpel's PC-Lint v8.00 on a C codebase and am looking to understand how it traverses modules. The PC-lint manual only goes as far as to say that PC-Lint "looks across multiple modules". How does it do this? For example, does it start with one module and combine all related include files and source files into one large piece of code to analyze? How deep does it search in order to understand the program flow?
In a second related question, I have a use case where it is beneficial for me to lint one C module from the codebase at a time instead of providing every C module in a long list to PC-Lint. However, if I only provide one C module, will it automatically find the other C modules which it depends on, and use those to understand the program flow of the specified C module?
PC Lint creates some sort of run-time database when it parses your source files, noting things like global variables, extern-declarations, etc.
When it has processed all compilation units (C files with all included files, recursively), it does what a linker does to generate your output, but in stead of generating code, it reports on certain types of errors, for instance: An extern-declaration that has not been used, an unused prototype without implementation, unused global functions. These are issues not always reported by the linker, since the code generation is very well possible: The items have never been used anywhere!
The search depth can be influenced by the option -passes, which enables a far better value-tracking at the cost of execution time. Refer to seciton 10.2.2.4 in the PDF manual (for version 9.x).
To your second question, no, if you only provide one (or a few) source (C) file name(s) on your Lint command line, PC Lint will process only that file - and all include files used, recursively. You may want to use the option -u for "unit-checkout" to tell PC Lint that it only processes a part of a full project. Lint will then suppress certain kinds of warnings not useful for a partial project.
I think in principle you're asking about LINT OBJECT MODULES, see Chapter 9 of Lint Manual PDF.
Using say lint -u a1.c -oo procudes the a1.lob, when then again can be linked together using lint *.lob to produce the inter-module messages.
Also you asked a related, specific questions ( Any tips for speeding up static analysis tool PC-Lint? Any experiences using .LOB files?) but I'm not sure if I understand your concern with "How much would you say it affected linting time?", because I would say it depends. What is your current lint-time / speed? You posted some years ago now, how about running the job on a novel machine, new cpu then? KR

Error: "The procedure entry point ?JPEG_convert_to_rgb##YAPAEHPAEPAH1#Z could not be located in the dynamic link library libimage.dll"

Windows XP, Visual Studio 2005, C/C++, automation for Unigraphics NX using Open C
I'm trying to code an external program for NXOpen (i.e. a program with the NX library that runs on Windows, as opposed to an internal program that runs within NX). Right now I'm just testing to make sure the link structure is good, etc.
When I try to run the .exe that was generated, it does nothing for a few moments and then I get the following error: "The procedure entry point ?JPEG_convert_to_rgb##YAPAEHPAEPAH1#Z could not be located in the dynamic link library libimage.dll"
I have nothing to go on and Googling so far has been vastly unhelpful. The stuff on here seems to be file-specific for each case, and I'd never heard of this JPEG_convert_to_rgb before now. What can I do to fix this?
Additional info: I'm not sure if I broke something when trying to solve my last issue, or if this would have happened anyway.
It looks like you are compiling a C header file in C++ and suffering from the C++ compiler mangling your names. The DLL should export non-mangled names. Try wrapping the include of the header file in an extern "C" block.
Well, I called up GTAC. The issue turned out to be quite specific to the NX library and I'm not even fully certain what happened.
Basically, I had some environment variables that needed to be set: TC_DATA and TC_ROOT, though for some people it will be IMAN_DATA and IMAN_ROOT. These can be found if you open up NX through Teamcenter, go to Help->NX Log File, and do a ctrl-F to search for these terms. There you should find what the variables should be set to, and then set them as that. You should also make sure the UGII_BASE_DIR is set properly, and that your UGII_ROOT_DIR is at the beginning of your PATH variable. Also: call %tc_data%\tc_profilevars to set the other TC variables; call %iman_data%\iman_profilevars to set the other IMAN variables. There's also something else that I can't remember - this answer is not complete, it's just as complete as I can make it.
If this makes no sense to you, and you're using NX Open, you should probably call GTAC; if you can use an internal application instead of an external, you might be better off doing so.

Resources