[fpc-pascal] Statically link library

Ludo Brands ludo.brands at free.fr
Sun Apr 21 18:35:16 CEST 2013


On 04/21/2013 05:20 PM, Darius Blaszyk wrote:
> <retrying with a smaller zip>
> 
> I have prepared a minimal example of the linking problems I get.  Just
> unzip, create a "build" dir and run cmake. I'm using gcc 4.6.2.
> 
> As soon as I try to use the function "file_size" I get the following
> linker errors:
> 
> Free Pascal Compiler version 2.6.2 [2013/03/17] for i386
> Copyright (c) 1993-2012 by Florian Klaempfl and others
> Target OS: Win32 for i386
> Compiling test.pp
> Compiling test_h.pas
> Linking project1.exe
> test.pp(9,1) Error: Undefined symbol: _open
> test.pp(9,1) Error: Undefined symbol: _filesize
> test.pp(9,1) Error: Undefined symbol: _close
> test.pp(9,1) Fatal: There were 3 errors compiling module, stopping
> 
> It really seems I need to add an additional library to the pascal source
> or that the linker cannot find some included symbols in libmsvcrt.a.
> 
> Any help appreciated.
> 

A few things wrong in your files:
- the c program. When you want to link against msvcrt then you better
use the ms functions;) There is no filesize, or open, or close (the
latter are deprecated since a while and not present anymore in msvcrt)
This works:

#include <stdio.h>

#include "test.h"
#include <fcntl.h>

int file_size(char * name)
{
	int file, size;

	if (name == 0) return(0);
	file = _open(name, O_RDONLY);
	if (file == -1) return (0);

	size = _filelength(file);
	_close(file);
	return(size);
}

- the binding:

unit test_h;

interface

{$linklib libtest.a}
{$linklib libmsvcrt.a}

uses
  ctypes;

function file_size(Name: PChar): cint; cdecl;external ;

implementation

end.

This runs correctly on my system.

I have attached the modified files and the 2 .a files (windows x86). The
MakeFile.win created by wxDev-C++ is included. I renamed the
Output/MingW/Project1.a to libtest.a. When using wxDev-C++ make sure to
set the file to compile as a C file. Default is C++ and that uses a
different name mangling.

Ludo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libtest.zip
Type: application/x-zip-compressed
Size: 33060 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130421/8ce6eafd/attachment.bin>


More information about the fpc-pascal mailing list