build a c program like portable program with libraries - c

I'm trying to create a c program that work in multi linux os (ex ubuntu 16 64bit, centos 6 64bit, ubuntu 12 64bit,..) without installing the dependencies.
for example I'm using the libgcrypt.so.20 library in my program
I need a way to compile my program and import all necessary library from lib folder, so in the other host i just copy my compiled program and my lib folder that contains all my .so library
because I don't have privilege to install libgcrypt in the other host.
so if this is possible ?

Related

How do you include external libraries when you cross-compile c programs using mingw?

I am building a simple command line game in C using the ncurses library on a Linux machine but I want to be able to run the compiled code on a Windows computer. To do this, I am using the MinGW-w64 cross compiler tool in Linux and compiling it to run in a 64 bit Windows environment. However, when I try to compile using this command:
x86_64-w64-mingw32-gcc -o game.exe barebones.c -lncurses
I get this error:
barebones.c:2:10: fatal error: ncurses.h: No such file or directory
2 | #include <ncurses.h>
| ^~~~~~~~~~~
compilation terminated.
I installed ncurses on my Ubuntu machine and can create and run the same simple program to run on Linux. I have been able to cross-compile and run simple programs that only use the default libraries. I think I must be listing the ncurses library incorrectly in the compliation command or that I am failing to understand other posts that show that this doesn't work.
I am using Windows 10 and Ubuntu 21.04.
Debian provides no cross-compiling packages for ncurses. (Ubuntu provides no additions or improvements to ncurses in any way, simply reusing packages from Debian). If you want to cross-compile ncurses, you'll have to build ncurses in cross-compiling form.
For development purposes, ncurses packages can be built using the scripts under the (sources) packages directory, e.g., after downloading the current source:
tar xf ncurses-whatever.tgz
cd ncurses-whatever
cp -var packages/debian-mingw64 ./debian
dpkg-buildpackage
That's a starting point. You'd have to do something about the email in the debian/control file to appease dpkg-buildpackage (tutorials are off-topic).

How can I link the 3rd party library bzip2 in my gcc compiler?

I'm a python developer new to C and developing C code on Windows that needs to work on Windows and Linux.
To that end, I downloaded MSYS2 and used pacman to install gcc and bz2.
My question is: How do I use bzip2 in my C code.
When I try to compile this C code:
#include <bzlib.h>
using the command gcc test.c -lbzip2 -o test.out I get the following error:
test.c:1:10: fatal error: bzlib.h: No such file or directory
Am I including the correct header file? Am I linking it correctly?
When not using 3rd party libraries a simple "hello world" program compiles and executes fine.
Short version: assuming you are using the MSYS target, pacman -S libbz2-devel.
Long version: In MSYS2 you can find which package contains a file using:
pacman -F bzlib.h
to which the answer is:
mingw32/mingw-w64-i686-bzip2 1.0.8-1 [installed]
mingw32/include/bzlib.h
mingw64/mingw-w64-x86_64-bzip2 1.0.8-1 [installed]
mingw64/include/bzlib.h
msys/libbz2-devel 1.0.8-1 (development)
usr/include/bzlib.h
To interpret this output, first understand that an MSYS2 installation supports three different development targets:
mingw32 (builds native Win32 applications using mingw-w64)
mingw64 (builds native Win64 applications using mingw-w64)
msys (builds Win32 or Win64 applications that depend on MSYS DLLs and runtime environment, using a custom GCC port and runtime library, and supports a lot of POSIX functionality).
When you install MSYS2 you will get three startup scripts in the Start Menu , one for each of those environments.
The output of pacman -F above told us that for targets mingw32 and mingw64, the package bzip2 contains the files required to do development with bzip. However, on the msys target, the package libbz2-devel is required.
This is a common package layout in msys and in the various *nix package managers (MSYS2 pacman is a port of ArchLinux pacman):
bzip2 is the binaries for using bzip2 in your shell
libbz2 is a shared object binary (DLL)
libbz2-devel is the header files and static libraries that you need to link bzip2 into your program.
You can list the files for each package with pacman -F --list libbz2-devel etc.
The mingw32/mingw64 targets typically have single packages that include all of those three things in the one package, e.g. pacman -F --list mingw64/mingw-w64-x86_64-bzip2.
I assume you are using msys target as otherwise this question would not have arisen .
Installing all the binary packages listed here and changing the header filename to bzlib.h fixed the problem.

C with Eclipse on Ubuntu: why is an .exe and no linux executable created?

I am using Neon.3 Release (4.6.3) Eclipse IDE for C/C++ Developers under Ubuntu 15.5. When I build an run my C-program the program
the program executes without any errors/warnings
delivers the expected output in the eclipse-console
and generates an .exe file in the the debug folder
For me it is very much surprising that an .exe file is generated using an Linux OS (I thought these files can only be created under Windows?). How can I configure Eclipse to generate a typical Linux-executable instead?
Many thanks!
Extensions don't matter much in Linux. You can name an executable something.exe and it won't change how it runs...

Creating a standalone C executable

I want to create a standalone executable from a C file so that the libraries that I'm using don't have to be installed on the computer that I'm running the executable on.
I'm using the libssh and libconfig libraries, both of which may need to be installed on the machine I'm running the executable on.
When I run my binary on a machine that doesn't have these libraries installed, I get:
error while loading shared libraries: libssh.so.4: cannot open shared object file: No such file or directory
Is there a way I can compile my program.c file so that it has the libssh binaries included within it?
See this Link http://api.libssh.org/master/libssh_linking.html about how to link the static version of the libssh. There must be similar instructions for libconfig.

Dealing with static libraries when porting C code from one operating system to another

I have been working on some C code on a windows machine and now I am in the process of transferring it to a Linux computer where I do not have full privileges. In my code I link to several static libraries.
Is it correct that these libraries need to be re-made for a Linux computer?
The library in question is GSL-1.13 scientific library
Side question, does anyone have a pre-compiled version of the above for Linux?
I have tried using automake to compile the source on the Linux machine, but no makefile seems to be created and no error is output.
Thanks
Yes, you do need to compile any library again when you switch from Windows to GNU/Linux.
As for how to do that, you don't need automake to build GSL. You should read the file INSTALL that comes inside the tarball (the file gsl-1.16.tar.gz) very carefully. In a nutshell, you run the commands
$ ./configure
$ make
inside the directory that you unpacked from the tarball.

Resources