sscanf + c99 not working on some platforms? - c

When I compile a simple Hello World! program that uses the sscanf function on my local Debian lenny x64, it works. But when I upload the same program to the server running CentOS x86, it will not work. If I do not use sscanf, then the program works on both computers.
gcc -std=c99 -O2 -pipe -m32
If I compile it with sscanf but without -std=c99, then it works on both computers.
gcc -O2 -pipe -m32
What is the problem with sscanf and c99 on CentOS x86 ? I thought that compiling with the -m32 flag would work on all Linuxes ? (I have limited access to the CentOS server, so I do not have access to error messages.)

Probably the CentOS box is using an old version of glibc. Since the nonstandard GNU extensions to their scanf implementation ended up making glibc conflict with c99, they added a nasty hack of redirecting *scanf to __isoc99_*scanf when -std=c99 is in use; if your copy of glibc is missing the __isoc99_sscanf symbol, the program will then fail to run.
Static linking, or linking to a different libc without ugly backwardsness-compatibility hacks, would solve the problem.

Are you uploading the binary or the source and then recompiling? If you are uploading the binary, you are probably running into a library compatibility issue between Debian and CentOS.
If that is the case, upload the source only and recompile on CentOS.

If you do not have permission to compile # CentOS, then try compiling a static binary. You can use dietlibc which makes smaller binaries than glibc or try EGLIBC which is the default C library that Debian will use starting Debian "squeeze".

