SOLVED : How to compile c emitted code with mingw toolchain

General FreeBASIC programming questions.
Post Reply
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

SOLVED : How to compile c emitted code with mingw toolchain

Post by marpon »

I am under windows 10 using mingw 32 or 64.

I would like to know how to compile the c emitted code drectly with the c compiler,
the idea is to modify the c emitted code by adding more c functions and after compile it directly with c.

I tried compiling some examples, and even I've put all the freebasic static libs on my lib toolchain (giving the path on the makefile)
it always fails not finding the references to the fb-xxxfunction @yy .

how to do it?

tank's in advance for help

Marpon
Last edited by marpon on Feb 23, 2019 11:19, edited 1 time in total.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: How to compile c emitted code with mingw toolchain

Post by counting_pine »

Have you tried running 'fbc -v ...'? It should tell you what external programs it's running, and how it runs them.

Then you should be able to run those same commands on the code produced by 'fbc -R', and hopefully get the same results.
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: How to compile c emitted code with mingw toolchain -SOLVED

Post by marpon »

Thank's for your quick reply.

It was exactly what I've done first ,
putting the fb static libs, and the differents .o files, seen on the compiled cmdline, into the lib folder of my mingw toolchain
replacing fbc by gcc on these cmd line
changing the paths for the static libs and .o to the ones on the lib folder of my mingw toolchain.

putting -v option to follow the process

get an exe without any error/warning

Erratum :
In fact, it works , probably did something wrong, in my first attempt!

sorry and thank's again
Last edited by marpon on Feb 22, 2019 16:46, edited 1 time in total.
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

SOLVED - How to compile c emitted code with mingw toolchain

Post by marpon »

In fact it works very well!

just used the cmdline done from the fbc compiler log when compiling to gen gcc with option -R

put that cmd line into a bach file into the same folder as the .bas (and .c emitted)
and worked without any problem.

after to modify the c emitted code, i added as an example

#include <stdio.h>

and added in the main
printf("To verify");

had to put an -I 'path to stdio-header" on the gcc cmdline to insure the function prototype for printf is defined

and compile without any problem

So, as a conclusion, the people knowing c synthax can easily use the freebasic emitted c code as a draft and append their own c code ,without the need to translate c to fb, and get a finished full working c compiled exe or lib or dll...

That's what i wanted to do!
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

SOLVED: How to compile c emitted code with mingw toolchain

Post by marpon »

a batch file to take the emitted c file as input to compile in c with your evolutions if you have

to use it :
put the right path of your freebasic version 32 or 64
put also the right path of your mingw toolchain same 32 or 64, needed for the .h headers you want to add

and launch it with "name_of_emitted_c_file_without_extension" 32 (or 64)

Code: Select all

@echo off
rem make_c.bat


cd /d %~dp0


if "%1" == "" goto failed
goto suite

:failed
echo.
echo usage : make_c file_c_to_compile_without_extension  32 (or 64)
echo.
echo close now!
goto last

:suite
if "%2" == "" goto do32
if "%2" == "64" goto do64


:do32
echo.
echo doing in 32 bits
 
rem path to fb 32bits version
SET varfb=D:\freebasic\FreeBASIC-1.06.0-win32
rem %varfb%

rem path to fb 32bits version bin folder
SET varbin=%varfb%\bin\win32
rem %varbin%

rem path to fb 32bits version lib folder
SET varlib=%varfb%\lib\win32
rem %varlib%

rem path to include32 c headers   .h
SET varinc=D:\mingw-w32-7.1.0\mingw32\i686-w64-mingw32\include
rem %varinc%


goto build

:do64
echo.
echo doing in 64 bits

rem path to fb 64bits version 
SET varfb=D:\freebasic\FreeBASIC-1.06.0-win64
rem %varfb%

rem path to fb 64bits version bin folder
SET varbin=%varfb%\bin\win64
rem %varbin%

rem path to fb 64bits version lib folder
SET varlib=%varfb%\lib\win64
rem %varlib%

rem path to include64 c headers   .h
SET varinc=D:\mingw-w64-7.1.0\mingw64\x86_64-w64-mingw32\include
rem %varinc%


:build
echo.
echo.
echo launch compile and link 


if "%2" == "" goto make32
if "%2" == "64" goto make64
:make32

"%varbin%\gcc.exe" -m32 -march=i486 -S -I %varinc% -nostdlib -nostdinc -Wall -Wno-unused-label -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-main -Werror-implicit-function-declaration -O2 -fno-strict-aliasing -frounding-math -fno-math-errno -fwrapv -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -masm=intel "%1.c" -o "%1.asm"
"%varbin%\as.exe" --32 --strip-local-absolute "%1.asm" -o "%1.o"
"%varbin%\ld.exe" -m i386pe -o "%1_32.exe" -subsystem console "%varlib%\fbextra.x" --stack 1048576,1048576 -s -L "%varlib%" -L "." "%varlib%\crt2.o" "%varlib%\crtbegin.o" "%varlib%\fbrt0.o" "%1.o" "-(" -lfbgfx -lgdi32 -lwinmm -lfb -lgcc -lmsvcrt -lkernel32 -luser32 -lmingw32 -lmingwex -lmoldname -lgcc_eh "-)" "%varlib%\crtend.o" 

goto last

:make64

"%varbin%\gcc.exe" -m64 -march=x86-64 -S -I %varinc% -nostdlib -nostdinc -Wall -Wno-unused-label -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-main -Werror-implicit-function-declaration -O2 -fno-strict-aliasing -frounding-math -fno-math-errno -fwrapv -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -masm=intel "%1.c" -o "%1.asm"
"%varbin%\as.exe" --64 --strip-local-absolute "%1.asm" -o "%1.o"
"%varbin%\ld.exe" -m i386pep -o "%1_64.exe" -subsystem console "%varlib%\fbextra.x" --stack 1048576,1048576 -s -L "%varlib%" -L "." "%varlib%\crt2.o" "%varlib%\crtbegin.o" "%varlib%\fbrt0.o" "%1.o" "-(" -lfbgfx -lgdi32 -lwinmm -lfb -lgcc -lmsvcrt -lkernel32 -luser32 -lmingw32 -lmingwex -lmoldname -lgcc_eh "-)" "%varlib%\crtend.o" 


:last
echo.
echo.
pause
Post Reply