Programming Newbie Compiler Question
Programming Newbie Compiler Question
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.
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.
Re: Programming Newbie Compiler Question
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.
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.
Re: Programming Newbie Compiler Question
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. :-)
Re: Programming Newbie Compiler Question
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.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. :-)
Re: Programming Newbie Compiler Question
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.
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.
Re: Programming Newbie Compiler Question
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.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.
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.
Re: Programming Newbie Compiler Question
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
@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
Re: Programming Newbie Compiler Question
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:
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.
Re: Programming Newbie Compiler Question
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
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
Re: Programming Newbie Compiler Question
Like below:
Compile a.bas
Build project:
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....
Compile a.bas
Build project:
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....
-
- Posts: 4292
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Programming Newbie Compiler Question
Going back to the opening post. I'm with Dinosaur on this.
Hello.bas
fbc Hello.bas will see "Hello1" printed first.
On the other hand
Hello.bas
fbc Hello.bas will see "Hello2" printed first.
Hello.bas
Code: Select all
#Include "Hello1.bas"
#Include "Hello2.bas"
Print "Done"
Sleep
On the other hand
Hello.bas
Code: Select all
#Include "Hello2.bas"
#Include "Hello1.bas"
Print "Done"
Sleep
Re: Programming Newbie Compiler Question
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: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....
- 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.
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)
Code: Select all
declare function sum(a as integer, b as integer) as integer
Code: Select all
declare function sum(a as integer, b as integer) as integer
print sum(6,7)
Re: Programming Newbie Compiler Question
There is a workaround about solution 1 in poseidonFB: ( for my example )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.
(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
(2) Press "Run" button on toolbar now can execute the program.....
(3) Not straightforward......
Re: Programming Newbie Compiler Question
First and foremost:caseih wrote:The first solution, that MrSwiss and Dinosaur seem to be promoting is...
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).
Re: Programming Newbie Compiler Question
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.
[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.