Programming Newbie Compiler Question

New to FreeBASIC? Post your questions here.
seafriend
Posts: 1
Joined: Feb 26, 2018 19:35

Programming Newbie Compiler Question

Post by seafriend »

Trying to compile 2 source files, but get unexpected ORDER of operation when running .exe:
hello1.bas
print "Hello1"
print "Press any key to continue..."
sleep

hello2.bas
print "Hello2"
print "Press any key to continue..."
sleep

Compile command used: fbc hello1.bas hello2.bas

I expected "Hello1" to print first but "Hello2" printed first when running .exe; The instructions say that the 1st source file in the list is treated as
the "main entry point", so not sure what is wrong?

Also tried to ensure that "hello1" was the entry point by using: fbc -m hello hello2.bas
But got the following error: C:\Program Files (x86)\FreeBASIC\lib\win32/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:
(.text.startup+0x39): undefined reference to `WinMain@16'

Also tried: fbc -m hello -b hello2.bas but got another long compiler error. I'm running the (x86) version on Win10. Any kind/gentle help is appreciated, as I am a newbie.
Boromir
Posts: 463
Joined: Apr 30, 2015 19:28
Location: Oklahoma,U.S., Earth,Solar System
Contact:

Re: Programming Newbie Compiler Question

Post by Boromir »

That is the expected order.
Are you familiar with subs and functions?
Generally, your non main .bas files would include all of your functions and subs.
They are then compiled first so that your main .bas file will be able to use them.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Programming Newbie Compiler Question

Post by lizard »

Why would one want to compile two .bas files in one executable? The second could be #included in the first. Mostly it contains functions, subs and such. Additionally .o, .rc or other files could be linked in. :-)
Boromir
Posts: 463
Joined: Apr 30, 2015 19:28
Location: Oklahoma,U.S., Earth,Solar System
Contact:

Re: Programming Newbie Compiler Question

Post by Boromir »

lizard wrote:Why would one want to compile two .bas files in one executable? The second could be #included in the first. Mostly it contains functions, subs and such. Additionally .o, .rc or other files could be linked in. :-)
There is no rule regarding the use of .bi vs .bas but it is general practice to declare functions,subs,types,etc.. in the include files whereas definitions are generally put in the .bas files.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Programming Newbie Compiler Question

Post by caseih »

I'm with Boromir on this. You shouldn't be including .bas files. The main reasons are managing dependencies and speed of compilation. On large projects you don't want a situation where one change in a a .bas file somewhere requires compiling the entire thing over again. Keep things modular and as uncoupled as possible. Compile each .bas file separately and link them together. FBC itself does this for you:

fbc -o myexe first.bas second.bas third.bas

In general, .bi files should only contain declarations for working with functions in other modules or libraries.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Programming Newbie Compiler Question

Post by caseih »

seafriend wrote: Also tried to ensure that "hello1" was the entry point by using: fbc -m hello hello2.bas
But got the following error: C:\Program Files (x86)\FreeBASIC\lib\win32/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:
(.text.startup+0x39): undefined reference to `WinMain@16'

Also tried: fbc -m hello -b hello2.bas but got another long compiler error. I'm running the (x86) version on Win10. Any kind/gentle help is appreciated, as I am a newbie.
You told fbc to use "hello" as the main module, but you provided hello1 and hello2 as your modules, so that's why it couldn't find it.

In general you don't really want main-level code in your various non-main modules. Restrict them to functions and subs, and you'll not have problems. Main-level code is only for initializing the module at run time, for example initializing module-global variables. The order in which the modules are initialized is not guaranteed as you've found.
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: Programming Newbie Compiler Question

Post by adele »

Hi,

@seafriend , I´m with you.
@lizard,you asked:
» Feb 27, 2018 13:55
"Why would one want to compile two .bas files in one executable? The second could be #included in the first. Mostly it contains functions, subs and such. Additionally .o, .rc or other files could be linked in. :-)"

Why?
IMO seafriend thought s.th. like...
Just to try with _very_ simple conditions how the compiler/linker etc work.
If seafriend ( hi! you´re welcome ! ) is not happy with assumptions, but tries to try the "real things", ok, in my opinion this is the right way to end up in planned coding, not in desperate try&error orgies.

Adi
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: Programming Newbie Compiler Question

Post by Kuan Hsu »

Dear seafriend:

If you compile using:
fbc hello2.bas hello1.bas or fbc -m hello2 hello1.bas hello2.bas
you will get what you want~~

But why? I don't know the rules, maybe FBC set hello2.bas is the entry point the add the source of hello1.bas to top of source of hello2.bas ( like STACK? )
Later maybe you will create your include file( *.bi ) in your project, my personal opion is like Boromir:
Boromir wrote:There is no rule regarding the use of .bi vs .bas but it is general practice to declare functions,subs,types,etc.. in the include files whereas definitions are generally put in the .bas files.
Dinosaur
Posts: 1478
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Programming Newbie Compiler Question

Post by Dinosaur »

Hi All

This subject seems to be what I have problems with here:
viewtopic.php?f=8&t=23935&start=765#p243995 last post.

For ease of reading I break .bas modules into categories of purpose.
Like core GUI code, Initialisation code, usb code , Hardware I/O etc etc.
So in other words I don't want one big file to browse through.

