Compiling the runtime library

New to FreeBASIC? Post your questions here.
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Compiling the runtime library

Post by Juergen Kuehlwein »

Trying to compile the runtime i ran into this problem:
CC src/rtlib/obj/win32/thread_call.o
In file included from src/rtlib/thread_call.c:33:0:
E:/MinGW/mingw32/i686-w64-mingw32/include/ffi.h:61:9: error: macro names must be identifiers
#ifndef @TARGET@
^
compilation terminated due to -Wfatal-errors.
make: *** [makefile:556: src/rtlib/obj/win32/thread_call.o] Fehler 1

I use Msys2 and MinGW 32 under Windows 7. It compiles fine for a few seconds ("CC ...") but then it stops right here. I got ffi.h from here (https://sourceware.org/libffi/) as recommended here (https://www.freebasic.net/wiki/wikka.ph ... ildWindows) Obvioulsy there is an error in ffi.h - maybe i got the wrong one. There is a more recent version of ffi.h on Github (Libffi), but the erroneous line is the same.

What´s wrong or where to get a correct ffi.h (and ffitarget.h)?

Thanks

JK
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Compiling the runtime library

Post by counting_pine »

It sounds like maybe you've taken the ffi.h.in file, and renamed it?

That file has to pass through the configure/make process to fill in some of the variables, in order to become a valid header file that works with your target.
If you've got a working build environment, then you should be able to cd into the libffi directory, and run './configure', then 'make'. This should build ffi without any issues.

This will produce header (*.h) and library (*.a) files in ./[target]/include and ./[target]/libs. (On a 32-bit build, the target seems to be 'i686-pc-mingw32'.)

Copy these into your MinGW-w64/[target] folder, into the include/ and lib/ subfolders, respectively. (Here, the 32-bit target name seems to be 'i686-w64-mingw32'.)
I guess your mileage may vary on the target names, especially if you're compiling for 64-bit, but it gives you an indication of what kind of name to look for.
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Compiling the runtime library

Post by Juergen Kuehlwein »

It sounds like maybe you've taken the ffi.h.in file, and renamed it?
Yes, that´s what i did.
That file has to pass through the configure/make process to fill in some of the variables, in order to become a valid header file that works with your target.
Thanks, i didn´t know that!


JK
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Compiling the runtime library

Post by Juergen Kuehlwein »

I finally ran a successful built!

JK
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Compiling the runtime library

Post by Juergen Kuehlwein »

Ok - "make rtlib TARGET=i686-w64-mingw32" runs without errors now and builds four files in \lib\freebasic\win32: fbextra.x, fbrt0.o. libfb.a and libfbmt.a.

Fbextra.x is the same as the existing one in my FreeBASIC installation, so i copy the other ones into \lib\win32 in my FreeBASIC installation. But then (with the new runtime libraries) i get this linker error when compiling a "Hello World" example:
D:\...lib\win32/libfb.a(init.o):init.c:(.text+0x97): undefined reference to `_imp____acrt_iob_func'
Obviously the newly created runtime libraries don´t work as expected - what am i doing wrong?


JK
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Compiling the runtime library

Post by MrSwiss »

Is the version of GCC the same, as the one used, to 'make' respective FBC?
(check: release notes, of the FBC version)
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Compiling the runtime library

Post by Juergen Kuehlwein »

No - they are different (5.2 vs. 7.3 for the runtime). But how could this be a problem, the library format shouldn´t change between versions - or am i wrong here?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Compiling the runtime library

Post by MrSwiss »

... the library format shouldn´t change between versions - or am i wrong here?
AFAIK, since fixes and/or imrovements may necessitate changes to internal
lib's too, it is a 'wise' choice, to use one (and the same) compiler version,
for a complete FBC build ... (incl. fbCRT, fbGFX2).
(GCC versions mutate too, over time)
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Compiling the runtime library

Post by Juergen Kuehlwein »

I agree that different GCC versions might cause problems.

But in this case i suspect a more basic failure like a missing command line switch, an improper setup or something like that. In fact the orginal libfb.a doesn´t contain "_imp____acrt_iob_func" at all, in the new one you find it 15 times mixed with rtl functions in .rdata sections. That is, in my compilation "_imp____acrt_iob_func" is somehow added as exported function and i didn´t apply any changes to the rtl files.

JK
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Compiling the runtime library

Post by MrSwiss »

There might be a issue similar to: exported proc (aka: public) and, internal proc (aka: private).

I agree, that this may be related, to a missing compiler switch (exclude private's).
Do you use a utility (make, msys, msys2) or, just command line?
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Compiling the runtime library

Post by srvaldez »

I tried to compile FB for Cygwin without success, but I have but one problem when building for windows see viewtopic.php?t=24868
perhaps it will be useful to you
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Compiling the runtime library

Post by Juergen Kuehlwein »

I´m compiling in Windows, therefore i installed MSYS2 and MinGW (V. 7.3, 32 and 64 bit). My desktop link for compiling the runtime library is:

Code: Select all

E:\msys64\msys2_shell.cmd -msys -c "./rtl.sh" 
and rtl.sh is:

Code: Select all

#!/bin/bash
cd ..
cd /d/freebasic/jk-fork-old

export PATH=/e/MinGW/mingw32/bin:$PATH

make rtlib TARGET=i686-w64-mingw32 ENABLE_STANDALONE=1

$SHELL
This compiles the runtime library files (crtfb0.o, libfb.a and libfbmt.a) without any error!

But the resulting libraries as well as some object files contain a function reference ("_imp____acrt_iob_func") which isn´t part of the sources. The function "_imp____acrt_iob_func" doesn´t exist (neither "private" nor "public") in the sources.

When adding the new runtime libraries to my FreeBASIC installation the linker complains about an undefined reference to "_imp____acrt_iob_func", which is correct, because there is no such function. But how can it become part of the object files and subsequently be added to the libraries?


JK
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Compiling the runtime library

Post by MrSwiss »

IIRC, MSYS2 created problems on St_W's Jenkins build server, which where gone,
after switching back, to MSYS ... (can't remember the exact reason).
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Compiling the runtime library

Post by St_W »

Mixing different versions of GCC and/or its runtime libraries may always cause troubles, but after short googling it looks like specific versions of MSYS2 also had a problem that could cause that error (for example this bug report describes that the error was fixed by updating MSYS2: https://github.com/xmrig/xmrig/issues/448). If you choose to do so make a backup copy of your MSYS2 folder first, because the MSYS2 project does not keep older versions of their packages on their servers (in general), so it allows you to install the most recent version only in case you'd need a fresh install.

Anyways, let's assume MSYS2 is fine and have a look at the FB runtime. The FB runtime is written in C and depends on the C runtime libraries (CRT). When linking your FreeBasic program you can see fbc calling the linker with several libraries in addition to the fb runtime libraries: ld.exe -m i386pe -o "test.exe" -subsystem console "fbextra.x" --stack 1048576,1048576 -s -L " "crt2.o" "crtbegin.o" "fbrt0.o" "test.o" "-(" -lfb -lgcc -lmsvcrt -lkernel32 -luser32 -lmingw32 -lmingwex -lmoldname -lgcc_eh "-)" "crtend.o"Some of these are part of the operating system (like msvcrt, kernel32, user32), others are shipping with the C compiler (GCC in our case; like crt*.o, gcc, mingw*) and others are part of the FreeBasic compiler (like fbrt0, fb). You can imagine that you may run into troubles when you compile your FreeBasic runtime in a different environment compared to the one you later use for linking those library to your FreeBasic application.
So what I would recommend (and I think this is also described in the wiki) is to use the same GCC version for both building the runtime library and linking your FreeBasic programs. What you need to do is to copy the libraries (and binaries) from your GCC directory to your FreeBasic build directory after you built your new runtime libraries, and use that fbc instead of copying just the runtime libraries to another (existing) FreeBasic installation. On my CI system I'm using a simple shell script like the following one (you can run that in the MSYS environment) (for 32-bit GCC 7.3.0):

Code: Select all

# Copy to C:\fbc\bin\win32:
#
#    C:\mingw\bin\{ar,as,dlltool,ld}.exe
#    GoRC.exe (from http://www.godevtool.com/8 or the previous FreeBASIC release) #
#
#
# Copy to C:\fbc\lib\win32:
#
#    C:\mingw\lib\{crt2,dllcrt2,gcrt2}.o
#    C:\mingw\lib\lib{gmon,mingw32,mingwex,moldname}.a
#    C:\mingw\lib\gcc\mingw32\[version]\{crtbegin,crtend}.o
#    C:\mingw\lib\gcc\mingw32\[version]\lib{gcc,supc++}.a
#    C:\mingw\lib\lib{advapi32,gdi32,kernel32,msvcrt,user32,winmm,winspool}.a (rename to lib*.dll.a)
#    C:\mingw\lib\libffi.a
#    and any additional libraries you want to use 
#
#
# Rename C:\fbc\fbc-new.exe to C:\fbc\fbc.exe, if wanted 
#

basedir=/d/gcc730
version=7.3.0

mkdir bin/win32 -p
cp $basedir/mingw32/bin/gcc.exe bin/win32/
mkdir bin/i686-w64-mingw32/bin -p
cp $basedir/mingw32/libexec/gcc/i686-w64-mingw32/$version/cc1.exe bin/i686-w64-mingw32/bin/
cp $basedir/mingw32/bin/ar.exe bin/win32/
cp $basedir/mingw32/bin/as.exe bin/win32/
cp $basedir/mingw32/bin/dlltool.exe bin/win32/
cp $basedir/mingw32/bin/ld.exe bin/win32/
cp $basedir/mingw32/bin/GoRC.exe bin/win32/


mkdir lib/win32 -p
cp $basedir/mingw32/i686-w64-mingw32/lib/crt2.o lib/win32/
cp $basedir/mingw32/i686-w64-mingw32/lib/dllcrt2.o lib/win32/
cp $basedir/mingw32/i686-w64-mingw32/lib/gcrt2.o lib/win32/

cp $basedir/mingw32/i686-w64-mingw32/lib/libgmon.a lib/win32/
cp $basedir/mingw32/i686-w64-mingw32/lib/libmingw32.a lib/win32/
cp $basedir/mingw32/i686-w64-mingw32/lib/libmingwex.a lib/win32/
cp $basedir/mingw32/i686-w64-mingw32/lib/libmoldname.a lib/win32/

cp $basedir/mingw32/i686-w64-mingw32/lib/crtbegin.o lib/win32/
cp $basedir/mingw32/i686-w64-mingw32/lib/crtend.o lib/win32/

cp $basedir/mingw32/lib/gcc/i686-w64-mingw32/$version/libgcc.a lib/win32/
cp $basedir/mingw32/lib/gcc/i686-w64-mingw32/$version/libgcc_eh.a lib/win32/
cp $basedir/mingw32/lib/gcc/i686-w64-mingw32/$version/libsupc++.a lib/win32/

cp $basedir/mingw32/i686-w64-mingw32/lib/libadvapi32.a lib/win32/libadvapi32.dll.a
cp $basedir/mingw32/i686-w64-mingw32/lib/libgdi32.a lib/win32/libgdi32.dll.a
cp $basedir/mingw32/i686-w64-mingw32/lib/libkernel32.a lib/win32/libkernel32.dll.a
cp $basedir/mingw32/i686-w64-mingw32/lib/libmsvcrt.a lib/win32/libmsvcrt.dll.a
cp $basedir/mingw32/i686-w64-mingw32/lib/libuser32.a lib/win32/libuser32.dll.a
cp $basedir/mingw32/i686-w64-mingw32/lib/libwinmm.a lib/win32/libwinmm.dll.a
cp $basedir/mingw32/i686-w64-mingw32/lib/libwinspool.a lib/win32/libwinspool.dll.a

cp $basedir/mingw32/i686-w64-mingw32/lib/libffi.a lib/win32/

# required for the functions.argv test
cp $basedir/mingw32/i686-w64-mingw32/lib/libmsimg32.a lib/win32/libmsimg32.dll.a
cp $basedir/mingw32/i686-w64-mingw32/lib/libversion.a lib/win32/libversion.dll.a
cp $basedir/mingw32/i686-w64-mingw32/lib/libimm32.a lib/win32/libimm32.dll.a


#mv fbc-new.exe fbc.exe
Some other things that were mentioned in this thread: Yes, I'm using the old MSYS (the one from MinGW.org) in combination with the MinGW-w64 toolchain on my CI machine, but unfortunately I cannot remember the reason why MSYS2 didn't work there back then either. For manual builds on other machines I'm using MSYS2 nowadays and it works just fine.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Compiling the runtime library

Post by marcov »

St_W wrote: Some of these are part of the operating system (like msvcrt, kernel32, user32),
Note that the import libraries come with msys, not the system. If you use import libraries for these dlls, that is also msys versioned and possibly bugged.
Post Reply