Opening a C library header file in Vim - c

Say I'm including a header file in vim while writing a C program, e.g
#include <time.h>
I have no idea what structures or functions are declared there. How can I jump to that file and open it in Vim? Is there any way to do that?

On Unix-like systems, Vim is already wired for C by default so you only have to move the cursor to <time.h> and press gf, to open the header in the same window or <C-w>f to open it in a new window.
See this answer to a similar question that was asked a few hours ago for additional pointers.

You can use:
find . -name "time.h"
to find the file and then you can copy the path and open it with vim.
But this this won't really help, because its not as structured as you might think it is. To learn more about time.h I would google it. There are several websites explaining structs, functions etc.
For example: https://www.tutorialspoint.com/c_standard_library/time_h.htm

Related

XCode is resolving the wrong time.h file

I'm just getting my feet wet in C with some work in GStreamer, but seem to already be stuck.
I'm compiling the project in X Code using GNU99. The <time.h> header file that is part of the GStreamer code has no reference to time_t, which is used by some of the files.
Therefore I'm seeing:
/Library/Frameworks/GStreamer.framework/Versions/1.0/Headers/glib/gbookmarkfile.h:171:11:
Unknown type name 'time_t'; did you mean 'size_t'?
Do some C versions have varying header files for <time.h> that have done away with the time_t type? If so, is there a workaround?
Edit:
Looks like it has something to do with how XCode is searching for the <time.h> header file.
It should grab it from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/time.h, but it's instead using one from FFmpeg
Under header search paths, I see this:
/Library/Frameworks/GStreamer.framework/Versions/1.0/Headers
This setting is directly from the tutorial download
My workaround solution was to add
#include </Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/time.h>
to the FFMPEG time.h file.
Your path to the proper time header file may vary.

C Include custom header file in Geany on Windows 10 compiling with gcc