There is obviously a number of ways to compile multiple .bas files.
1: Write the fbc line to add all the .bas modules you want to add.
So it becomes a Mile long line in big projects.
2. Add #Include statements in the Main module.
#Include Module2.bas
#Include Module3.bas
etc
3. Add One #Include Project.bi statement and fill in the Project.bi with all the includes.
This what I do. If I change a Hardware I/O board, I simply change to the Project.bi for that board.

I have done this since the early QuickBasic days and did the same with MASM.

Looking at a lot of C code lately, the same thing is being done there in .h files with sections of code in them.
I don't see the objection to doing this if it works and achieves the end result.

Regards
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: Programming Newbie Compiler Question

Post by Kuan Hsu »

Like below:
Compile a.bas
Image
Build project:
Image

I think the multiple definition issue is when compile a.bas, it add the code of b.bas to a.bas, so a.bas have the sum() function, if we build the project, compiler continue compile the b.bas, so we have two sum() function, multiple definition fail....
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Programming Newbie Compiler Question

Post by deltarho[1859] »

Going back to the opening post. I'm with Dinosaur on this.

Hello.bas

Code: Select all

#Include "Hello1.bas"
#Include "Hello2.bas"
Print "Done"
Sleep
fbc Hello.bas will see "Hello1" printed first.

On the other hand

Hello.bas

Code: Select all

#Include "Hello2.bas"
#Include "Hello1.bas"
Print "Done"
Sleep
fbc Hello.bas will see "Hello2" printed first.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Programming Newbie Compiler Question

Post by caseih »

Kuan Hsu wrote:I think the multiple definition issue is when compile a.bas, it add the code of b.bas to a.bas, so a.bas have the sum() function, if we build the project, compiler continue compile the b.bas, so we have two sum() function, multiple definition fail....
Correct. You've told your IDE that the project consists of two files, a.bas and b.bas, so it will compile both of them and try to link them together. But then you went in to a.bas and had it include b.bas. This means now the a module has a function sum() in it. But b.bas also has a function sum() in it. Hence the linker is going to complain that you have conflicting symbols. Why would you expect otherwise? There are two ways to solve the problem:
  • Remove b.bas from the project listing so it won't be compiled separately. This has the disadvantage that you can't easily open b.bas for editing in the IDE since it doesn't believe it's part of the project (and in fact it isn't).
  • Remove the #include line from a.bas and replace it with either a declare function for sum, or create a .bi file that declares the function.
The common practice in the programming world is the latter. The first solution, that MrSwiss and Dinosaur seem to be promoting is... unusual and extremely unorthodox to say the least. And in the context of an IDE like PoseidonFB with a built-in build system, problematic. That is to say it won't work.

If you're going to use multiple .bas files and you're using a nice IDE like poseidenFB to manage the build, then definitely choose the latter. Hope this helps.

Your a.bas file should be like this:

Code: Select all

#include "b.bi"

print sum(4,5)
And b.bi is:

Code: Select all

declare function sum(a as integer, b as integer) as integer
Alternatively just put the declaration in a.bas:

Code: Select all

declare function sum(a as integer, b as integer) as integer

print sum(6,7)
For modularity and ease of development, putting the declarations in a .bi file is recommended. Usually there will be one .bi file for each of your .bas modules. Sometimes you will have standalone .bi files that contain configuration information that you can include in any module that needs it.
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: Programming Newbie Compiler Question

Post by Kuan Hsu »

caseih wrote: [*]Remove b.bas from the project listing so it won't be compiled separately. This has the disadvantage that you can't easily open b.bas for editing in the IDE since it doesn't believe it's part of the project (and in fact it isn't).
[*]Remove the #include line from a.bas and replace it with either a declare function for sum, or create a .bi file that declares the function.[/list]

The common practice in the programming world is the latter. The first solution, that MrSwiss and Dinosaur seem to be promoting is... unusual and extremely unorthodox to say the least. And in the context of an IDE like PoseidonFB with a built-in build system, problematic. That is to say it won't work.

If you're going to use multiple .bas files and you're using a nice IDE like poseidenFB to manage the build, then definitely choose the latter. Hope this helps.
There is a workaround about solution 1 in poseidonFB: ( for my example )
(0) Compile a.bas( focus on a.bas the press "Compile" ) but will create a.exe not "projectName".exe or "projectTarget".exe
(1) "Right-click" "Compile" button on toolbar, add compiler option with -x projectName".exe or -x "projectTarget".exe and compile the sources
Image
(2) Press "Run" button on toolbar now can execute the program.....
(3) Not straightforward......
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Programming Newbie Compiler Question

Post by MrSwiss »

caseih wrote:The first solution, that MrSwiss and Dinosaur seem to be promoting is...
First and foremost:
I don't promote anything here, I've simply pointed out the source of the problem and a *workaround*.

Explanation of the issue:
1) before we had build systems (but, still wanting 2 below)
2) wanting to program in modular fashion (leading to 3)
3) was simply the only sensible way of approach (include a file, that manages all the necssary
other includes [usually in those days: .bas files])

This wasn't in any way either "unorthodox" nor "unusual", it was rather "common practice" for
the few coders wanting it modular, in the early days (as stated before: NO BUILD SYSTEMS).
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Programming Newbie Compiler Question

Post by caseih »

I'm not sure when these early days were that you speak of[1], but I understand what you're saying now.

[1] QuickBasic had a way of working with multiple .bas files in one project and compiling them each separately before linking them all together, although it may have been fairly primitive. PowerBasic for DOS could import external object files and libraries, but I don't think it had a built-in build management system.
Post Reply