Forum > General

Library calling troubles


EasyTEF Team:
Hi everyone,

I'm trying to call a simple library of mine but the program simply doesn't run. I'm using the following call:

--- Code: ---function retornar: LongBool; stdcall; external '';

--- End code ---

When I comment it, the program runs. Attached is program and library sources. I'm using Lazarus 0.9.29 and fpc 2.5.1

Looks like project1 can't find but both files are in same directory:

robson@ubuntu:~/tmp$ ls project1 -la
-rwxr-xr-x 1 robson robson  1087450 2010-10-24 15:44
-rwxr-xr-x 1 robson robson 21503685 2010-10-24 16:06 project1

Am I missing something? I have no idea what to do... Attached is program and library source code. Here is terminal output message. Take a look at the last message  %)

Free Pascal Compiler version 2.5.1 [2010/10/24] for i386
Copyright (c) 1993-2010 by Florian Klaempfl
Target OS: Linux for i386
Compiling project1.lpr
Compiling resource /home/robson/tmp/lib/i386-linux/project1.or
Linking project1
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
20 lines compiled, 1.5 sec
2 hint(s) issued
[TCompiler.Compile] end
TMainIDE.DoInitProjectRun ProgramFilename=/home/robson/tmp/project1
[TCmdLineDebugger] Debug PID: 4058
TGDBMIDebugger.ProcessResult Error: ,msg="No symbol table is loaded.  Use the \"file\" command."
[Debugger] Running GDB version: GDB
[TDebugger.SetFileName] "/home/robson/tmp/project1"
[TMainIDE.DoRunProject] B TGDBMIDebugger
[TMainIDE.DoRunProject] END
TGDBMIDebugger.StartDebugging WorkingDir="/home/robson/tmp/"
TGDBMIDebugger.ProcessResult Error: ,msg="No symbol \"FPC_THREADVAR_RELOCATE_PROC\" in current context."
[Debugger] Log output: &"info functions FPC_CPUINIT\n"
[Debugger] Log output: &"set width 50000\n"
[Debugger] Log output: &"info address main\n"
[Debugger] Log output: &"ptype TObject\n"
[Debugger] Log output: &"info address FPC_RAISEEXCEPTION\n"
[Debugger] Log output: &"info address FPC_BREAK_ERROR\n"
[Debugger] Log output: &"info address FPC_RUNERROR\n"
[Debugger] Log output: &"info file\n"
[Debugger] File type: elf32-i386
[Debugger] Entry point: 0x82b1b10
[Debugger] Log output: &"Function \"foo\" not defined.\n"
[WARNING] Debugger: Unexpected async-record: =thread-group-started,id="i1",pid="4064"
[DBGTGT] /home/robson/tmp/project1: error while loading shared libraries: cannot open shared object file: No such file or directory

I don't use Linux and don't know the default behavior for how it locates libraries, but try putting your .so in a common location for libraries, maybe /usr/lib or /usr/local/lib and see if that makes any difference. If so, it probably means it doesn't look by default in the executable's folder the way Windows does. Also, I believe in Linux the LD_LIBRARY_PATH environment setting gives the list of folders to search.

For more control over the location of a library, you could load it manually with LoadLibrary, where you pass in the path to the library, then find each function that you want with GetProcAddress. See the FPC MySQL source for example of how to do that.




In Linux, you need to add your .so file directory to the /etc/ file. After adding the directory to the execute the ldconf. In Ubuntu you can create a simple text file which suffix is ".conf" (without quotes) in the /etc/ and add the mentioned .so file directory to the file then execute the ldconf.

EasyTEF Team:
Thanx guys!
You helped me a lot! I've used LoadLibrary and GetProcAddress functions and it just worked! I'll check it out about the .conf file ;)

LoadLibrary and GetProcAddress are the better solution. You won't have to worry about where the OS is searching for the library since you specify its path yourself.

Also, you can check for the existence of the library and handle that situation yourself. Generally the OS will throw an exception if it can't load the library itself.




[0] Message Index

Go to full version