问题
A C library contains this code:
/*
* Custom specific code here...
* several static functions.
*/
/**
* receiveMessages( +Socket, ?DoorsAreOpen, ?DoorsAreClosed, ?CabinRequests, ?CabinLocation, ?LandingRequests )
*/
static foreign_t receiveMessages(
term_t Socket,
term_t doorsAreOpen,
term_t doorsAreClosed,
term_t cabinRequests,
term_t cabinLocation,
term_t landingRequests )
{
int sockfd;
return PL_get_integer_ex( Socket, &sockfd )
&& sockfd
&& udpReceive( sockfd, doorsAreOpen, doorsAreClosed, cabinRequests, cabinLocation, landingRequests );
}
void install_elevatorMessages( void ) {
request2 = PL_new_functor( PL_new_atom( "request" ), 2 );
PL_register_foreign( "receiveMessages", 6, receiveMessages, 0 );
}
Eclipse CDT is used to create the shared library libelevatorMessages.so
.
$ nm /home/aubin/Dev/IA/prolog-Ascenseur/Debug/libelevatorMessages.so | grep -w T
0000000000001210 T _fini
0000000000000918 T _init
00000000000011c5 T install_elevatorMessages
Loading fail:
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.13)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- assertz( file_search_path( elevatorLibsPath, '/home/aubin/Dev/IA/prolog-Ascenseur/Debug/' )).
true.
?- load_foreign_library( elevatorLibsPath( elevatorMessages )).
ERROR: source_sink `elevatorLibsPath(elevatorMessages)' does not exist
ERROR: In:
ERROR: [16] throw(error(existence_error(source_sink,...),_14836))
ERROR: [14] shlib:load_foreign_library(elevatorLibsPath(elevatorMessages),user,default(install)) at /usr/lib/swi-prolog/library/shlib.pl:381
ERROR: [13] <meta call>
ERROR: [12] with_mutex('$foreign',load_foreign_library(elevatorLibsPath(elevatorMessages),user,default(install))) <foreign>
ERROR: [9] toplevel_call(user:user: ...) at /usr/lib/swi-prolog/boot/toplevel.pl:1113
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
Exception: (14) shlib:load_foreign_library(elevatorLibsPath(elevatorMessages), user, default(install)) ? abort
% Execution Aborted
?-
Why?
回答1:
The conventions about UNIX libraries doesn't apply here, nor the use of LD_LIBRARY_PATH
.
The install method name, in C code, is deduced from the name of the library file, if we name the library conventionally libelevatorMessages.so
, we have to rename the install
method too.
Finally, the names and paths are:
- In Prolog:
:- use_foreign_library( './Debug/libelevatorMessages' ).
- In C :
void install_libelevatorMessages( void )
Note the relative path to access the library and the omission of .so
.
来源:https://stackoverflow.com/questions/64917179/foreign-library-cant-be-loaded