gcc to use newlib instead of glibc? - gcc

I want to use newlib instead of glibc in order to compile small static binaries. (I do not intend to cross-compile as the binaries are to be used by the same computer.) I believe that I need to compile a separate gcc for this ?
I compiled gcc:
./configure --prefix=/home/myuser/Desktop/gcc-4.4.5 --libexecdir=/home/myuser/Desktop/gcc-4.4.5 --libdir=/home/myuser/Desktop/gcc-4.4.5 --with-gxx-include-dir=/home/myuser/Desktop/gcc-4.4.5 --enable-languages=c --enable-libmudflap --disable-multilib --disable-libssp --disable-nls --with-newlib --with-gnu-as --with-gnu-ld --with-system-zlib
make
It compiled without errors but now when I try to compile a simple Hello World! program it wants to use headers from /usr instead of the path I specified above. These are some of the errors:
In file included from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/stdio.h:34:21: error: stddef.h: No such file or directory
In file included from /usr/include/stdio.h:75,
from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/libio.h:53:21: error: stdarg.h: No such file or directory
In file included from /usr/include/stdio.h:75,
from /home/myprogram/Desktop/myprogram.c:1:
/usr/include/libio.h:332: error: expected specifier-qualifier-list before 'size_t'
/usr/include/libio.h:364: error: expected declaration specifiers or '...' before 'size_t'
/usr/include/libio.h:373: error: expected declaration specifiers or '...' before 'size_t'
What am I doing wrong ? Is compiling a new gcc necessary or can I use my existing gcc and use newlib instead of glibc ???

You shouldn't need to rebuild gcc for this; you just need to point your existing gcc at the right things (using -I, -L etc.) and tell it not pull in the usual system stuff (using -nostdlib).
The section entitled "Shared newlib" in the newlib README file has runes for building and linking against either shared or static newlib when compiled natively.

I would strongly recommend using crosstool-ng to build your GCC toolchain. If you choose x86 followed by "bare metal" as the operating system, you can safely use newlib as a libc.
Note that newlib does not work with an operating system - that is standard things, like IO, won't work out of the box.

You have to tell the compiler where it can find the include files:
gcc -IyourDirectoryHere -IanotherDirectoryHere
-I
(that is a minus followed by a capital i as in Italy)
More details: http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Directory-Options.html

Related

Errors about asm for a gcc build on macos

I used to build a project on windows and now that I am trying to build it on macos (M1) I get errors on a dependency (nested in a dependency):
pixman-0.34.0/pixman/pixman-x86.c:103:4: error: unknown register name '%eax' in asm
: "%eax", "%ecx");
^
pixman-0.34.0/pixman/pixman-x86.c:136:4: error: invalid output constraint '=a' in asm
: "=a" (*a), "=r" (*b), "=c" (*c), "=d" (*d)
^
I also get warnings all around:
pixman-0.34.0/pixman/pixman-x86.c:83:5: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
#if _PIXMAN_X86_64 || defined (_MSC_VER) || defined(__native_client__)
^
pixman-0.34.0/pixman/pixman-x86.c:78:6: note: expanded from macro '_PIXMAN_X86_64'
(defined(__amd64__) || defined(__x86_64__) || defined(_M_AMD64))
^
The project is configured with CMake with very few parameters, most of the configuration is made in the CMake of the dependency (Orthanc Stone).
There is an emscripten build of almost the same sources and libs that is working fine so I guess I'm missing something in the CMake configuration to make it work on macos.
I suppose the target architecture is not compatible with the pixman lib (and probably others that the compilation has not reached yet). But I did not find how to fix it.
I installed gcc using homebrew
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc-11
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/11.3.0/bin/../libexec/gcc/aarch64-apple-darwin21/11/lto-wrapper
Target: aarch64-apple-darwin21
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/11 --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-11 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 11.3.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --build=aarch64-apple-darwin21 --with-system-zlib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Homebrew GCC 11.3.0)
The project is configured with CMake with very few parameters, most of the configuration is made in the CMake of the dependency (Orthanc).
Is it this Orthanc? How exactly are they building pixman with CMake? Pixman has lots of special cases implemented in Assembler. At build time, autoconf detects the right one to enable. Since you are writing that you are using CMake, I bet someone side-stepped all that auto-detection and just hardcoded what to build. And hardcoded the x86 build.
If this guess is right, then that is a bug in that CMake file and there is nothing anyone but orthanc devs can do about that.
Which part of orthanc are you trying to build exactly and how? I fail to find its use of cairo...

Problems installing packages which use SDL2 via cgo on Windows 7

