Static library header file - c

I am trying to import a static library based on Hierarchical Matrices (H2Lib). The folder contains a make file which compiles the library files, examples and tests into a single .a file. I have referred to tutorials on creating and using static libraries in C using archiver command line in Linux but this does not create a header file, which I had to create manually while working out the tutorial. The H2Lib has multiple files and it would be difficult and time consuming to create a header file manually for this. I am not sure if I am missing something here or doing something wrong; I am new to the concept of libraries in C. Can some one please help me on how to use this library in C?
P.S: git repository link for H2Lib: https://github.com/H2Lib/H2Lib/tree/master

You are not supposed to write the header files yourself. Somewhere on the folder where the library is defined there should be a directory with multiple .h files (the headers) (it's usually named include).
What you need to do is include them into your project. You do this by appending -I to the path of each folder containing the headers and then writing #include "headername.h" in your source code.
So if the headers are in dir/include, you'd do:
gcc yourfiles.c <flags> output.o -I dir/include

Related

How to install C source files and headers?

I've been given these source files and headers. In the README.md the authors explain how to launch the test executables without the need of a proper installation. It is just a make command to run. They explain how to generate the .so files. I think these latter are meant to be used if I wanted to install the APIs at a system level (the definitions should be in api.h). My question is: where should I copy the shared objects generated by the Makefile and the api.h header? I aim to write a source file from scratch where I use those APIs (e.g. crypto_sign()) just including the headers, if it is possible. Thanks
where should I copy the shared objects generated by the Makefile and the api.h header? I aim to write a source file from scratch where I use those APIs (e.g. crypto_sign()) just including the headers, if it is possible
Nowhere.
The project comes with CMake support. Use CMake in your project and just add_subdirectory the repository directory.
Anyway, if you really wish to install the library system-wide, then FHS specifies directory structure on linux. For local system administration use /usr/local/lib for local libraries .so files and /usr/local/include for local C header files.

Extra Header Files Created After Build in IAR Workbench

I learn embedded programming with STM32F401RE in IAR Workbench.
I am confused about header file creation after build. Here below my question:
In the below file structure of IAR Workbench before build only a c file and there is no header file in the user folder
However after build process there are many header file in the user folder file.
My question is what is the purpose of the header file in it.
In addition to that question all headers files must be involved before build process?
Thanks.
Those are all header files that you have included in your main.c file, either directly or indirectly. Most at least are IAR library headers that come with the compiler. They are not created, but instead detected to be in use when you build your project.
For example, if you #include <stdio.h> in your source file, then stdio.h will be on that list. And all files that stdio.h includes will also be on that list. And then all includes from those included files are also on that list.
IAR library header files have typically have a lot of nested includes in them. Most likely you have at least one IAR library #include in your main.c file, or you have preincluded in a library header with your compiler command.

how can i fix undefined reference to '_imp_curl_easy_setopt' in c?

extracting the curl folder in include path project and include them in code then copy the content of lib folder there is two files 'libcurl.a,libcurl.dll.a' into lib path in mingw and linked it using eclipse ide.
properties>c/c++ build>settings>MinGW c linker>libraries
Are you trying to link with curl statically or to link with curl import library both files have the suffix ".lib" so it is a little confusing.
If you are trying to link with curl static library, you need to have the define CURL_STATICLIB defined at your project pre-processor definition.

How do I use a compiled binary for a C library on Linux?

I downloaded the libftp source from here, and compiled it using make. Now I just have a binary called "ftp" in the same directory. Do I need to move it to one of the root directories, like /usr/bin? Do I still need the source files so that I can #include the library?
To use a library in your own code, first you need that library - generally named lib{name}.a or lib{name}.so. The library will be required during linking. So when you make, make sure it creates the library and not just the ftp binary.
Secondly, at compile time you will need the header files so that the #include's don't throw errors. So as far as source goes, you will at least need the header files.

How do you actually use a C library?

I'm sure this question has been asked many times, but I can't figure this out. Bear with me.
So when you download a library, you get a bunch of .c and .h files, plus a lot of other stuff. Now say you want to write a program using this library.
I copy all the .h files into my project directory. It just doesn't compile.
Great, so then I get the library as a bunch of .dll's, and i copy the dlls into my project directory. Still doesn't compile.
How does this work?
What do you do, like right after creating the folder for your project? What parts of the library package do you copy/paste into the folder? How do you make it so that it can compile? Go through the steps with me please.
Where to put the .h files?
Where to put the .dll files?
How to compile?
Thanks.
(the library I'm trying to get working is libpng, I'm in windows with MinGW, and i'm looking to compile from command-line like usual.)
(from what i gather, you put the .h files in directory A and the .dll files in directory B and you can use -l and -L compiler options to tell the compiler where to find them, is this correct?)
Here's a brief guide to what happens when you compile and build a basic C project:
The first stage compiles all your source files - this takes the source files you've written and translates them into what are called object files. At this stage the compiler needs to know the declaration of all functions you use in your code, even in external libraries, so you need to use #include to include the header files of whatever libraries you use. This also means that you need to tell the compiler the location of those header files. With GCC you can use the -I command line to feed in directories to be searched for header files.
The next stage is to link all the object files together into one executable. At this stage the linker needs to resolve the calls to external libraries. This means you need the library in object form. Most libraries will give you instructions on how to generate this or might supply it ready built. Under Linux the library file is often a .a or .so file, though it might just be a .o. Again you can feed the location of the library's object file to GCC with the -L option.
Thus your command line would look like this:
gcc myProg.c -I/path/to/libpng/include -L/path/to/libpng/lib -lpng -o myProg.exe
(Note that when using the -l command line GCC automatically adds lib to the start of the library, so -lpng causes libpng.a to be linked in.)
Hope that helps.
Doing it under windows (supposing you user Visual Studio)
After unpacking add the library include directories to your projects' settings (Project -> Properties -> C/C++ -> Additional Include Directories)
Do the same thing for the Libraries Directory (Project -> Properties -> Linker -> Additional Library Directories)
Specify the name of the library in your Linker Input: Project -> Properties -> Linker -> Input -> Additional Dependencies
After this hopefully should compile.
I don't recommend adding the directories above to the Global settings in Visual Studio (Tools -> Options -> Project and Solutions) since it will create and environment where something compiles on your computer and does NOT compile on another one.
Now, the hard way, doing it for a Makefile based build system:
Unpack your stuff
Specify the include directory under the -I g++ flag
Specify the Library directory under the -L g++ flag
Specify the libraries to use like: -llibrary name (for example: -lxml2 for libxml2.so)
Specify the static libraries like: library name.a
at the end you should have a command which is ugly and looks like:
g++ -I/work/my_library/include -L/work/my_library/lib -lmylib my_static.a -o appname_exe MYFILE.CPP
(the line above is not really tested just a general idea)
I recommend go, grab a template makefile from somewhere and add in all your stuff.
You must link against a .lib or something equivalent i.e. add the ".lib" to the libraries read by the linker. At least that's how it works under Linux... haven't done Windows so a long while.
The ".lib" contains symbols to data/functions inside the .dll shared library.
It depends on the library. For examples, some libraries contain precompiled binaries (e.g. dlls) and others you need to compile them yourself. You'd better see the library's documentation.
Basically, to compile you should:
(1) have the library's include (.h) file location in the compiler's include path,
(2) have the library stubs (.lib) location in the linker's library path, and have the linker reference the relevant library file.
In order to run the program you need to have the shared libraries (dlls) where the loader can see them, for example in your system32 directory.
There are two kinds of libraries: static and dynamic (or shared.)
Static libraries come in an object format and you link them directly into your application.
Shared or dynamic libraries reside in a seperate file (.dll or .so) which must be present at the time your application is run. They also come with object files you must link against your application, but in this case they contain nothing more than stubs that find and call the runtime binary (the .dll or the .so).
In either case, you must have some header files containing the signatures (declarations) of the library functions, else your code won't compile.
Some 'libraries' are header-only and you need do nothing more than include them. Some consist of header and source files. In that case you should compile and link the sources against your application just as you would do with a source file you wrote.
When you compile, assuming you have the libs and the headers in the same folder as the sources you are compiling, you need to add to your compile line -L . -I . -lpng. -L tells the linker where to look for the library, -I tells the compiler where to look for the headers and -lpng tells the linker to link with the png library.
[Edit]
Normal projects would have some sort of hierarchy where the headers are in an /include folder and the 3rd party libs are in a /libs folder. In this case, you'd put -I ./include and -L ./libs instead of -I . and -L.
[Edit2] Most projects make use of makefile in order to compile from the command line. You can only compile manually for a small number of files, it gets quite hectic after that
Also,
you may want to look over Dynamic Loading support in various languages and on various
platforms.
This support is very handy in cases when you want to use a library optionally and you don't want your program to fail in case that library is not available.

Resources