I'm having an incredibally hard time finding answers to this for Windows. As if the majority of people use Linux...
Anyways, I need a custom CSV parsing library for C. I found one and downloaded the header file. I tried adding #include <csvparser.h> at the top of my c program but of course, it says file not found. I placed the downloaded file in the same directory as the program.
I think i need to be able to specify an absolute path in the include or place the file csvparser.h in the include directory, but I know how to do neither of these things. What is the default include directory in Windows? I use gcc as my compiler. How can i specify an absolute path in the include statement, on windows? i can find no answer to this.
Thanks!
EDIT
Thank you for the quick reply, I seem to have included the file correctly, but now I'mhaving problems using it.
I found the program at https://sourceforge.net/p/cccsvparser/wiki/Home/
I placed it in the source directory and tried using it, bbut when I try the usage example I'm getting an error. This is my code:
#include <stdio.h>
#include <string.h>
#include "csvparser.h"
#define MAXCHAR 10000
int main() {
// int i = 0;
// file, delimiter, first_line_is_header?
CsvParser *csvparser = CsvParser_new("../MagicProg/Files/MagicProg_csv_ikoria.csv", "|", 1);
return 0;
}
When I try executing this, geany gives me the error:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\ccsiwJPq.o:hello.c:(.text+0x22): undefined reference to `CsvParser_new'
What am I doing wrong? thanks again
If you're including something that's in your source directory you need to use a different style:
#include "csvparser.h"
The angle-brackets form is exclusively for things found in your include path, not in your source directory. That's reserved for things like OS and compiler headers, as well as system-installed libraries.
I made the huge newb error of not including the src files along with the header file. I blame myself. thanks everyone for help

How to include a folder of libraries in C?

I'm trying to include a folder that contains a combination of around 60 .h and .hpp files. This folder contains libraries for programming robots with a Wallaby (a mini-computer-like device) for Botball competition. include is located in the same place as main.c (inside code). Up until now, this is what my header for including libraries looks like:
#include "../code/include/accel.h"
Just like accel.h, I have 60 other .h and .hpp files inside include. So, coming to my question, do I need to type out all the 60 header lines? or is there a way to include the include folder.
I'm using Clion for this project, if I can't include the folder itself, does anyone know of a shortcut in Clion to include all the files in include.
I was also thinking of using some sort of placeholder for the folder name and only specify the file type. So, for example: #include "../code/include/(generic placeholder name).h". I have no clue if something like this exists.
I would also request you to keep in mind that I'm a beginner to programming, so please keep your answers simple.
This is just for some extra info:
The Wallaby is a mini computer to which you would connect your sensors, motors, servos and cameras in order to control a robot for the Botball competition. Usually, one can connect to the Wallaby either via Wifi Direct or a cable and write programs on it directly through an online interface (not entirely sure of the word for it, but you just type in an IP address in your browser and it brings up an interface where you can make projects and code). All the code written in that interface saves directly onto the Wallaby. Here the default include statement is #include <kipr/botball.h>, so I'm assuming that botball.h (which is located on the Wallaby's storage) has all those 60 libraries consolidated in it. I got the include folder that I'm using from GitHub. This link was provided to me by one of the Botball organisers. So the main point in me trying to download the library is so that I can write and successfully compile code even when I'm not connected to the Wallaby. Hope this provides some relevant context.
Thank you for your answers!
What I'd do is
Create (maybe with scripting tools or a specific program) a "all.h" file which includes all the other header files
#ifndef ALL_INCLUDED
#define ALL_INCLUDED
#include "accel.h"
#include "bccel.h"
//...
#include "zccel.h"
#endif
Include "all.h" in your main file
#include "../code/include/all.h"
You can create "all.h" automatically every time you build your code.
CLion is an IDE for Clang and GCC. These compilers are instructed to search paths for include files by specifying -I<path> command line arguments. Any number may be specified, and they are searched in the order given, and the first match found is the file that gets included.
I am not familiar with CLion specifically but no doubt it has a dialog somewhere where you can set header file search paths.
Edit: It seems that CLion may not make this so straightforward. I understand that you have to add then via CMake: https://cmake.org/cmake/help/v3.0/command/include_directories.html#command:include_directories, but after that, the IDE will not recognise the header in the editor and will warn you of unrecognised files and will not provide code comprehension features. I believe it will build nonetheless.

Can a C header file only specify a name of another header file

I found a pacman project in github where a file conf.c includes a header file #include "ini.h" where ini.h contains only a single line (i.e no #include statement):
//ini.h
../common/ini.c
I have never seen anyone doing this before! It seems a bit hackish/rough around the edges. My questions are:
Is this legal C?
Is it portable?
Is it recommended?
I would have assumed the answer should be no for all these questions, but I may be learning something new...
edit
From the answers, I see its a Linux symlink. I guess that this means it is not portable to Windows, and would also make it more difficult to read outside a unix environment. I would also imagine that using relative paths (or include directories) instead of symlinks would be a better practice in cases like this for reasons mentioned above...
src/pacman/ini.h is a symbolic link according to the site.
Symbolic link has an information of where the target file is (path name), and I guess it is what is displayed on the site.
The OS will redirect access to that ini.h to ../common/ini.h, which is a normal C code.
I don't see any reason why not. The include statement indicates the compiler to replace that line with the whatever is in the included file

C (or embedded script) function that will open a .txt file visually

I am writing a program that writes to a .txt file but with a separate function I would like to open the file visually (in the default text editor).
I want the function to do the same as double clicking a file...
Not opening a file just to edit it in the code (not with fopen()) but actually view the file in a separate window!
Cross-Platform if possible.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
system("exec programname filename");
return 0;
}
This works on linux, hope that helps. Im not quite sure how you could tell which editor is default and open that up though.
If you were programming this hypothetical function in C on Windows, you could do something like
system ("notepad myfile.txt");
There is no way to do this cross-platform.
If a user double-clicks on a file, then the OS takes over and checks to see which application has been associated with the file type. Since this is an OS specific activity, it differs in implementation from OS to OS.
To do it you would need to query the OS for the application to launch with a system call. This is OS-specific though.

Resources