I have problems installing SDL2 for golang, using Visual Studio Code.
I try to get the package:
"C:\Users\Bob\go\src\flappyGopher>go get -v github.com/veandco/go-sdl2/sdl
github.com/veandco/go-sdl2/sdl
# github.com/veandco/go-sdl2/sdl
In file included from ..\github.com\veandco\go-sdl2\sdl\audio.go:4:0:
./sdl_wrapper.h:2:23: fatal error: SDL2/SDL.h: No such file or directory
compilation terminated."
I used this manual: https://github.com/vinzBad/go-sdl2-tut/blob/master/00_preparation/windows.md
I have also the env variables:
CGO_CFLAGS C:\MinGW64\mingw64\include
GOPATH C:\Users\Bob\go
GOROOT C:\Go\
PATH ... C:\MinGW64\mingw64\x86_64-w64-mingw32\bin;C:\MinGW64\mingw64\bin;C:\Users\Bob\Downloads\SDL2-2.0.8\x86_64-w64-mingw32\include\SDL2
edit:the gcc output looks like a mess
C:\Users\Bob>gcc -xc -E -v -
Using built-in specs.
COLLECT_GCC=gcc
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-5.1.0/configure --build=x86_64-w64- mingw32 --e
nable-targets=all --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable
-libgomp --enable-lto --enable-graphite --enable-cxx-flags=-DWINPTHREAD_STATIC -
-disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-libstdcxx-d
ebug --enable-threads=posix --enable-version-specific-runtime-libs --enable-full
y-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-l
d --disable-werror --disable-nls --disable-win32-registry --prefix=/mingw64tdm -
-with-local-prefix=/mingw64tdm --with-pkgversion=tdm64-1 --with-bugurl=http://td
m-gcc.tdragon.net/bugs
Thread model: posix
gcc version 5.1.0 (tdm64-1)
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=x86-64'
C:/Program Files/TDM-GCC-64/bin/../libexec/gcc/x86_64-w64-mingw32/5.1.0/cc1.exe
-E -quiet -v -iprefix C:/Program Files/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-min
gw32/5.1.0/ -D_REENTRANT - -mtune=generic -march=x86-64
ignoring duplicate directory "C:/Program Files/TDM-GCC-64/lib/gcc/../../lib/gcc/
x86_64-w64-mingw32/5.1.0/include"
ignoring duplicate directory "C:/Program Files/TDM-GCC-64/lib/gcc/../../lib/gcc/
x86_64-w64-mingw32/5.1.0/../../../../include"
ignoring duplicate directory "C:/Program Files/TDM-GCC-64/lib/gcc/../../lib/gcc/
x86_64-w64-mingw32/5.1.0/include-fixed"
ignoring duplicate directory "C:/Program Files/TDM-GCC-64/lib/gcc/../../lib/gcc/
x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
C:/Program Files/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/include
C:/Program Files/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../..
/include
C:/Program Files/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/include-fixed
C:/Program Files/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../..
/x86_64-w64-mingw32/include
End of search list.
Please see this.
The reason is go install tried to compile some
cgo
code of the github.com\veandco\go-sdl2 package, and that code apparently
wants to use the C header files
of the libsdl2 library¹.
You need to make libsdl2
available to the compiler.
To figure out where the C compiler expects to find the include (header)
files, see this.
¹ That's how C works. To use a compiled library in other C code, the compiler
usually needs to know the definitions of the types and functions provided
by that library. The formats of such libraries—contrary to Go libraries—do not typically embed type information in their metadata, so the compiler
needs to get that information from somewhere. In C and C++ world this information is commonly kept in the so-called include (also: header) files
which are included—through the use of a so-called preprocessor—both into the
code used to build a library and the code which uses the library.

How to build a static tar command

