What is an "UNDEFINED REFERENCE" ?

General FreeBASIC programming questions.
Post Reply
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

What is an "UNDEFINED REFERENCE" ?

Post by integer »

I do not know what the compiler output actually means.
Command executed:
"C:\FreeBasic\fbc.exe" "C:\FB37\May25.bas" -ex

Compiler output:
C:\FB37\May25.o:fake:(.text+0x1351): undefined reference to `STATIONOFFSET@40'
C:\FB37\May25.o:fake:(.text+0x560e): undefined reference to `STATIONOFFSET@40'
C:\FB37\May25.o:fake:(.text+0x5e80): undefined reference to `STATIONOFFSET@40'
C:\FB37\May25.o:fake:(.text+0x7911): undefined reference to `STATIONOFFSET@40'
C:\FB37\May25.o:fake:(.text+0x835e): undefined reference to `SYSTEMHARDWARE@32'
C:\FB37\May25.o:fake:(.text+0x83d7): undefined reference to `SYSTEMHARDWARE@32'

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc: FreeBASIC Compiler - Version 0.18 (05-08-2007) for win32 (target:win32)
OS: Windows XP (build 2600, Service Pack 2)
This does not appear in all of the compiler warnings and error messages listed on the Wiki page, or the manual.

I can understand what a reference to an undefined variable, symbol, sub or function might be. But an 'undefined reference' seems to imply an indirect access method.

With the hexadecimal numbers given (addresses?) I do not know how to map those values to lines in the program that generated the problem.

Anyone know what the compiler output means?
Is there enough information given to trace back to a specific line in the source code?


[/quote]
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

"Undefined references" are linker errors. It means it can't find the definition of one of the symbols your code references (uses), either because it wasn't defined, or the module in which it is defined is not being linked with. Make sure that STATIONOFFSET and SYSTEMHARDWARE are defined in some module (or library) and that module (or library) is being linked with.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

It usually means you Declared a Sub that does not really exist.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

KristopherWindsor wrote:It usually means you Declared a Sub that does not really exist.
AND you called it :P
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Post by integer »

THANKS TO ALL.

I declared the subs and I created the SUB,
and placed them in my library.

BUT
the multi-line comment feature caused all of my brilliant work to be ignored.

There ought to be a law against inattentiveness.
I feel terrible about making such a blunder.
mmbcastle
Posts: 16
Joined: May 05, 2021 11:46
Location: Tennessee, USA
Contact:

Re: What is an "UNDEFINED REFERENCE" ?

Post by mmbcastle »

Sorry to bump an old thread, but this seems like the closest fit for what I'm running in to, and it gets a high Google hit, so it'll be useful for other people having the same issue.

Since Windows is going to lose 16 bit code support one of these days, I've been looking for a QB replacement, and I'm hoping FB is going to be it. I have hundreds of thousands of lines of QB code in production I need to port over. As part of that, I have an extensive library of functions for screen handling, threaded binary trees, string manipulation, etc I need to port first. So far, this has been most successful trying to make them as static libraries.

So I don't go into too much more detail now, I started with some of the "core" libraries. I got my "BaseLib" to compile with adjustments to make it compile in fblite mode. I moved on to my "SCBSLib" (Screen base lib), which referenced BaseLib. After some tweaking of the bi files, libpath, etc, I got it to compile and stop complaining about undefined references. I built a "SondLib" with Angelo Rosina's SOUND replacement in it, then I moved on to my "ScrnLib" (full screen input library). After some more fiddling, I got it to compile without undefined references, yeah!

However, now I'm trying to compile my first test program that actually uses all these things. When I compile it, I'm back to getting undefined references. I've tried combining these libraries into one, but that doesn't work either.

Is there a problem with mixing language modes (fb for sondlib, fblite for everything else)? Something else I need to do?
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: What is an "UNDEFINED REFERENCE" ?

Post by SARG »

You should post the exact errors you get and some others details (32/64bit, gas/gcc, compilation options).
That'll be easier for helping you.
mmbcastle
Posts: 16
Joined: May 05, 2021 11:46
Location: Tennessee, USA
Contact:

Re: What is an "UNDEFINED REFERENCE" ?

Post by mmbcastle »

FB v1.07.2 Win10 32bit. No compiler command line options.

To break it into smaller pieces, I'm working on a program to test just the BaseLib library functions.

It was going fine. I had tested about 10 functions until I added references to theses..

basetest.o:fake:(.text+0x517): undefined reference to `PUTSTRING@20'
basetest.o:fake:(.text+0x5ae): undefined reference to `QBINKEY@16'
basetest.o:fake:(.text+0x684): undefined reference to `PRINT.MESSAGE@12'

The compiler directives in "BaseTest.bas" are...

#lang "fblite"
#libpath "m:\fb\bbslib"
#include once "m:\fb\bbslib\baselib.bi"

The definitions of these functions in baselib.bi are...

declare SUB PUTSTRING (ByRef MAIN$ , ByVal ADD$ , ByVal PSTN% )
declare SUB qbINKEY (ByRef ParmVAR$ , ByVal VALID$ )
declare SUB PRINT.MESSAGE (ByVal MSG$ )

The definitions in baselib.bas are...

public SUB PUTSTRING (MAIN$, ADD$, PSTN%) STATIC
public SUB qbINKEY (ParmVAR$, VALID$) STATIC
public SUB PRINT.MESSAGE (MSG$) STATIC

One thought was that it had to do with multi-parameter calls, but this one works...

declare SUB WORDWRAP (ByRef WRAPSTRG$ , ByRef LNE% , ByRef COL% )
public SUB WORDWRAP (WRAPSTRG$, LNE%, COL%) STATIC

One note: I know there are better ways to define data types, etc. I have written many programs in VB, Java, C and most recently learned PHP. I'm just trying to find the fastest, most efficient way to convert all this legacy code without re-writing it. You can see how I'm renaming things because of naming conflicts (the original QB names are ALLUPPER). I already know because FB doesn't support the FIELD statement for random files, I'll have a lot of conversion work there.
mmbcastle
Posts: 16
Joined: May 05, 2021 11:46
Location: Tennessee, USA
Contact:

Re: What is an "UNDEFINED REFERENCE" ?

Post by mmbcastle »

After further testing, it appears to be related to the ByVal parameters. There is an internal function TRIMMER that has been working in the test program, but when I call it directly, I get...

basetest.o:fake:(.text+0x57a): undefined reference to `TRIMMER@40'