I came up with the similar problem, it works # Ubuntu 64-bit, but the compile fails # CenseOS 64-bit (REHL5 desktop):
the error message is:
undefined reference to `__isoc99_sscanf#GLIBC_2.7'
when i copied the executable file compiled #Ubuntu to REHL5, and run it another error appeared:
elf file os abi invalid
it is compiled without flag -std=c99, i'm a newbie at C, and looking forword some workarounds, ex. add some flag.
Makefile:
CC=gcc
CCFLAGS= -Wall -O2 -DLINUX -I../include
demos:linuxdemo.c
$(CC) $(CCFLAGS) -o demoA linuxdemo.c -L../lib -lsense4 -lusb
$(CC) $(CCFLAGS) -o demoSO linuxdemo.c -lusb -lsense4
clean:
rm -f demoA
rm -f demoSO

You need to update your glibc to 2.7
download the rpm package from here:
http://archive.fedoraproject.org/pub/archive/fedora/linux/releases/8/Everything/x86_64/os/Packages/
needs:
libc-common-2.7-2.x86_64.rpm
glibc-headers-2.7-2.x86_64.rpm
glibc-devel-2.7-2.x86_64.rpm
glibc-2.7-2.x86_64.rpm
command:
rpm -Uvh --aid --nodeps glibc-common-2.7-2.x86_64.rpm
rpm -Uvh --aid --nodeps glibc-headers-2.7-2.x86_64.rpm
rpm -Uvh --aid --nodeps glibc-devel-2.7-2.x86_64.rpm
rpm -Uvh --aid --nodeps glibc-2.7-2.x86_64.rpm

Related

Qemu can't boot custom operating system [duplicate]

I followed this article on how to make a very simple kernel which prints "Hello, World". The files to my project are available on Github.
I built my cross compiler from this project. I use these command to cross compile, assemble, and build my ISO (CD-ROM image):
i686-elf-as boot.s -o boot.o
i686-elf-gcc -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
i686-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc
mkdir -p isodir/boot/grub
cp myos.bin isodir/boot/myos.bin
cp grub.cfg isodir/boot/grub/grub.cfg
grub-mkrescue -o myos.iso isodir
When I tried to execute it on VirtualBox, it gives the following message:
FATAL: No bootable medium found! System halted.
Why am I getting this error, and how can I fix it?
P.S. I use Ubuntu subsystem for Windows 10
Likely there is a GRUB component missing or an incorrect version of Xorriso. In the Bare Bones article you linked to it says this:
You can easily create a bootable CD-ROM image containing the GRUB bootloader and your kernel using the program grub-mkrescue. You may need to install the GRUB utility programs and the program xorriso (version 0.5.6 or higher).
When using grub-mkrescue to make ISO images, more often than not unbootable ISOs is a result of Xorisso installation missing. If grub-mkrescue runs but doesn't produce ISOs at all then likely a GRUB component is missing. To ensure that you have all the GRUB and Xorriso components installed install the components on modern Ubuntu releases with:
sudo apt-get install grub2-common grub-pc xorriso
Then attempt to rebuild and rerun your ISO image in VirtualBox.

Dynamic linking libgit2 .so in gcc

I'm running a Debian (Buster) container and my goal is to compile a small program I wrote which relies on libgit2. First, I was installing libgit2 via the libgit2-dev package and my Makefile had the following:
gcc -O2 -fpic -shared -I /usr/local/include -lgit2 -o output.so my_app.c
However, I'd rather have a "cleaner" environment and install libgit2 via the libgit-27 which, AFAIK, only installs the shared object binary instead of also including the development files like libgit2-dev does.
Using find I can find where the .so file is installed into:
$ find / -name "*git2*" -print 2>/dev/null
/usr/lib/x86_64-linux-gnu/libgit2.so.0.27.7
/usr/lib/x86_64-linux-gnu/libgit2.so.27
/usr/share/doc/libgit2-27
/var/lib/dpkg/info/libgit2-27:amd64.list
/var/lib/dpkg/info/libgit2-27:amd64.symbols
/var/lib/dpkg/info/libgit2-27:amd64.md5sums
/var/lib/dpkg/info/libgit2-27:amd64.shlibs
/var/lib/dpkg/info/libgit2-27:amd64.triggers
and I've been trying several combinations of linking this .so with gcc like:
gcc -O2 -fpic -shared -L /usr/lib/x86_64-linux-gnu/ -libgit2.so.27 -o output.so my_app.c
but so far I always get the following error:
my_app.c:1:10: fatal error: git2.h: No such file or directory
#include <git2.h>
^~~~~~~~
compilation terminated.
I understand this is a glaring lack of knowledge on how C compilation works. My two questions are:
Is it possible to compile my program by just relying on the libgit2-27 Debian Buster package instead of libgit2-dev? If not, why?
If yes, an example and explanation would be appreciated!

gcc can't find -lX11

I've used linuxbrew to install gcc 5.3 on a machine on which I don't have sudo access. I now want to link with X11:
> gcc test.c -lX11
ld: cannot find -lX11
I've checked that libX11.so exists in /usr/lib64/ which is on the compiler's LIBRARY_PATH. If I use the system's gcc it works fine, but I need a newer version to compile my actual program.
use -L flag, like this -L/usr/lib64, or you can specify full path to library like this gcc test.c /usr/lib64/libX11.so
According to this comment by a linuxbrew developer,
linuxbrewed gcc removes /usr/lib64 from the library path because mixing system libraries with brewed libraries creates havoc.
The solution is to brew install linuxbrew/xorg/xorg.

gcc and glibc versions

I have gcc 4.1.2 installed. I installed a new separate gcc (version 4.4.6) too using yum on CentOS. Now my question is, do these two gcc versions use the same glibc version or glibc is different for both of them? How can I find out? Secondly, is it better to have a newer version of glibc in terms of performance?
Both GCC versions will use the glibc version you have installed on your system. GCC packages don't (usually) ship a separate C library.
Write a simple program which makes a call to a glibc function. Then compile it with both versions of gcc and then do ldd a.out on each compilation. You'll get the list of libraries used.
If your source file is test.c then:
$ gcc test.c -o out1 # with gcc 4.1.2
$ gcc test.c -o out2 # with gcc 4.4.6
$ ldd out1
$ ldd out2
This will show the libc versions used by each gcc.
Performance may or may not be better depending on the update done for glibc functions.

How do you compile a C program?

This might be the most newbie question ever, but how do you compile a C program?
I’ve downloaded the source of a C program (ffmpeg, to be precise). How do I compile it?
For most Unix-style C programs, the incantation is:
./configure
make
sudo make install
This should already be documented in the INSTALL file, which additionally may contain further useful information.
For a single file just cc file.c (or gcc or whatever you C compiler is called)
For a complex project like ffmpeg, then either make, cmake, configure some other. Check their documentation
It depends on what OS and compilers you have, but typically the sequence is:
$ ./configure
$ make
$ sudo make install
to compile simple math program, it's not enough to <include math.h>. See
gcc file.c -lmath -o program_bin
for a single .c file using ffmpeg libraries, it can be made this way:
gcc -Wall -g live_segmenter.c -o live_segmenter -lavformat -lavcodec -lavutil -lbz2 -lm -lz -lfaac -lmp3lame -lx264 -lfaad -lpthread -I/home/devicer/ffmpeg/include -L/home/devicer/ffmpeg/lib
notice -L and -I options. In serious projects they are usually set by pkg-config.
for the ffmpeg itself..
- install lame, few other required libraries, then do as Chris said.
Btw, sometimes it requires gmake, not make.
Also, have a look on
./configure --prefix /home/devicer/ffmpeg
This is what was mentioned (used for) in segmenter compilation above.

Resources