How to create a makefile to use the mpir and mpfr libraries - c

I've recently written a C program which uses the public-domain mpir and mpfr libraries. I've been compiling it on Windows, using the Microsoft Visual C++ 10.0 compiler. To get that to work, I had to do the following:
Download / build the mpir and mpfr libraries from http://gladman.plushost.co.uk/oldsite/computing/gmp4win.php
Move the files mpir.h and mpfr.h into the Include directory for the Microsoft compiler
Move the files mpir.lib, mpir.pdb, mpfr.lib and mpfr.pdb into the lib directory for the Microsoft compiler
#include mpir.h and mpfr.h in the program
Compile using cl <..module names..> /link mpir.lib mpfr.lib
I now want to send the source / header files I've written to someone else, along with a makefile that they can use to compile the code and check that it works. Since that person won't have the mpir / mpfr libraries installed, and might not be using the same compiler, I'm not quite sure how to do this.
Here is what I can do:
Send them the binaries mpir.lib, mpir.pdb, mpfr.lib and mpfr.pdb as well as the source / header files.
Here is what I can't do:
Send them my entire Microsoft Visual C++ 10.0 setup
Ask them to stick files in their Include and lib directories (unless there's no other way)
Ideally, I should be able to send them the source/header files, together with the pertinent mpir/mpfr binaries, and a makefile which they can then run to build the program.
Thanks in advance for your help!

Why on earth are you adding those files to your compiler installation path?? The compiler has command line options for specifying search paths.
For instance,
cl /I"path/to/mpfr/header" <...filenames...> /link /LIBPATH:"path/to/mpfr/lib" mpir.lib mpfr.lib
You should only have to send your source code, mpir.h, mpir.lib, mpfr.h and mpfr.lib. The PDB files contain debugging information, and are not necessary for compilation.
Also, I don't know how to create a makefile, but a simple batch file with the command above should suffice for something so simple.

Related

How can I use my C library as others like stdio

I recently made a small library in C, and I wanted to put it together with the standard libraries so I don't have to always copy the files for each new project.
Where do I have to put it so I can import it like the standard libraries?
Compiler : MinGW
OS: Windows
You need to create a library, but you don't necessarily need to put it in the same place as MinGW's standard libraries (in fact I think that's a bad idea).
It is better to put your own library/libraries in specific place and then use the -I compiler flag to tell the compiler where to find the header files (.h, .hpp, .hh) and the -L linker flag to tell the linker where to find the library archives (.a, .dll.a). If you have .dll files you should make sure they are in your PATH environment variable when you run your .exe or make sure the .dll files are copied in the same folder as your .exe.
If you use an IDE (e.g. Code::Blocks or Visual Studio Code) you can set these flags in the global IDE compiler/linker settings so you won't have to add the flags for each new project.
Then when building a project that uses your library you will need to add the -l flag with the library name to your linker flags, but without the lib prefix and without the extension (e.g. to use libmystuff.a/libmystuff.dll.a specify linker flag -lmystuff). The use of the -static flag will tell the linker to use the static library instead of the shared library if both are available.
I have created a minimal example library at https://github.com/brechtsanders/ci-test to illustrate on how to create a library that can be build both as static and shared (DLL) library on Windows, but the same code also compiles on
macOS and Linux.
If you don't use build tools like Make or CMake and want do the steps manually they would look like this for a static library:
gcc -c -o mystuff.o mystuff.c
ar cr libmystuff.a mystuff.c
To distribute the library in binary form you should distribute your header files (.h) and the library archive files (.a).

What files can be safely deleted from the MinGW-w64 binaries?

I am using the MinGW-w64 installer found here:
https://mingw-w64.org/doku.php/download/mingw-builds
Installing to a directory creates a folder called mingw32 or mingw64 that contains the compiler. I already know that the additional batch script, HTML link, and uninstall executable in the same directory can be deleted.
The mingw32/64 folder contains these folders:
bin
etc
include
lib
libexec
licenses
opt
share
i686-w64-mingw32 (mingw32) or x86_64-w64-mingw32 (mingw64)
I only wish to compile with C and nothing else. So what files can I delete in order to reduce the file count because the compiler contains files for Ruby, Fortran and other languages which as I stated, I don't need it.
Why are people still using Mingw or Mingw64 instead of TDM-GCC ? You have to rely on the default builds or build it yourself from source. Trying to install it on your own terms sucks.
TDM-GCC on the other hand, you can specify where and which components to install as you wish. You can upgrade or remove it without any worries. It's more fast, easy,modern and equivelant these days.
Now for your question, you can download and install TDM-GCC by it's installer. I don't really think you need any further instructions as a 5 year old would understand how to use it. Just select only the components required for c.
If you don't understand,I would gladly help you more if needed. ^_^
If you want a smaller GCC toolset, try MinGW Distro.
Scroll down to "How To Build Your Own Distro", download components-14.1.7z, extract it, extract binutils 2.27 and GCC 6.3.0 inside the same folder. The total size is about 270MB.
as far as i know the minimum are yours
1, bin
5. libexec
not all tools in bin you may need but generally i would hold both of those dirs
you also ned libraries and headers if you want to build abainst standard c library and windows.h, so
include
lib
is also needed (hovever part of it may contain also headers and libraries for c++ standard library) all those main 3 libs are kinda mixed there afaik so it might be a bit hard to find what comes for what

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 install C packages on windows

I have to use LU decompostion to fit a simple model to some data (simulated) in C. An example of what I need to do is here:
However, I'm stuck with a more basic problem: how do I install packages in C and call them in my code?
I'm new in C and I'm used to R. But I have this assingment to do some tests about Matrix inversion, LU decomposision and the professor suggested using Lapack to easy things (thus, I don't need to code myself the LU decomposition etc.). But I don't know how to install the package and call it in my code, in order to use the functions of LAPACK.
I have a windows 7 64 bits and I'm using compiler Code Blocks 8.02
Thanks for any help.
Normally you don't "install" C libraries in that sense. Normally, in Windows you have three types of files. The header files, typically ending in .h, the dynamic library, .dll, and most likely some linker files (typically, .lib, .a or something). The linker and compiler will need to be able to find these files somewhere. Normally you set the include directory paths, and library directory paths.
E.g. Let's say you downloaded a library called foo, and you extract it to C:\foo.
In that folder, libfoo.a, foo.dll and foo.h reside. In Code::Blocks you will have to point include directory path to C:\foo and library path to C:\foo so that the linker and compiler know where to look for these files. Since you're linking against the foo library, you will also have to set -lfoo or something similiar in linker command line. This is GCC syntax, but I think Code::Blocks uses GCC compiler behind the scenes anyways.
In the C code you can just #include <foo.h> and the compiler will find it for you.
You need to install that library and it might actually supply a tool for that. Check their documentation (e.g. a file INSTALL or README in their distributed sources). If the library uses only headers you might only need to copy it's headers to some directory on your system, but their buildsystem might be able to do that for you.
Once that is done you would tell your IDE on where to look for the sources and if the library uses not just headers to link against the actual library file. See the documentation in the Code::Blocks Wiki on how this is done for some example cases and adapt for your library.
The simplest thing to do in your situation is to install Cygwin. You can use the setup.exe installer to install the GCC and the LAPACK libraries. When you want to use the LAPACK library, you will add the -llapack option to your GCC command line.

How to build gnu `libiconv` on & for windows?

I want to build a static library (*.LIB file) GNU libiconv on windows to be used with other libraries in Visual C++. Other libraries I'm using are built with "MultiThreaded DLL" (/MD) Runtime option. So, I need to build libiconv with the same option.
Problem is the libiconv uses GNU build system and I want to compile with /MD option. You can see the source structure of libiconv here:
http://cvs.savannah.gnu.org/viewvc/libiconv/?root=libiconv
Mr. Zlatkovic maintains the windows port of GNU libiconv for libxml2
you can see them here:
ftp://xmlsoft.org/libxml2/win32/iconv-1.9.2.win32.zip
I cannot use his port. I need to build from the latest version of libiconv-1.13. I wonder how this guy has ported it? Can some one please tell me how to build *.lib from this and compile it using MSVC?
EDIT:
Actually, I need to build few more gnu libraries with same settings. So, if I get solution for one library. I can do the same for all others.
I found PARK Youngho's How to Build libiconv with Microsoft Visual Studio over at The Code Project to be complete and clean (for VS2010 and GNU libiconv 1.14).
A little addition to your answer.
I had the same issue and found that the MinGW + MSYS solution was perfect.
Though, I needed to go a little further and generate also the .lib file in order to be able to link with the resulting dll.
This is what I found:
generate a .def file from the dll with dumpbin (a Visual Studio tool).
generate the .lib file from the .def with the lib program (Visual Studio tool too)
This allows you to specify some link flags if appropriate.
Everything detailed here (I'm not the author of this method):
http://wiki.videolan.org/GenerateLibFromDll
I also realized that this lib/dll couple can be linked with both MD and MDd libraries.
Hope that can help people that find this post, like it helped me.
-David
I'm the OP. MSYS is the exact thing what I was looking for.
Just install MinGW & MSYS which contains shell sh.exe & make.exewith which you can configure and generate a Makefile after that you can use make.exe to run it.
Its as simple as that.
compile them using MinGW using Msys for the environment if needed. MinGW's .a files are apparently, according to the mailing list, the same format as .lib files (just do a rename). You might want to check first to see if the iconv static library is included already in the MinGW download / filesystem.
Edit: it's in msys (C:\msys\1.0\lib), along with:
libiconv.a
libiconv.dll.a
libiconv.la
and additionally
libiconv-2.dll (in C:\msys\1.0\local\bin)
Edit: is it in here, the libiconv you need? these versions seem to have MSVC makefiles :) http://www.opensource.apple.com/source/libiconv/

Resources