I downloaded tar 1.28 and am attempting to compile a static binary to use on system recovery media on Fedora Linux (x86_64). It doesn't have to include common libraries, such as libc or pthreads, but I'd like to eliminate dependencies on selinux, acl, attr, pcre and lzma, etc.
The README indicates I can use
./configure CC=gcc LDFLAGS=-static
but this creates a fully static binary (including libc), but is missing support for anything normally found in the shared libraries (SELinux, ACLs, etc). Reading the config.log shows failed attempts to find a static library for acl
So, I installed libselinux-devel-static RPM, and this time got the selinux support in there, but I can't find static libraries for anything else. Search results say that libacl.a is in libacl-devel, but not mine.
A fully static binary is ok, but really just want to get the odd-balls in there so I don't have to put the shared libraries on the media.
I was looking at this as well, and i would get the following error message:
CCLD tar
../gnu/libgnu.a(quotearg.o): In function `quote':
/home/anarcat/dist/tar-1.27.1/gnu/quotearg.c:968: multiple definition of `quote'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libacl.a(quote.o):(.text+0x0): first defined here
names.o: In function `gid_to_gname':
/home/anarcat/dist/tar-1.27.1/src/names.c:95: warning: Using 'getgrgid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
names.o: In function `gname_to_gid':
/home/anarcat/dist/tar-1.27.1/src/names.c:155: warning: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
names.o: In function `uname_to_uid':
/home/anarcat/dist/tar-1.27.1/src/names.c:125: warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
names.o: In function `uid_to_uname':
/home/anarcat/dist/tar-1.27.1/src/names.c:65: warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
../lib/libtar.a(rtapelib.o): In function `rmt_open__':
/home/anarcat/dist/tar-1.27.1/lib/rtapelib.c:428: warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status
Makefile:1304: recipe for target 'tar' failed
I don't quite get it, but apparently, "You cannot getpwnam and friends with the whole flexibility that glibc has without shared libraries".
So game over? unclear.
For Fedora you can enable Repository CERT Forensics Tools
and complete build dependencies with some static libs (non-exhaustive list)
zlib-static
libiconv-static
libffi-devel
gettext-devel
pcre-static
ncurses-static
nss-devel
glibc-static
libselinux-static
libsepol-static
libattr-devel
libacl-devel
if libacl.a is missing you have to download source first from:
git clone https://git.savannah.nongnu.org/git/attr.git
git clone https://git.savannah.nongnu.org/git/acl.git
and compile with (first libattr, then libacl, then tar):
./autogen.sh
./configure --prefix=/usr --libexecdir=/usr/lib --enable-static --disable-shared --disable-rpath
make CFLAGS='-static'
make install

Missing crt1 and crti when crosscompiling

I'am trying to build a cross compile toolchain based on GCC4.5 and newlib with gold and link-time-optimization enabled. GCC compiles fine but it did not generate the crt1.o or crti.o files. Therefore when I tries to use the compiler for building Newlib it complains with the message:
ld: error: cannot open crti.o: No such file or directory
ld: error: cannot open crtn.o: No such file or directory
ld: error: cannot find -lc
When searching for files named crt* in the directory where GCC4.5 is installed i got the following result:
find ../../../tooltarget/ -name "crt*" -print #(result modified to consume less space)
crtprec80.o, crtend.o, crtfastmath.o, crtbegin.o, crtendS.o, crtprec32.o, crtbeginS.o, crtbeginT.o, crtprec64.o
From the GCC spec's it seems like gcc needs both the crtbegin.o and the crti.o files, but only one of them is available.
*startfile:
%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o
%s;:crtbegin.o%s}
Following is the flags i used when compiling GCC:
--prefix=${TTP}/usr --build=x86_64-pc-linux-gnu
--host=x86_64-pc-linux-gnu --target=x86_64-awn-linux-gnu
--with-gmp=${TTP}/usr --with-mpc=${TTP}/usr
--with-mpfr=${TTP}/usr --with-libelf=${TTP}/usr
--enable-languages=c --enable-lto
--disable-nls --disable-shared
--disable-multilib --disable-decimal-float
--disable-libmudflap --disable-libssp
--disable-libgomp --disable-threads
--without-headers --with-newlib
--with-build-sysroot=${TTP} --with-build-time-tools=${TTP}/usr/bin
I'm sure on if this is due to I configured GCC wrongly, or "stuff" simply do not work this way, or if the files crti.o should come from somewhere else.
Thanks in advance
Allan W. Nielsen
Some crt* files come not from the compiler, but from the C library. I suspect this is the case here for your crt1.o and crti.o.

Problem compiling gcc 4.4.0 on OpenSolaris 2009.6

I am attempting to compile gcc 4.4.0 on opensolaris 2009.6
Currently in the box (which is a AMD 64bit machine), I have the gcc 3.4.6 installed.
I unpacked the gcc 4.4.0 tarball.
I set the following env variables:
export CXX=/usr/local/bin/g++
export CC=/usr/local/bin/gcc
Then I ran "configure && make" and this is the error message that I got:
checking for i386-pc-solaris2.11-gcc... /export/home/me/wd/gcc/gcc-4.4.0/host-i386-pc-solaris2.11/gcc/xgcc -B/export/home/me/wd/gcc/gcc-4.4.0/host-i386-pc-solaris2.11/gcc/ -B/usr/local/i386-pc-solaris2.11/bin/ -B/usr/local/i386-pc-solaris2.11/lib/ -isystem /usr/local/i386-pc-solaris2.11/include -isystem /usr/local/i386-pc-solaris2.11/sys-include -m64
checking for suffix of object files... configure: error: in `/export/home/me/wd/gcc/gcc-4.4.0/i386-pc-solaris2.11/amd64/libgcc':
configure: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
Anyone has any suggestion as to how to work around this error message?
/Edit:
Content of the config.log is posted here: link text
Normally the GCC build is bootstrapped, i.e. first it uses the system compiler to build GCC C compiler, and then it uses the freshly built compiler to recompile GCC once again (and then even once more time again). The configure line shows that it is not the system compiler but the already-built GCC compiler which is used for configure test there.
Since it fails, the problem is that the freshly-built GCC is somehow "stillborn" here. If config.log will not help you, I'd suggest to ask at gcc-help#gcc.gnu.org.
EDIT: Ah-ha, I think it is the assembler. You are using GNU assembler, but the unsupported options look like they were meant for Sun assembler. This should be solved by adding --with-gnu-as configure option (and then possibly having to specify its path explicitly with --with-as=/usr/gnu/bin/as)
You can also take a look at Solaris-specific GCC build instructions.
There's a readily available build for gcc4, which you can try updating. Its current version is 4.3.3. To get started, install pkg-get from OpenCSW and check out the build from the subversion repository:
svn co https://gar.svn.sourceforge.net/svnroot/gar/csw/mgar/pkg/gcc4/trunk/ gcc4
cd gcc4
gmake package

Resources