Including libraries in the custom code section in Simulink - c

I'm trying to Include some Libraries, like metis in the custom code library section in Simulink. Do you know what type of libraries Simulink excepts? Must they have the ending .a, .o, .dll or lib?
And can I include them into my custom c code with #include <metis.h>?

The library format should match the architecture on which you are going to compile the generated code. So .a or .so for GNU/Linux, .lib for Windows (you usually link against the .lib file not the .dll), and usually .dylib on Mac. You can also link in object files, (i.e. .o, .obj) but typically a software package will build some type of library for you to use.
If you are using any of the functions, types, etc. defined in any of the metis headers, then those headers need to be included in the generated code.
You can add #include "header_name.h" to the settings:
"Configuration Parameters->Simulation Target->Custom Code->Header File"
and:
"Configuration Parameters->Code Generation->Custom Code->Header File"
where header_name.h is replaced with the needed header file's name.
Since this question is tagged MATLAB Coder you can also use coder.cinclude('header_name.h'); in your MATLAB code to generate a #include "header_name.h" in the C code.
You may also need to augment the Include directories settings on the aforementioned Custom Code panes to allow the compiler to locate the headers.

Related

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 to Include external C library on windows

I am fairly new to C and I am trying to include a external library without using any IDE, only text-editor and the minGW compiler on windows cmd. The library is libPNG in this case, I would really like to understand how the process work not only for this library.
If there is a better way (and easier) to do this, I would also like to know.
You have two parts to take care of:
compilation,
linking.
Compilation
In compilation, when you transform source files in object files, your compiler must know what are the functions provided by the external library.
You could declare each function you use or you can include the library header file(s) in your code:
#incude <library_file.h>
It's not enough, you will have to tell your compiler where it can find this file:
-I<path_to_lib_folder> with gcc
/I<path_to_lib_folder> with cl (the visual studio compiler)
Linking
In linking process, you put the object and library files together to construct an executable file.
You need to tell the linker
what files it must use and
where it can find the library file
You tell the linker what files to use with the -l options, for instance, -lfoo will tell it to search for the libfoo.so lib
Note: with cl you can tell specify which library to use directly in your source code with #pragma comment (lib, "libfoo.lib")
Add you specify where with:
-L<path_to_lib_folder> with gcc
/LIBPATH:<path_to_lib_folder> with link (the visual studio linker)
You can also use dynamic linking, but let's start with the first step.

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 to tell linker to include a library in header file

I am using the Pelles C IDE and for certain projects have to tell the linker to include a certain library e.g. Ws2_32.lib. Currently I am developing a function I plant to reuse frequently throughout numerous projects, so I decided to make a header file for it. Is there a way to tell the linker to include a certain library from the header file ?
You have link the library, no other way.
If compiler is gcc in linux, you can link the lirary libtemp.so like
gcc youfile.c -ltemp
And before running the executable add the path of libtemp.so to LD_LIBRARY_PATH
In your IDE you can config the samething.

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