Definitions...

declare SUB TRIMMER (ByVal INSTRING$, ByRef OUTSTRING$, ByVal WHITE$, ByVal CTRL$)
public SUB TRIMMER (INSTRING$, OUTSTRING$, WHITE$, CTRL$) STATIC

The quick answer would appear to be to drop any ByVal and just convert them all to ByRef, but I was looking forward to having that bit of extra safety introduced as a result of the FB port.

As an untested guess, "fblite" assumes all references are ByRef by default, so when I define them as ByVal in the header I'm creating a reference that doesn't match the profile of the function in the library?
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: What is an "UNDEFINED REFERENCE" ?

Post by SARG »

From the help :
In the -lang fb dialect, ByVal is the default parameter passing convention for all built-in types except String and user-defined Type which are passed ByRef by default.
The ZString and WString built-in types are also passed ByRef by default, but passing ByVal is forbidden. Arrays are always passed ByRef and the use of the specifier ByRef or ByVal is forbidden.

In -lang qb and -lang fblite dialects, ByRef is the default parameter passing convention.
Either you change the libraries (for more safety) or when calling the libraries you force the declaration with byref. :-)
mmbcastle
Posts: 16
Joined: May 05, 2021 11:46
Location: Tennessee, USA
Contact:

Re: What is an "UNDEFINED REFERENCE" ?

Post by mmbcastle »

Thanks for the confirmation of the fblite default.

For expediency of this initial "proof of concept" port of the library, I'll probably just swap in the ByRef declarations. There are a lot of things I'll have to come back and revisit after I confirm this is going to work and start optimizing.
yevrowl
Posts: 13
Joined: Aug 12, 2022 21:37
Location: Kiev
Contact:

Re: What is an "UNDEFINED REFERENCE" ?

Post by yevrowl »

Trying to compile the file http://truechess.org/chess.bas

And get an error:
$ fbc -lang qb -gen gcc -O 2 chess.bas
/usr/local/bin/../bin/ld: /usr/local/bin/../lib/freebasic/freebsd-x86_64/libfb.a(hook_ports.o): in function ‛fb_Wait’:
hook_ports.c:(.text+0xbf): undefined reference to ‛fb_hIn’
/usr/local/bin/../bin/ld: /usr/local/bin/../lib/freebasic/freebsd-x86_64/libfb.a(hook_ports.o): in function ‛fb_In’:
hook_ports.c:(.text+0x39): undefined reference to ‛fb_hIn’
/usr/local/bin/../bin/ld: /usr/local/bin/../lib/freebasic/freebsd-x86_64/libfb.a(hook_ports.o): in function ‛fb_Out’:
hook_ports.c:(.text+0x7c): undefined reference to ‛fb_hOut’
At the same time, under DOS and Windows, it is compiled without problems. Please suggest what could be the reason?

Version of FreeBasic: 1.09.0.
Operating system: FreeBSD (GhostBSD).
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: What is an "UNDEFINED REFERENCE" ?

Post by coderJeff »

yevrowl wrote: Dec 29, 2022 22:49 At the same time, under DOS and Windows, it is compiled without problems. Please suggest what could be the reason?
The OUT statement is not available on FreeBSD.
You can probably remove the OUT statements from the chess.bas source.

In this case 'undefined reference' message by the linker indicates that there is a procedure that is declared and called but is not defined anywhere.

In this specific case on FreeBSD, the fb run time library is trying to link to definitions for fb_wait, fb_in, fb_out, but they don't exist in the FreeBSD version of the run-time library. They have never been implemented.

in the chess.bas program:
- Out &H64, &HFE - maybe some kind of keyboard reset?
- Out 984, 46 - something to with palette or colours on CGA?
statements want to use the fb_out function from fb's run time library, but it doesn't exist.

The OUT statement is for accessing hardware directly and the program can maybe work without them.
yevrowl
Posts: 13
Joined: Aug 12, 2022 21:37
Location: Kiev
Contact:

Re: What is an "UNDEFINED REFERENCE" ?

Post by yevrowl »

coderJeff wrote: Dec 30, 2022 0:16
yevrowl wrote: Dec 29, 2022 22:49 At the same time, under DOS and Windows, it is compiled without problems. Please suggest what could be the reason?
The OUT statement is not available on FreeBSD.
Thanks a lot! Now the sources have been successfully compiled!
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: What is an "UNDEFINED REFERENCE" ?

Post by marcov »

(iirc there is a header x86intr.h that declares iopl and ioperm to request permissions, after which you can use normal port I/O)
Post Reply